diff options
author | Robert Schuppenies <okkotonushi@googlemail.com> | 2008-07-14 10:13:31 (GMT) |
---|---|---|
committer | Robert Schuppenies <okkotonushi@googlemail.com> | 2008-07-14 10:13:31 (GMT) |
commit | fbe94c55ca482bc30a831f8319bdc2074124a4e3 (patch) | |
tree | ef806672dc53507d7529838ad8250feee9b9d88f /Python/sysmodule.c | |
parent | 3065b87a075656d52bb018821c7ba30cea26ec7a (diff) | |
download | cpython-fbe94c55ca482bc30a831f8319bdc2074124a4e3.zip cpython-fbe94c55ca482bc30a831f8319bdc2074124a4e3.tar.gz cpython-fbe94c55ca482bc30a831f8319bdc2074124a4e3.tar.bz2 |
Merged revisions 64842,64853,64856,64945 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r64842 | robert.schuppenies | 2008-07-10 15:43:26 +0200 (Thu, 10 Jul 2008) | 2 lines
Fixed Issue3122 and extended sys.getsizeof tests for built-in types.
........
r64853 | robert.schuppenies | 2008-07-10 17:24:04 +0200 (Thu, 10 Jul 2008) | 3 lines
Added additional __sizeof__ implementations and addressed comments made in
Issue3122.
........
r64856 | robert.schuppenies | 2008-07-10 19:13:55 +0200 (Thu, 10 Jul 2008) | 3 lines
Added garbage collector overhead and optional default return value to
sys.getsizeof.
........
r64945 | robert.schuppenies | 2008-07-14 10:42:18 +0200 (Mon, 14 Jul 2008) | 2 lines
Fixed test failure on Win64 machines.
........
Diffstat (limited to 'Python/sysmodule.c')
-rw-r--r-- | Python/sysmodule.c | 63 |
1 files changed, 47 insertions, 16 deletions
diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 2a98eb5..e45d2a3 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -610,9 +610,16 @@ sys_mdebug(PyObject *self, PyObject *args) #endif /* USE_MALLOPT */ static PyObject * -sys_getsizeof(PyObject *self, PyObject *args) +sys_getsizeof(PyObject *self, PyObject *args, PyObject *kwds) { - static PyObject * str__sizeof__ = NULL; + PyObject *res = NULL; + static PyObject *str__sizeof__, *gc_head_size = NULL; + static char *kwlist[] = {"object", "default", 0}; + PyObject *o, *dflt = NULL; + + if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:getsizeof", + kwlist, &o, &dflt)) + return NULL; /* Initialize static variable needed by _PyType_Lookup */ if (str__sizeof__ == NULL) { @@ -621,24 +628,47 @@ sys_getsizeof(PyObject *self, PyObject *args) 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); + /* Initialize static variable for GC head size */ + if (gc_head_size == NULL) { + gc_head_size = PyLong_FromSsize_t(sizeof(PyGC_Head)); + if (gc_head_size == NULL) return NULL; - } - return PyObject_CallFunctionObjArgs(method, args, NULL); - } + } + + /* Make sure the type is initialized. float gets initialized late */ + if (PyType_Ready(Py_TYPE(o)) < 0) + return NULL; + + PyObject *method = _PyType_Lookup(Py_TYPE(o), str__sizeof__); + if (method == NULL) + PyErr_Format(PyExc_TypeError, + "Type %.100s doesn't define __sizeof__", + Py_TYPE(o)->tp_name); else - return PyObject_CallMethod(args, "__sizeof__", NULL); + res = PyObject_CallFunctionObjArgs(method, o, NULL); + + /* Has a default value been given */ + if ((res == NULL) && (dflt != NULL) && + PyErr_ExceptionMatches(PyExc_TypeError)) + { + PyErr_Clear(); + Py_INCREF(dflt); + return dflt; + } + else if (res == NULL) + return res; + + /* add gc_head size */ + if (PyObject_IS_GC(o)) { + PyObject *tmp = res; + res = PyNumber_Add(tmp, gc_head_size); + Py_DECREF(tmp); + } + return res; } PyDoc_STRVAR(getsizeof_doc, -"getsizeof(object) -> int\n\ +"getsizeof(object, default) -> int\n\ \n\ Return the size of object in bytes."); @@ -845,7 +875,8 @@ 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}, + {"getsizeof", (PyCFunction)sys_getsizeof, + METH_VARARGS | METH_KEYWORDS, getsizeof_doc}, {"_getframe", sys_getframe, METH_VARARGS, getframe_doc}, #ifdef MS_WINDOWS {"getwindowsversion", (PyCFunction)sys_getwindowsversion, METH_NOARGS, |