]> granicus.if.org Git - python/commitdiff
Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
authorRobert Schuppenies <okkotonushi@googlemail.com>
Sun, 1 Jun 2008 16:16:17 +0000 (16:16 +0000)
committerRobert Schuppenies <okkotonushi@googlemail.com>
Sun, 1 Jun 2008 16:16:17 +0000 (16:16 +0000)
Doc/library/sys.rst
Lib/test/test_sys.py
Misc/NEWS
Objects/bytesobject.c
Objects/dictobject.c
Objects/listobject.c
Objects/longobject.c
Objects/typeobject.c
Python/sysmodule.c

index 390f73df650f54af9fc6b5bbe041315342446783..2e396333dd3d6f6e0b7cf97cca87c023dfddc080 100644 (file)
@@ -409,6 +409,16 @@ always available.
    :func:`setrecursionlimit`.
 
 
+.. function:: getsizeof(object)
+
+   Return the size of an object in bytes. The object can be any type of
+   object. All built-in objects will return correct results, but this
+   does not have to hold true for third-party extensions as it is implementation 
+   specific.
+
+   .. versionadded:: 2.6
+
+
 .. function:: _getframe([depth])
 
    Return a frame object from the call stack.  If optional integer *depth* is
index a4d8a72c61a437d77afa4f59dd6b8be138f8c1b6..32ab90e811092033b46e665a32a95554bb39befc 100644 (file)
@@ -1,6 +1,6 @@
 # -*- coding: iso-8859-1 -*-
 import unittest, test.test_support
-import sys, cStringIO
+import sys, cStringIO, os
 
 class SysModuleTest(unittest.TestCase):
 
@@ -405,8 +405,155 @@ class SysModuleTest(unittest.TestCase):
         self.assertEqual(out, '?')
 
 
+class SizeofTest(unittest.TestCase):
+
+    def setUp(self):
+        import struct
+        self.i = len(struct.pack('i', 0))
+        self.l = len(struct.pack('l', 0))
+        self.p = len(struct.pack('P', 0))
+        self.headersize = self.l + self.p
+        if hasattr(sys, "gettotalrefcount"):
+            self.headersize += 2 * self.p
+        self.file = open(test.test_support.TESTFN, 'wb')
+
+    def tearDown(self):
+        self.file.close()
+        os.remove(test.test_support.TESTFN)
+
+    def check_sizeof(self, o, size):
+        result = sys.getsizeof(o)
+        msg = 'wrong size for %s: got %d, expected %d' \
+            % (type(o), result, size)
+        self.assertEqual(result, size, msg)
+
+    def align(self, value):
+        mod = value % self.p
+        if mod != 0:
+            return value - mod + self.p
+        else:
+            return value
+
+    def test_align(self):
+        self.assertTrue( (self.align(0) % self.p) == 0 )
+        self.assertTrue( (self.align(1) % self.p) == 0 )
+        self.assertTrue( (self.align(3) % self.p) == 0 )
+        self.assertTrue( (self.align(4) % self.p) == 0 )
+        self.assertTrue( (self.align(7) % self.p) == 0 )
+        self.assertTrue( (self.align(8) % self.p) == 0 )
+        self.assertTrue( (self.align(9) % self.p) == 0 )
+
+    def test_standardtypes(self):
+        i = self.i
+        l = self.l
+        p = self.p
+        h = self.headersize
+        # bool
+        self.check_sizeof(True, h + l)
+        # buffer
+        self.check_sizeof(buffer(''), h + 2*p + 2*l + self.align(i) +l)
+        # bytearray
+        self.check_sizeof(bytes(), h + self.align(i) + l + p)
+        # cell
+        def get_cell():
+            x = 42
+            def inner():
+                return x
+            return inner
+        self.check_sizeof(get_cell().func_closure[0], h + p)
+        # old-style class
+        class class_oldstyle():
+            def method():
+                pass
+        self.check_sizeof(class_oldstyle, h + 6*p)
+        # instance
+        self.check_sizeof(class_oldstyle(), h + 3*p)
+        # method
+        self.check_sizeof(class_oldstyle().method, h + 4*p)
+        # code
+        self.check_sizeof(get_cell().func_code, h + self.align(4*i) + 8*p +\
+                            self.align(i) + 2*p)
+        # complex
+        self.check_sizeof(complex(0,1), h + 2*8)
+        # enumerate
+        self.check_sizeof(enumerate([]), h + l + 3*p)
+        # reverse
+        self.check_sizeof(reversed(''), h + l + p )
+        # file
+        self.check_sizeof(self.file, h + 4*p + self.align(2*i) + 4*p +\
+                            self.align(3*i) + 3*p + self.align(i))
+        # float
+        self.check_sizeof(float(0), h + 8)
+        # function
+        def func(): pass
+        self.check_sizeof(func, h + 9 * l)
+        class c():
+            @staticmethod
+            def foo():
+                pass
+            @classmethod
+            def bar(cls):
+                pass
+            # staticmethod
+            self.check_sizeof(foo, h + l)
+            # classmethod
+            self.check_sizeof(bar, h + l)
+        # generator
+        def get_gen(): yield 1
+        self.check_sizeof(get_gen(), h + p + self.align(i) + 2*p)
+        # integer
+        self.check_sizeof(1, h + l)
+        # builtin_function_or_method
+        self.check_sizeof(abs, h + 3*p)
+        # module
+        self.check_sizeof(unittest, h + p)
+        # xange
+        self.check_sizeof(xrange(1), h + 3*p)
+        # slice
+        self.check_sizeof(slice(0), h + 3*p)
+
+        h += l
+        # new-style class
+        class class_newstyle(object):
+            def method():
+                pass
+        # type (PyTypeObject + PyNumberMethods +  PyMappingMethods +
+        #       PySequenceMethods +  PyBufferProcs)
+        len_typeobject = p + 2*l + 15*p + l + 4*p + l + 9*p + l + 11*p
+        self.check_sizeof(class_newstyle, h + \
+                              len_typeobject + 42*p + 10*p + 3*p + 6*p)
+
+
+    def test_specialtypes(self):
+        i = self.i
+        l = self.l
+        p = self.p
+        h = self.headersize
+        # dict
+        self.check_sizeof({}, h + 3*l + 3*p + 8*(l + 2*p))
+        longdict = {1:1, 2:2, 3:3, 4:4, 5:5, 6:6, 7:7, 8:8}
+        self.check_sizeof(longdict, h + 3*l + 3*p + 8*(l + 2*p) + 16*(l + 2*p))
+        # list
+        self.check_sizeof([], h + l + p + l)
+        self.check_sizeof([1, 2, 3], h + l + p + l + 3*l)
+
+        h += l
+        # long
+        self.check_sizeof(0L, h + self.align(2))
+        self.check_sizeof(1L, h + self.align(2))
+        self.check_sizeof(-1L, h + self.align(2))
+        self.check_sizeof(32768L, h + self.align(2) + 2)
+        self.check_sizeof(32768L*32768L-1, h + self.align(2) + 2)
+        self.check_sizeof(32768L*32768L, h + self.align(2) + 4)
+        # string
+        self.check_sizeof('', h + l + self.align(i + 1))
+        self.check_sizeof('abc', h + l + self.align(i + 1) + 3)
+
+
 def test_main():
-    test.test_support.run_unittest(SysModuleTest)
+    test_classes = (SysModuleTest, SizeofTest)
+
+    test.test_support.run_unittest(*test_classes)
 
 if __name__ == "__main__":
     test_main()
index 790e980c74aa050760883e7af2c33d8cddb7ee70..155ade0a8dd8855abfb5e2e14148df9502d9c820 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,8 @@ What's New in Python 2.6 beta 1?
 Core and Builtins
 -----------------
 
+- Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
+
 - New environment variable PYTHONIOENCODING.
 
 - Patch #2488: Add sys.maxsize.
index 79c1e4f0d4e8b04a93878024e25ca288727a8c3d..0de24f84d94a98b0935f317807eb605172f3c32b 100644 (file)
@@ -3917,6 +3917,17 @@ string_splitlines(PyBytesObject *self, PyObject *args)
     return NULL;
 }
 
+PyDoc_STRVAR(sizeof__doc__,
+"S.__sizeof__() -> size of S in bytes");
+
+static PyObject *
+string_sizeof(PyBytesObject *v)
+{
+       Py_ssize_t res;
+       res = sizeof(PyBytesObject) + v->ob_size * v->ob_type->tp_itemsize;
+       return PyInt_FromSsize_t(res);
+}
+
 #undef SPLIT_APPEND
 #undef SPLIT_ADD
 #undef MAX_PREALLOC
@@ -4024,6 +4035,8 @@ string_methods[] = {
         expandtabs__doc__},
        {"splitlines", (PyCFunction)string_splitlines, METH_VARARGS,
         splitlines__doc__},
+       {"__sizeof__", (PyCFunction)string_sizeof, METH_NOARGS,
+        sizeof__doc__},
        {"__getnewargs__",      (PyCFunction)string_getnewargs, METH_NOARGS},
        {NULL,     NULL}                     /* sentinel */
 };
index cdf0dfaaaf4444861c8217fd156f80d16880f871..d3a7cb9bc45ce88b7ded621220240822fc32e060 100644 (file)
@@ -2032,6 +2032,16 @@ dict_iteritems(PyDictObject *dict)
        return dictiter_new(dict, &PyDictIterItem_Type);
 }
 
+static PyObject *
+dict_sizeof(PyDictObject *mp)
+{
+       Py_ssize_t res;
+
+       res = sizeof(PyDictObject) + sizeof(mp->ma_table);
+       if (mp->ma_table != mp->ma_smalltable)
+               res = res + (mp->ma_mask + 1) * sizeof(PyDictEntry);
+       return PyInt_FromSsize_t(res);
+}
 
 PyDoc_STRVAR(has_key__doc__,
 "D.has_key(k) -> True if D has a key k, else False");
@@ -2041,6 +2051,9 @@ PyDoc_STRVAR(contains__doc__,
 
 PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
 
+PyDoc_STRVAR(sizeof__doc__,
+"D.__sizeof__() -> size of D in bytes");
+
 PyDoc_STRVAR(get__doc__,
 "D.get(k[,d]) -> D[k] if k in D, else d.  d defaults to None.");
 
@@ -2092,6 +2105,8 @@ static PyMethodDef mapp_methods[] = {
         contains__doc__},
        {"__getitem__", (PyCFunction)dict_subscript,    METH_O | METH_COEXIST,
         getitem__doc__},
+       {"__sizeof__",  (PyCFunction)dict_sizeof,       METH_NOARGS,
+        sizeof__doc__},
        {"has_key",     (PyCFunction)dict_has_key,      METH_O,
         has_key__doc__},
        {"get",         (PyCFunction)dict_get,          METH_VARARGS,
index 9d742d8f825de90b6ccaadbb9f31b6d2edeb2cc9..0216a855aac2a268c94d24256e549f309ab055f7 100644 (file)
@@ -2420,6 +2420,15 @@ list_init(PyListObject *self, PyObject *args, PyObject *kw)
        return 0;
 }
 
+static PyObject *
+list_sizeof(PyListObject *self)
+{
+       Py_ssize_t res;
+
+       res = sizeof(PyListObject) + self->allocated * sizeof(void*);
+       return PyInt_FromSsize_t(res);
+}
+
 static PyObject *list_iter(PyObject *seq);
 static PyObject *list_reversed(PyListObject* seq, PyObject* unused);
 
@@ -2427,6 +2436,8 @@ PyDoc_STRVAR(getitem_doc,
 "x.__getitem__(y) <==> x[y]");
 PyDoc_STRVAR(reversed_doc,
 "L.__reversed__() -- return a reverse iterator over the list");
+PyDoc_STRVAR(sizeof_doc,
+"L.__sizeof__() -- size of L in bytes");
 PyDoc_STRVAR(append_doc,
 "L.append(object) -- append object to end");
 PyDoc_STRVAR(extend_doc,
@@ -2452,6 +2463,7 @@ static PyObject *list_subscript(PyListObject*, PyObject*);
 static PyMethodDef list_methods[] = {
        {"__getitem__", (PyCFunction)list_subscript, METH_O|METH_COEXIST, getitem_doc},
        {"__reversed__",(PyCFunction)list_reversed, METH_NOARGS, reversed_doc},
+       {"__sizeof__",  (PyCFunction)list_sizeof, METH_NOARGS, sizeof_doc},
        {"append",      (PyCFunction)listappend,  METH_O, append_doc},
        {"insert",      (PyCFunction)listinsert,  METH_VARARGS, insert_doc},
        {"extend",      (PyCFunction)listextend,  METH_O, extend_doc},
index 82a57ec29abce5d8284fd2b7ee13472b0b365ba8..5876495bc6e8951baae230fac2ba678d95fd16de 100644 (file)
@@ -3436,6 +3436,17 @@ long__format__(PyObject *self, PyObject *args)
        return NULL;
 }
 
+static PyObject *
+long_sizeof(PyLongObject *v)
+{
+       Py_ssize_t res;
+
+       res = sizeof(PyLongObject) + abs(v->ob_size) * sizeof(digit);
+        if (v->ob_size != 0)
+               res -=  sizeof(digit);
+       return PyInt_FromSsize_t(res);
+}
+
 #if 0
 static PyObject *
 long_is_finite(PyObject *v)
@@ -3455,6 +3466,8 @@ static PyMethodDef long_methods[] = {
          "Truncating an Integral returns itself."},
        {"__getnewargs__",      (PyCFunction)long_getnewargs,   METH_NOARGS},
         {"__format__", (PyCFunction)long__format__, METH_VARARGS},
+       {"__sizeof__",  (PyCFunction)long_sizeof, METH_NOARGS,
+        "Returns size in bytes"},
        {NULL,          NULL}           /* sentinel */
 };
 
index 151ea69f4f9dc30e1a9efe5d3e9ebc2be91526eb..5405fec607b995ad90a2d6045c2301f2ba34106c 100644 (file)
@@ -3397,6 +3397,20 @@ object_format(PyObject *self, PyObject *args)
         return result;
 }
 
+static PyObject *
+object_sizeof(PyObject *self, PyObject *args)
+{
+       Py_ssize_t res, isize;
+
+       res = 0;
+       isize = self->ob_type->tp_itemsize;
+       if (isize > 0)
+               res = self->ob_type->ob_size * isize;
+       res += self->ob_type->tp_basicsize;
+
+       return PyInt_FromSsize_t(res);   
+}
+
 static PyMethodDef object_methods[] = {
        {"__reduce_ex__", object_reduce_ex, METH_VARARGS,
         PyDoc_STR("helper for pickle")},
@@ -3406,6 +3420,8 @@ static PyMethodDef object_methods[] = {
         object_subclasshook_doc},
         {"__format__", object_format, METH_VARARGS,
          PyDoc_STR("default object formatter")},
+        {"__sizeof__", object_sizeof, METH_NOARGS,
+         PyDoc_STR("__sizeof__() -> size of object in bytes")},
        {0}
 };
 
index e4fcc506d6cb3ce2bad11e0e75bd537b6eab6b00..54d0ddd1265de0fa1ff2d14d849c7eae294a9158 100644 (file)
@@ -639,6 +639,45 @@ 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__ = PyString_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);
+       } 
+       /* Instance of old-style classes */
+       else if(PyInstance_Check(args))
+               return PyInt_FromSsize_t(PyInstance_Type.tp_basicsize);
+       /* Old-style class */
+       else if (PyClass_Check(args))
+               return PyInt_FromSsize_t(PyClass_Type.tp_basicsize);
+       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)
 {
@@ -850,6 +889,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,
@@ -1031,6 +1071,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\