Skip to content

Commit a9406e7

Browse files
Issue python#25421: __sizeof__ methods of builtin types now use dynamic basic size.
This allows sys.getsize() to work correctly with their subclasses with __slots__ defined.
2 parents a254921 + 5c4064e commit a9406e7

19 files changed

Lines changed: 54 additions & 22 deletions

‎Lib/test/test_sys.py‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,6 +1112,36 @@ class newstyleclass(object): pass
11121112
# weakcallableproxy
11131113
check(weakref.proxy(int), size('2Pn2P'))
11141114

1115+
def check_slots(self, obj, base, extra):
1116+
expected = sys.getsizeof(base) + struct.calcsize(extra)
1117+
if gc.is_tracked(obj) and not gc.is_tracked(base):
1118+
expected += struct.calcsize('2Pn') # PyGC_Head
1119+
self.assertEqual(sys.getsizeof(obj), expected)
1120+
1121+
def test_slots(self):
1122+
# check all subclassable types defined in Objects/ that allow
1123+
# non-empty __slots__
1124+
check = self.check_slots
1125+
class BA(bytearray):
1126+
__slots__ = 'a', 'b', 'c'
1127+
check(BA(), bytearray(), '3P')
1128+
class D(dict):
1129+
__slots__ = 'a', 'b', 'c'
1130+
check(D(x=[]), {'x': []}, '3P')
1131+
class L(list):
1132+
__slots__ = 'a', 'b', 'c'
1133+
check(L(), [], '3P')
1134+
class S(set):
1135+
__slots__ = 'a', 'b', 'c'
1136+
check(S(), set(), '3P')
1137+
class FS(frozenset):
1138+
__slots__ = 'a', 'b', 'c'
1139+
check(FS(), frozenset(), '3P')
1140+
from collections import OrderedDict
1141+
class OD(OrderedDict):
1142+
__slots__ = 'a', 'b', 'c'
1143+
check(OD(x=[]), OrderedDict(x=[]), '3P')
1144+
11151145
def test_pythontypes(self):
11161146
# check all types defined in Python/
11171147
size = test.support.calcobjsize

‎Misc/NEWS‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ Release date: tba
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #25421: __sizeof__ methods of builtin types now use dynamic basic size.
14+
This allows sys.getsize() to work correctly with their subclasses with
15+
__slots__ defined.
16+
1317
- Issue #25709: Fixed problem with in-place string concatenation and utf-8 cache.
1418

1519
- Issue #5319: New Py_FinalizeEx() API allowing Python to set an exit status

‎Modules/_collectionsmodule.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1476,7 +1476,7 @@ deque_sizeof(dequeobject *deque, void *unused)
14761476
Py_ssize_t res;
14771477
Py_ssize_t blocks;
14781478

1479-
res = sizeof(dequeobject);
1479+
res = _PyObject_SIZE(Py_TYPE(deque));
14801480
blocks = (size_t)(deque->leftindex + Py_SIZE(deque) + BLOCKLEN - 1) / BLOCKLEN;
14811481
assert(deque->leftindex + Py_SIZE(deque) - 1 ==
14821482
(blocks - 1) * BLOCKLEN + deque->rightindex);

‎Modules/_decimal/_decimal.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4529,7 +4529,7 @@ dec_sizeof(PyObject *v, PyObject *dummy UNUSED)
45294529
{
45304530
Py_ssize_t res;
45314531

4532-
res = sizeof(PyDecObject);
4532+
res = _PyObject_SIZE(Py_TYPE(v));
45334533
if (mpd_isdynamic_data(MPD(v))) {
45344534
res += MPD(v)->alloc * sizeof(mpd_uint_t);
45354535
}

‎Modules/_elementtree.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,7 @@ static Py_ssize_t
847847
_elementtree_Element___sizeof___impl(ElementObject *self)
848848
/*[clinic end generated code: output=bf73867721008000 input=70f4b323d55a17c1]*/
849849
{
850-
Py_ssize_t result = sizeof(ElementObject);
850+
Py_ssize_t result = _PyObject_SIZE(Py_TYPE(self));
851851
if (self->extra) {
852852
result += sizeof(ElementObjectExtra);
853853
if (self->extra->children != self->extra->_children)

‎Modules/_io/bufferedio.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ buffered_sizeof(buffered *self, void *unused)
423423
{
424424
Py_ssize_t res;
425425

426-
res = sizeof(buffered);
426+
res = _PyObject_SIZE(Py_TYPE(self));
427427
if (self->buffer)
428428
res += self->buffer_size;
429429
return PyLong_FromSsize_t(res);

‎Modules/_io/bytesio.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -991,7 +991,7 @@ bytesio_sizeof(bytesio *self, void *unused)
991991
{
992992
Py_ssize_t res;
993993

994-
res = sizeof(bytesio);
994+
res = _PyObject_SIZE(Py_TYPE(self));
995995
if (self->buf && !SHARED_BUF(self))
996996
res += _PySys_GetSizeOf(self->buf);
997997
return PyLong_FromSsize_t(res);

‎Modules/_pickle.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4087,7 +4087,7 @@ _pickle_Pickler___sizeof___impl(PicklerObject *self)
40874087
{
40884088
Py_ssize_t res, s;
40894089

4090-
res = sizeof(PicklerObject);
4090+
res = _PyObject_SIZE(Py_TYPE(self));
40914091
if (self->memo != NULL) {
40924092
res += sizeof(PyMemoTable);
40934093
res += self->memo->mt_allocated * sizeof(PyMemoEntry);
@@ -6509,7 +6509,7 @@ _pickle_Unpickler___sizeof___impl(UnpicklerObject *self)
65096509
{
65106510
Py_ssize_t res;
65116511

6512-
res = sizeof(UnpicklerObject);
6512+
res = _PyObject_SIZE(Py_TYPE(self));
65136513
if (self->memo != NULL)
65146514
res += self->memo_size * sizeof(PyObject *);
65156515
if (self->marks != NULL)

‎Modules/_struct.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1924,7 +1924,7 @@ s_sizeof(PyStructObject *self, void *unused)
19241924
Py_ssize_t size;
19251925
formatcode *code;
19261926

1927-
size = sizeof(PyStructObject) + sizeof(formatcode);
1927+
size = _PyObject_SIZE(Py_TYPE(self)) + sizeof(formatcode);
19281928
for (code = self->s_codes; code->fmtdef != NULL; code++)
19291929
size += sizeof(formatcode);
19301930
return PyLong_FromSsize_t(size);

‎Modules/arraymodule.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1743,7 +1743,7 @@ array_array___sizeof___impl(arrayobject *self)
17431743
/*[clinic end generated code: output=d8e1c61ebbe3eaed input=805586565bf2b3c6]*/
17441744
{
17451745
Py_ssize_t res;
1746-
res = sizeof(arrayobject) + self->allocated * self->ob_descr->itemsize;
1746+
res = _PyObject_SIZE(Py_TYPE(self)) + self->allocated * self->ob_descr->itemsize;
17471747
return PyLong_FromSsize_t(res);
17481748
}
17491749

0 commit comments

Comments
 (0)