]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #25421: __sizeof__ methods of builtin types now use dynamic basic size.
authorSerhiy Storchaka <storchaka@gmail.com>
Sat, 19 Dec 2015 18:05:25 +0000 (20:05 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Sat, 19 Dec 2015 18:05:25 +0000 (20:05 +0200)
This allows sys.getsize() to work correctly with their subclasses with
__slots__ defined.

19 files changed:
Lib/test/test_sys.py
Misc/NEWS
Modules/_collectionsmodule.c
Modules/_decimal/_decimal.c
Modules/_elementtree.c
Modules/_io/bufferedio.c
Modules/_io/bytesio.c
Modules/_pickle.c
Modules/_struct.c
Modules/arraymodule.c
Modules/itertoolsmodule.c
Modules/mmapmodule.c
Modules/parsermodule.c
Objects/bytearrayobject.c
Objects/codeobject.c
Objects/dictobject.c
Objects/listobject.c
Objects/odictobject.c
Objects/setobject.c

index 2d9565328c20cd89480acd51bb7da35ac8b9a6fa..4f1577d65d85e3faf730a3509b32c11d7be16c30 100644 (file)
@@ -1112,6 +1112,36 @@ class SizeofTest(unittest.TestCase):
         # weakcallableproxy
         check(weakref.proxy(int), size('2Pn2P'))
 
+    def check_slots(self, obj, base, extra):
+        expected = sys.getsizeof(base) + struct.calcsize(extra)
+        if gc.is_tracked(obj) and not gc.is_tracked(base):
+            expected += struct.calcsize('2Pn')  # PyGC_Head
+        self.assertEqual(sys.getsizeof(obj), expected)
+
+    def test_slots(self):
+        # check all subclassable types defined in Objects/ that allow
+        # non-empty __slots__
+        check = self.check_slots
+        class BA(bytearray):
+            __slots__ = 'a', 'b', 'c'
+        check(BA(), bytearray(), '3P')
+        class D(dict):
+            __slots__ = 'a', 'b', 'c'
+        check(D(x=[]), {'x': []}, '3P')
+        class L(list):
+            __slots__ = 'a', 'b', 'c'
+        check(L(), [], '3P')
+        class S(set):
+            __slots__ = 'a', 'b', 'c'
+        check(S(), set(), '3P')
+        class FS(frozenset):
+            __slots__ = 'a', 'b', 'c'
+        check(FS(), frozenset(), '3P')
+        from collections import OrderedDict
+        class OD(OrderedDict):
+            __slots__ = 'a', 'b', 'c'
+        check(OD(x=[]), OrderedDict(x=[]), '3P')
+
     def test_pythontypes(self):
         # check all types defined in Python/
         size = test.support.calcobjsize
index 1f4d55cac484cc19a07ccc074cdfc7194110dc01..299ed3c9fcc2b54b6878c35d19d8b5a2aa9abe51 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,10 @@ Release date: tba
 Core and Builtins
 -----------------
 
+- Issue #25421: __sizeof__ methods of builtin types now use dynamic basic size.
+  This allows sys.getsize() to work correctly with their subclasses with
+  __slots__ defined.
+
 - Issue #25709: Fixed problem with in-place string concatenation and utf-8 cache.
 
 - Issue #24097: Fixed crash in object.__reduce__() if slot name is freed inside
index 214872b393f37d0d0a18972a9511b724fadb720b..4fffe01a84d84f90fb1bad5f3ad6fcf7316f14df 100644 (file)
@@ -1443,7 +1443,7 @@ deque_sizeof(dequeobject *deque, void *unused)
     Py_ssize_t res;
     Py_ssize_t blocks;
 
-    res = sizeof(dequeobject);
+    res = _PyObject_SIZE(Py_TYPE(deque));
     blocks = (deque->leftindex + Py_SIZE(deque) + BLOCKLEN - 1) / BLOCKLEN;
     assert(deque->leftindex + Py_SIZE(deque) - 1 ==
            (blocks - 1) * BLOCKLEN + deque->rightindex);
index 169914c2f749a76a96401270cc5b865feee5fcf8..112b44fda7b5846428e11ce54c35697869ad726d 100644 (file)
@@ -4529,7 +4529,7 @@ dec_sizeof(PyObject *v, PyObject *dummy UNUSED)
 {
     Py_ssize_t res;
 
-    res = sizeof(PyDecObject);
+    res = _PyObject_SIZE(Py_TYPE(v));
     if (mpd_isdynamic_data(MPD(v))) {
         res += MPD(v)->alloc * sizeof(mpd_uint_t);
     }
index 915a0be4d4a254b979f3beb1d6f3f0f83a92a409..c69998cd17c85ccd2d5ffdb7b02553c8292b8fa4 100644 (file)
@@ -847,7 +847,7 @@ static Py_ssize_t
 _elementtree_Element___sizeof___impl(ElementObject *self)
 /*[clinic end generated code: output=bf73867721008000 input=70f4b323d55a17c1]*/
 {
-    Py_ssize_t result = sizeof(ElementObject);
+    Py_ssize_t result = _PyObject_SIZE(Py_TYPE(self));
     if (self->extra) {
         result += sizeof(ElementObjectExtra);
         if (self->extra->children != self->extra->_children)
index 29e000bde4a6ff0d2561594b3aa5787e3e6955a5..6bb2200e20a4929b564803ce23e88eaf74aede9a 100644 (file)
@@ -423,7 +423,7 @@ buffered_sizeof(buffered *self, void *unused)
 {
     Py_ssize_t res;
 
-    res = sizeof(buffered);
+    res = _PyObject_SIZE(Py_TYPE(self));
     if (self->buffer)
         res += self->buffer_size;
     return PyLong_FromSsize_t(res);
index 31cc1f79a16dccaf81d147df9de5798017040854..eef3b3de72b31a0d09aa858d4a6e25e4fcd919f9 100644 (file)
@@ -991,7 +991,7 @@ bytesio_sizeof(bytesio *self, void *unused)
 {
     Py_ssize_t res;
 
-    res = sizeof(bytesio);
+    res = _PyObject_SIZE(Py_TYPE(self));
     if (self->buf && !SHARED_BUF(self))
         res += _PySys_GetSizeOf(self->buf);
     return PyLong_FromSsize_t(res);
index 41f1cd1975748b318aa55d59487e9568f6181d1a..56bbd587b596d142740ab7acfcce64a02a1ad1a6 100644 (file)
@@ -4015,7 +4015,7 @@ _pickle_Pickler___sizeof___impl(PicklerObject *self)
 {
     Py_ssize_t res, s;
 
-    res = sizeof(PicklerObject);
+    res = _PyObject_SIZE(Py_TYPE(self));
     if (self->memo != NULL) {
         res += sizeof(PyMemoTable);
         res += self->memo->mt_allocated * sizeof(PyMemoEntry);
@@ -6418,7 +6418,7 @@ _pickle_Unpickler___sizeof___impl(UnpicklerObject *self)
 {
     Py_ssize_t res;
 
-    res = sizeof(UnpicklerObject);
+    res = _PyObject_SIZE(Py_TYPE(self));
     if (self->memo != NULL)
         res += self->memo_size * sizeof(PyObject *);
     if (self->marks != NULL)
index 068c5d1e1d83853ce7b23781c53304c047158954..b61f9f6fa9cafad942a2b3508744f968a75dc83e 100644 (file)
@@ -1924,7 +1924,7 @@ s_sizeof(PyStructObject *self, void *unused)
     Py_ssize_t size;
     formatcode *code;
 
-    size = sizeof(PyStructObject) + sizeof(formatcode);
+    size = _PyObject_SIZE(Py_TYPE(self)) + sizeof(formatcode);
     for (code = self->s_codes; code->fmtdef != NULL; code++)
         size += sizeof(formatcode);
     return PyLong_FromSsize_t(size);
index a3ccf9344ff3f507fcfe069b667cd0af3e182ef5..6af75a4357a356fa1d7fc44930d9998d45dba7c9 100644 (file)
@@ -1743,7 +1743,7 @@ array_array___sizeof___impl(arrayobject *self)
 /*[clinic end generated code: output=d8e1c61ebbe3eaed input=805586565bf2b3c6]*/
 {
     Py_ssize_t res;
-    res = sizeof(arrayobject) + self->allocated * self->ob_descr->itemsize;
+    res = _PyObject_SIZE(Py_TYPE(self)) + self->allocated * self->ob_descr->itemsize;
     return PyLong_FromSsize_t(res);
 }
 
index dfafeab5203153e00bb6a7f8cb69481b7e312f8e..9c210ea8719bb2408084a4ea06a950f39fc625ad 100644 (file)
@@ -2089,7 +2089,7 @@ product_sizeof(productobject *lz, void *unused)
 {
     Py_ssize_t res;
 
-    res = sizeof(productobject);
+    res = _PyObject_SIZE(Py_TYPE(lz));
     res += PyTuple_GET_SIZE(lz->pools) * sizeof(Py_ssize_t);
     return PyLong_FromSsize_t(res);
 }
@@ -2420,7 +2420,7 @@ combinations_sizeof(combinationsobject *co, void *unused)
 {
     Py_ssize_t res;
 
-    res = sizeof(combinationsobject);
+    res = _PyObject_SIZE(Py_TYPE(co));
     res += co->r * sizeof(Py_ssize_t);
     return PyLong_FromSsize_t(res);
 }
@@ -2761,7 +2761,7 @@ cwr_sizeof(cwrobject *co, void *unused)
 {
     Py_ssize_t res;
 
-    res = sizeof(cwrobject);
+    res = _PyObject_SIZE(Py_TYPE(co));
     res += co->r * sizeof(Py_ssize_t);
     return PyLong_FromSsize_t(res);
 }
@@ -3110,7 +3110,7 @@ permutations_sizeof(permutationsobject *po, void *unused)
 {
     Py_ssize_t res;
 
-    res = sizeof(permutationsobject);
+    res = _PyObject_SIZE(Py_TYPE(po));
     res += PyTuple_GET_SIZE(po->pool) * sizeof(Py_ssize_t);
     res += po->r * sizeof(Py_ssize_t);
     return PyLong_FromSsize_t(res);
index daab52b3880b23c879583119aa41af0f810c0c80..bb98a9942757b2fab3adb1982d4be82ffbcc1303 100644 (file)
@@ -719,7 +719,7 @@ mmap__sizeof__method(mmap_object *self, void *unused)
 {
     Py_ssize_t res;
 
-    res = sizeof(mmap_object);
+    res = _PyObject_SIZE(Py_TYPE(self));
     if (self->tagname)
         res += strlen(self->tagname) + 1;
     return PyLong_FromSsize_t(res);
index 8023558fd15181b4ebc9755ea4b42cad9ea41abc..6471b8ee997fcf8ea1f6d5aed201edf82b823985 100644 (file)
@@ -397,7 +397,7 @@ parser_sizeof(PyST_Object *st, void *unused)
 {
     Py_ssize_t res;
 
-    res = sizeof(PyST_Object) + _PyNode_SizeOf(st->st_node);
+    res = _PyObject_SIZE(Py_TYPE(st)) + _PyNode_SizeOf(st->st_node);
     return PyLong_FromSsize_t(res);
 }
 
index 5647b57a52fa58ea23c2f9484e449466ec04be8a..c59ad2499adb189e2df76d81138e6d702d42af01 100644 (file)
@@ -2974,7 +2974,7 @@ bytearray_sizeof_impl(PyByteArrayObject *self)
 {
     Py_ssize_t res;
 
-    res = sizeof(PyByteArrayObject) + self->ob_alloc * sizeof(char);
+    res = _PyObject_SIZE(Py_TYPE(self)) + self->ob_alloc * sizeof(char);
     return PyLong_FromSsize_t(res);
 }
 
index 353f414a38486b8ab319d8fdd07934ba5b4b54d5..b0e34468a7e7aa1fec1584d9e510fe459eae3583 100644 (file)
@@ -384,7 +384,7 @@ code_sizeof(PyCodeObject *co, void *unused)
 {
     Py_ssize_t res;
 
-    res = sizeof(PyCodeObject);
+    res = _PyObject_SIZE(Py_TYPE(co));
     if (co->co_cell2arg != NULL && co->co_cellvars != NULL)
         res += PyTuple_GET_SIZE(co->co_cellvars) * sizeof(unsigned char);
     return PyLong_FromSsize_t(res);
index 624ae9b88858adcb5139427dbe78c1f3c677a169..3e6e112ba0431529452a694111ed9327a756bc9a 100644 (file)
@@ -2560,7 +2560,7 @@ _PyDict_SizeOf(PyDictObject *mp)
     Py_ssize_t size, res;
 
     size = DK_SIZE(mp->ma_keys);
-    res = sizeof(PyDictObject);
+    res = _PyObject_SIZE(Py_TYPE(mp));
     if (mp->ma_values)
         res += size * sizeof(PyObject*);
     /* If the dictionary is split, the keys portion is accounted-for
index 45e54ce31d201f0cefc9700d55a5ce31e513b6c2..eee7c68e9e4562c8da0c63efc80b67fc70ef261f 100644 (file)
@@ -2324,7 +2324,7 @@ list_sizeof(PyListObject *self)
 {
     Py_ssize_t res;
 
-    res = sizeof(PyListObject) + self->allocated * sizeof(void*);
+    res = _PyObject_SIZE(Py_TYPE(self)) + self->allocated * sizeof(void*);
     return PyLong_FromSsize_t(res);
 }
 
index 5ad88bcacc5cb5958ac4e99874bdf6c46e059fe2..4e51f4d1475011d638404ccd0ef6cddfa61a0c22 100644 (file)
@@ -951,8 +951,6 @@ odict_sizeof(PyODictObject *od)
     if (res == -1 && PyErr_Occurred())
         return NULL;
 
-    res += sizeof(PyODictObject) - sizeof(PyDictObject);
-
     /* instance dict */
     pylong = _PyDict_SizeOf((PyDictObject *)od->od_inst_dict);
     if (pylong == NULL)
index 704d7e2b2a76ec2db079eb275592a76f69886937..2faaf12954bf65a0584cd989646ab1c76e3dfda5 100644 (file)
@@ -1940,7 +1940,7 @@ set_sizeof(PySetObject *so)
 {
     Py_ssize_t res;
 
-    res = sizeof(PySetObject);
+    res = _PyObject_SIZE(Py_TYPE(so));
     if (so->table != so->smalltable)
         res = res + (so->mask + 1) * sizeof(setentry);
     return PyLong_FromSsize_t(res);