From 00709aaa3dc7da374e94a3ede8e05603e8a95066 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Martin=20v=2E=20L=C3=B6wis?= <martin@v.loewis.de>
Date: Wed, 4 Jun 2008 14:18:43 +0000
Subject: [PATCH] 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.
........
---
 Doc/library/sys.rst       |  10 +++
 Lib/test/test_descrtut.py |   1 +
 Lib/test/test_sys.py      | 137 +++++++++++++++++++++++++++++++++++++-
 Objects/bytesobject.c     |  13 ++++
 Objects/dictobject.c      |  19 ++++++
 Objects/listobject.c      |  12 ++++
 Objects/longobject.c      |  13 ++++
 Objects/typeobject.c      |  16 +++++
 Python/sysmodule.c        |  35 ++++++++++
 9 files changed, 254 insertions(+), 2 deletions(-)

diff --git a/Doc/library/sys.rst b/Doc/library/sys.rst
index b35460372fe..b69de3f7f1c 100644
--- a/Doc/library/sys.rst
+++ b/Doc/library/sys.rst
@@ -331,6 +331,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
diff --git a/Lib/test/test_descrtut.py b/Lib/test/test_descrtut.py
index bd298bdd8cb..11a3422a40f 100644
--- a/Lib/test/test_descrtut.py
+++ b/Lib/test/test_descrtut.py
@@ -195,6 +195,7 @@ You can get the information from the list type:
      '__rmul__',
      '__setattr__',
      '__setitem__',
+     '__sizeof__',
      '__str__',
      '__subclasshook__',
      'append',
diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py
index e4821fc2bbe..493ef8d5f45 100644
--- a/Lib/test/test_sys.py
+++ b/Lib/test/test_sys.py
@@ -1,6 +1,6 @@
 # -*- coding: iso-8859-1 -*-
 import unittest, test.support
-import sys, io
+import sys, io, os
 
 class SysModuleTest(unittest.TestCase):
 
@@ -369,8 +369,141 @@ class SysModuleTest(unittest.TestCase):
         self.assertEqual(out, b'?')
 
 
+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.support.TESTFN, 'wb')
+
+    def tearDown(self):
+        self.file.close()
+        test.support.unlink(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.assertEqual(self.align(0) % self.p, 0)
+        self.assertEqual(self.align(1) % self.p, 0)
+        self.assertEqual(self.align(3) % self.p, 0)
+        self.assertEqual(self.align(4) % self.p, 0)
+        self.assertEqual(self.align(7) % self.p, 0)
+        self.assertEqual(self.align(8) % self.p, 0)
+        self.assertEqual(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 + 2*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().__closure__[0], h + p)
+        # code XXX wrong size
+        # self.check_sizeof(get_cell().__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 XXX wrong size
+        #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 + 11 * p)
+        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)
+        # builtin_function_or_method
+        self.check_sizeof(abs, h + 3*p)
+        # module
+        self.check_sizeof(unittest, h + p)
+        # range
+        self.check_sizeof(range(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)
+        # XXX wrong size
+        # 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(0, h + self.align(2))
+        self.check_sizeof(1, h + self.align(2))
+        self.check_sizeof(-1, h + self.align(2))
+        self.check_sizeof(32768, h + self.align(2) + 2)
+        self.check_sizeof(32768*32768-1, h + self.align(2) + 2)
+        self.check_sizeof(32768*32768, h + self.align(2) + 4)
+        # XXX add Unicode support
+        # self.check_sizeof('', h + l + self.align(i + 1))
+        # self.check_sizeof('abc', h + l + self.align(i + 1) + 3)
+
+
 def test_main():
-    test.support.run_unittest(SysModuleTest)
+    test.support.run_unittest(SysModuleTest, SizeofTest)
 
 if __name__ == "__main__":
     test_main()
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
index 3f22e7c772e..ab6207be94c 100644
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -2784,6 +2784,17 @@ string_fromhex(PyObject *cls, PyObject *args)
 	return NULL;
 }
 
+PyDoc_STRVAR(sizeof__doc__,
+"S.__sizeof__() -> size of S in memory, in bytes");
+
+static PyObject *
+string_sizeof(PyBytesObject *v)
+{
+	Py_ssize_t res;
+	res = sizeof(PyBytesObject) + Py_SIZE(v) * Py_TYPE(v)->tp_itemsize;
+	return PyLong_FromSsize_t(res);
+}
+
 
 static PyObject *
 string_getnewargs(PyBytesObject *v)
@@ -2848,6 +2859,8 @@ string_methods[] = {
 	 translate__doc__},
 	{"upper", (PyCFunction)stringlib_upper, METH_NOARGS, _Py_upper__doc__},
 	{"zfill", (PyCFunction)stringlib_zfill, METH_VARARGS, zfill__doc__},
+	{"__sizeof__", (PyCFunction)string_sizeof, METH_NOARGS,
+	 sizeof__doc__},
 	{NULL,     NULL}		     /* sentinel */
 };
 
diff --git a/Objects/dictobject.c b/Objects/dictobject.c
index fc2c8f0fe37..b4f9182073e 100644
--- a/Objects/dictobject.c
+++ b/Objects/dictobject.c
@@ -1840,11 +1840,28 @@ dict_tp_clear(PyObject *op)
 
 static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
 
+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 PyLong_FromSsize_t(res);
+}
+
+PyDoc_STRVAR(has_key__doc__,
+"D.has_key(k) -> True if D has a key k, else False");
+
 PyDoc_STRVAR(contains__doc__,
 "D.__contains__(k) -> True if D has a key k, else False");
 
 PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
 
+PyDoc_STRVAR(sizeof__doc__,
+"D.__sizeof__() -> size of D in memory, in bytes");
+
 PyDoc_STRVAR(get__doc__,
 "D.get(k[,d]) -> D[k] if k in D, else d.  d defaults to None.");
 
@@ -1890,6 +1907,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__},
 	{"get",         (PyCFunction)dict_get,          METH_VARARGS,
 	 get__doc__},
 	{"setdefault",  (PyCFunction)dict_setdefault,   METH_VARARGS,
diff --git a/Objects/listobject.c b/Objects/listobject.c
index c84cce5cc80..d5f3a19dd47 100644
--- a/Objects/listobject.c
+++ b/Objects/listobject.c
@@ -2247,6 +2247,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 PyLong_FromSsize_t(res);
+}
+
 static PyObject *list_iter(PyObject *seq);
 static PyObject *list_reversed(PyListObject* seq, PyObject* unused);
 
@@ -2254,6 +2263,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 memory, in bytes");
 PyDoc_STRVAR(append_doc,
 "L.append(object) -- append object to end");
 PyDoc_STRVAR(extend_doc,
@@ -2279,6 +2290,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},
diff --git a/Objects/longobject.c b/Objects/longobject.c
index 10d03ae0b06..d0d4d3aae41 100644
--- a/Objects/longobject.c
+++ b/Objects/longobject.c
@@ -3625,6 +3625,17 @@ long_round(PyObject *self, PyObject *args)
 #undef UNDEF_NDIGITS
 }
 
+static PyObject *
+long_sizeof(PyLongObject *v)
+{
+	Py_ssize_t res;
+
+	res = sizeof(PyLongObject) + abs(Py_SIZE(v)) * sizeof(digit);
+        if (Py_SIZE(v) != 0)
+		res -=  sizeof(digit);
+	return PyLong_FromSsize_t(res);
+}
+
 #if 0
 static PyObject *
 long_is_finite(PyObject *v)
@@ -3651,6 +3662,8 @@ static PyMethodDef long_methods[] = {
 	 "Rounding with an ndigits arguments defers to float.__round__."},
 	{"__getnewargs__",	(PyCFunction)long_getnewargs,	METH_NOARGS},
         {"__format__", (PyCFunction)long__format__, METH_VARARGS},
+	{"__sizeof__",	(PyCFunction)long_sizeof, METH_NOARGS,
+	 "Returns size in memory, in bytes"},
 	{NULL,		NULL}		/* sentinel */
 };
 
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index 73d501c3e52..6e4ac7add02 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -3275,6 +3275,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 = Py_SIZE(self->ob_type) * isize;
+	res += self->ob_type->tp_basicsize;
+
+	return PyLong_FromSsize_t(res);	 
+}
+
 static PyMethodDef object_methods[] = {
 	{"__reduce_ex__", object_reduce_ex, METH_VARARGS,
 	 PyDoc_STR("helper for pickle")},
@@ -3284,6 +3298,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 memory, in bytes")},
 	{0}
 };
 
diff --git a/Python/sysmodule.c b/Python/sysmodule.c
index dee49651ab8..de2e91dbd1c 100644
--- a/Python/sysmodule.c
+++ b/Python/sysmodule.c
@@ -609,6 +609,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)
 {
@@ -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\
-- 
2.30.9