diff Objects/typeobject.c @ 76530:b044e0568be2

Account for shared keys in type's __sizeof__ (#13903).
author Martin v. Loewis <martin@v.loewis.de>
date Tue, 24 Apr 2012 19:13:57 +0200
parents 0c1c8f8955d8
children a3beae842f13
line wrap: on
line diff
--- a/Objects/typeobject.c	Tue Apr 24 18:39:53 2012 +0200
+++ b/Objects/typeobject.c	Tue Apr 24 19:13:57 2012 +0200
@@ -2730,6 +2730,22 @@
     return result;
 }
 
+static PyObject*
+type_sizeof(PyObject *self, PyObject *args_unused)
+{
+    Py_ssize_t size;
+    PyTypeObject *type = (PyTypeObject*)self;
+    if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
+        PyHeapTypeObject* et = (PyHeapTypeObject*)type;
+        size = sizeof(PyHeapTypeObject);
+        if (et->ht_cached_keys)
+            size += _PyDict_KeysSize(et->ht_cached_keys);
+    }
+    else
+        size = sizeof(PyTypeObject);
+    return PyLong_FromSsize_t(size);
+}
+
 static PyMethodDef type_methods[] = {
     {"mro", (PyCFunction)mro_external, METH_NOARGS,
      PyDoc_STR("mro() -> list\nreturn a type's method resolution order")},
@@ -2745,6 +2761,8 @@
      PyDoc_STR("__subclasscheck__() -> bool\ncheck if a class is a subclass")},
     {"__dir__", type_dir, METH_NOARGS,
      PyDoc_STR("__dir__() -> list\nspecialized __dir__ implementation for types")},
+    {"__sizeof__",      type_sizeof,       METH_NOARGS,
+     "__sizeof__() -> int\nreturn memory consumption of the type object"},
     {0}
 };