diff options
author | Martin v. Löwis <martin@v.loewis.de> | 2008-06-04 14:18:43 (GMT) |
---|---|---|
committer | Martin v. Löwis <martin@v.loewis.de> | 2008-06-04 14:18:43 (GMT) |
commit | 00709aaa3dc7da374e94a3ede8e05603e8a95066 (patch) | |
tree | 50acc73f0d94cd4294b8a6a0f2efdb03cb43f409 /Python | |
parent | 01a7d82432edbf297c32289737a40d7103a03527 (diff) | |
download | cpython-00709aaa3dc7da374e94a3ede8e05603e8a95066.zip cpython-00709aaa3dc7da374e94a3ede8e05603e8a95066.tar.gz cpython-00709aaa3dc7da374e94a3ede8e05603e8a95066.tar.bz2 |
Merged revisions 63856-63857,63859-63860 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r63856 | robert.schuppenies | 2008-06-01 18:16:17 +0200 (So, 01 Jun 2008) | 2 lines
Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
........
r63859 | georg.brandl | 2008-06-01 18:42:16 +0200 (So, 01 Jun 2008) | 2 lines
Some style nits. Also clarify in the docstrings what __sizeof__ does.
........
r63860 | georg.brandl | 2008-06-01 19:05:56 +0200 (So, 01 Jun 2008) | 2 lines
Fix test_descrtut.
........
Diffstat (limited to 'Python')
-rw-r--r-- | Python/sysmodule.c | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/Python/sysmodule.c b/Python/sysmodule.c index dee4965..de2e91d 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -610,6 +610,39 @@ sys_mdebug(PyObject *self, PyObject *args) #endif /* USE_MALLOPT */ static PyObject * +sys_getsizeof(PyObject *self, PyObject *args) +{ + static PyObject * str__sizeof__ = NULL; + + /* Initialize static variable needed by _PyType_Lookup */ + if (str__sizeof__ == NULL) { + str__sizeof__ = PyUnicode_InternFromString("__sizeof__"); + if (str__sizeof__ == NULL) + return NULL; + } + + /* Type objects */ + if (PyType_Check(args)){ + PyObject *method = _PyType_Lookup(Py_TYPE(args), + str__sizeof__); + if (method == NULL) { + PyErr_Format(PyExc_TypeError, + "Type %.100s doesn't define __sizeof__", + Py_TYPE(args)->tp_name); + return NULL; + } + return PyObject_CallFunctionObjArgs(method, args, NULL); + } + else + return PyObject_CallMethod(args, "__sizeof__", NULL); +} + +PyDoc_STRVAR(getsizeof_doc, +"getsizeof(object) -> int\n\ +\n\ +Return the size of object in bytes."); + +static PyObject * sys_getrefcount(PyObject *self, PyObject *arg) { return PyLong_FromSsize_t(arg->ob_refcnt); @@ -812,6 +845,7 @@ static PyMethodDef sys_methods[] = { {"getrefcount", (PyCFunction)sys_getrefcount, METH_O, getrefcount_doc}, {"getrecursionlimit", (PyCFunction)sys_getrecursionlimit, METH_NOARGS, getrecursionlimit_doc}, + {"getsizeof", sys_getsizeof, METH_O, getsizeof_doc}, {"_getframe", sys_getframe, METH_VARARGS, getframe_doc}, #ifdef MS_WINDOWS {"getwindowsversion", (PyCFunction)sys_getwindowsversion, METH_NOARGS, @@ -983,6 +1017,7 @@ getdlopenflags() -- returns flags to be used for dlopen() calls\n\ getprofile() -- get the global profiling function\n\ getrefcount() -- return the reference count for an object (plus one :-)\n\ getrecursionlimit() -- return the max recursion depth for the interpreter\n\ +getsizeof() -- return the size of an object in bytes\n\ gettrace() -- get the global debug tracing function\n\ setcheckinterval() -- control how often the interpreter checks for events\n\ setdlopenflags() -- set the flags to be used for dlopen() calls\n\ |