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

13 files changed:
Lib/test/test_sys.py
Misc/NEWS
Modules/_collectionsmodule.c
Modules/_io/bufferedio.c
Modules/_io/bytesio.c
Modules/_struct.c
Modules/arraymodule.c
Modules/mmapmodule.c
Modules/parsermodule.c
Objects/bytearrayobject.c
Objects/dictobject.c
Objects/listobject.c
Objects/setobject.c

index de13f2204b528b6c4e45abcbe500a1df55448ff7..e6c17a2d35f760a750f337726e97070498ad1865 100644 (file)
@@ -1,9 +1,12 @@
 # -*- coding: iso-8859-1 -*-
 import unittest, test.test_support
 from test.script_helper import assert_python_ok, assert_python_failure
-import sys, os, cStringIO
-import struct
+import cStringIO
+import gc
 import operator
+import os
+import struct
+import sys
 
 class SysModuleTest(unittest.TestCase):
 
@@ -754,6 +757,32 @@ class SizeofTest(unittest.TestCase):
         check(xrange(1), size('3l'))
         check(xrange(66000), size('3l'))
 
+    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('3P')  # 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')
+
     def test_pythontypes(self):
         # check all types defined in Python/
         size = test.test_support.calcobjsize
index adb4a1556be0c95712eeb2c7af29c46e5aedcc38..315ebf0b9d16df5e59ee0d2609360985dfffd3b1 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,10 @@ What's New in Python 2.7.12?
 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 #19543: Added Py3k warning for decoding unicode.
 
 - Issue #24097: Fixed crash in object.__reduce__() if slot name is freed inside
index aaac660131a08d921f6ea8d982a562842d363d61..d4cc960af4e212c610e2aa6b05cac391869295fb 100644 (file)
@@ -1103,7 +1103,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 + deque->len + BLOCKLEN - 1) / BLOCKLEN;
     assert(deque->leftindex + deque->len - 1 ==
            (blocks - 1) * BLOCKLEN + deque->rightindex);
index b4632edf44f7f31525770bbc6d9ba1e445df620d..dd518d7ac4e48dc99e3a4a29c65ec17b2bd8b6a9 100644 (file)
@@ -391,7 +391,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 f643e9124d5fd363ded0ce4b56f9001b913deef8..82f3851ea4cf61ce4e29793f2133e31267695c05 100644 (file)
@@ -809,7 +809,7 @@ bytesio_sizeof(bytesio *self, void *unused)
 {
     Py_ssize_t res;
 
-    res = sizeof(bytesio);
+    res = _PyObject_SIZE(Py_TYPE(self));
     if (self->buf)
         res += self->buf_size;
     return PyLong_FromSsize_t(res);
index bca7a2e7ca4af96482db1f50868364168b0d187f..02dd7d369aaac27bd6f52e055ecb0c0a8004cb92 100644 (file)
@@ -1737,7 +1737,7 @@ s_sizeof(PyStructObject *self, void *unused)
 {
     Py_ssize_t size;
 
-    size = sizeof(PyStructObject) + sizeof(formatcode) * (self->s_len + 1);
+    size = _PyObject_SIZE(Py_TYPE(self)) + sizeof(formatcode) * (self->s_len + 1);
     return PyLong_FromSsize_t(size);
 }
 
index 1d1f0d3845cc1cc5bac42181adab491e0f346312..f14711563c5304d37b0285b9f71bcc125d9ad86d 100644 (file)
@@ -1541,7 +1541,7 @@ static PyObject *
 array_sizeof(arrayobject *self, PyObject *unused)
 {
     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 8fdf7f75e1ab8bb9f85e2697fcac6418a1306264..1ebccdfa82a5820655a61450fa268d274a8ec0c2 100644 (file)
@@ -655,7 +655,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 eb2d600689901a8d8f82c4246bd8997e7f05e5da..2434c6742ff8e63a2be30ed46e3f1a2ede9bbd26 100644 (file)
@@ -720,7 +720,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 5276da51bdb7e4174e110e791d1422d271e48665..1fdd9167fad7fcaf25fa977988bc58288aeb48d9 100644 (file)
@@ -2780,7 +2780,7 @@ bytearray_sizeof(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 PyInt_FromSsize_t(res);
 }
 
index 50afa3f654a85a808a95b8bb7b2bbc58bd0d0c7c..b281948be8f5eed3bac683d2586a93e2b315f055 100644 (file)
@@ -2152,7 +2152,7 @@ dict_sizeof(PyDictObject *mp)
 {
     Py_ssize_t res;
 
-    res = sizeof(PyDictObject);
+    res = _PyObject_SIZE(Py_TYPE(mp));
     if (mp->ma_table != mp->ma_smalltable)
         res = res + (mp->ma_mask + 1) * sizeof(PyDictEntry);
     return PyInt_FromSsize_t(res);
index 1f43ba292c394d3452f9ef51a34dbc4671d2f609..27365b6e96bd2c879fceb79f55b7a956e8a3a5c5 100644 (file)
@@ -2469,7 +2469,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 PyInt_FromSsize_t(res);
 }
 
index 8a58d65d37ddc21b9b847fe618427b6890913a07..a9ebbec5a92909d87a73ce4f0f5393184797211f 100644 (file)
@@ -1985,7 +1985,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 PyInt_FromSsize_t(res);