summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2015-12-19 18:07:11 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2015-12-19 18:07:11 (GMT)
commita9406e77fa7bd3618c16edd248c24010af7035c1 (patch)
tree885dac8e53616cebafbb37bc5613040513bd9581
parenta254921cd4c1ca22d725872601292c48b7caa20a (diff)
parent5c4064e8bd199d70eefd7ec24766957c22f1b8e8 (diff)
downloadcpython-a9406e77fa7bd3618c16edd248c24010af7035c1.zip
cpython-a9406e77fa7bd3618c16edd248c24010af7035c1.tar.gz
cpython-a9406e77fa7bd3618c16edd248c24010af7035c1.tar.bz2
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.
-rw-r--r--Lib/test/test_sys.py30
-rw-r--r--Misc/NEWS4
-rw-r--r--Modules/_collectionsmodule.c2
-rw-r--r--Modules/_decimal/_decimal.c2
-rw-r--r--Modules/_elementtree.c2
-rw-r--r--Modules/_io/bufferedio.c2
-rw-r--r--Modules/_io/bytesio.c2
-rw-r--r--Modules/_pickle.c4
-rw-r--r--Modules/_struct.c2
-rw-r--r--Modules/arraymodule.c2
-rw-r--r--Modules/itertoolsmodule.c8
-rw-r--r--Modules/mmapmodule.c2
-rw-r--r--Modules/parsermodule.c2
-rw-r--r--Objects/bytearrayobject.c2
-rw-r--r--Objects/codeobject.c2
-rw-r--r--Objects/dictobject.c2
-rw-r--r--Objects/listobject.c2
-rw-r--r--Objects/odictobject.c2
-rw-r--r--Objects/setobject.c2
19 files changed, 54 insertions, 22 deletions
diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py
index 2d95653..4f1577d 100644
--- a/Lib/test/test_sys.py
+++ b/Lib/test/test_sys.py
@@ -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
diff --git a/Misc/NEWS b/Misc/NEWS
index 92572fa..465c1bd 100644
--- 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 #5319: New Py_FinalizeEx() API allowing Python to set an exit status
diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c
index 0e59470..e3d1910 100644
--- a/Modules/_collectionsmodule.c
+++ b/Modules/_collectionsmodule.c
@@ -1476,7 +1476,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 = (size_t)(deque->leftindex + Py_SIZE(deque) + BLOCKLEN - 1) / BLOCKLEN;
assert(deque->leftindex + Py_SIZE(deque) - 1 ==
(blocks - 1) * BLOCKLEN + deque->rightindex);
diff --git a/Modules/_decimal/_decimal.c b/Modules/_decimal/_decimal.c
index 169914c..112b44f 100644
--- a/Modules/_decimal/_decimal.c
+++ b/Modules/_decimal/_decimal.c
@@ -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);
}
diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c
index c483d87..57c7af2 100644
--- a/Modules/_elementtree.c
+++ b/Modules/_elementtree.c
@@ -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)
diff --git a/Modules/_io/bufferedio.c b/Modules/_io/bufferedio.c
index 29e000b..6bb2200 100644
--- a/Modules/_io/bufferedio.c
+++ b/Modules/_io/bufferedio.c
@@ -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);
diff --git a/Modules/_io/bytesio.c b/Modules/_io/bytesio.c
index 31cc1f7..eef3b3d 100644
--- a/Modules/_io/bytesio.c
+++ b/Modules/_io/bytesio.c
@@ -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);
diff --git a/Modules/_pickle.c b/Modules/_pickle.c
index b3d9b91..487b533 100644
--- a/Modules/_pickle.c
+++ b/Modules/_pickle.c
@@ -4087,7 +4087,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);
@@ -6509,7 +6509,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)
diff --git a/Modules/_struct.c b/Modules/_struct.c
index 068c5d1..b61f9f6 100644
--- a/Modules/_struct.c
+++ b/Modules/_struct.c
@@ -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);
diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c
index a3ccf93..6af75a4 100644
--- a/Modules/arraymodule.c
+++ b/Modules/arraymodule.c
@@ -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);
}
diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c
index 2e7cc4f..0c6daa1 100644
--- a/Modules/itertoolsmodule.c
+++ b/Modules/itertoolsmodule.c
@@ -2109,7 +2109,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);
}
@@ -2440,7 +2440,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);
}
@@ -2781,7 +2781,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);
}
@@ -3129,7 +3129,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);
diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c
index daab52b..bb98a99 100644
--- a/Modules/mmapmodule.c
+++ b/Modules/mmapmodule.c
@@ -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);
diff --git a/Modules/parsermodule.c b/Modules/parsermodule.c
index 8023558..6471b8e 100644
--- a/Modules/parsermodule.c
+++ b/Modules/parsermodule.c
@@ -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);
}
diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c
index 131e04d..96ab57d 100644
--- a/Objects/bytearrayobject.c
+++ b/Objects/bytearrayobject.c
@@ -2904,7 +2904,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);
}
diff --git a/Objects/codeobject.c b/Objects/codeobject.c
index 353f414..b0e3446 100644
--- a/Objects/codeobject.c
+++ b/Objects/codeobject.c
@@ -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);
diff --git a/Objects/dictobject.c b/Objects/dictobject.c
index 4a72c9a..752853f 100644
--- a/Objects/dictobject.c
+++ b/Objects/dictobject.c
@@ -2563,7 +2563,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
diff --git a/Objects/listobject.c b/Objects/listobject.c
index 45e54ce..eee7c68 100644
--- a/Objects/listobject.c
+++ b/Objects/listobject.c
@@ -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);
}
diff --git a/Objects/odictobject.c b/Objects/odictobject.c
index 5ad88bc..4e51f4d 100644
--- a/Objects/odictobject.c
+++ b/Objects/odictobject.c
@@ -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)
diff --git a/Objects/setobject.c b/Objects/setobject.c
index 45aa200..9468ee6 100644
--- a/Objects/setobject.c
+++ b/Objects/setobject.c
@@ -2003,7 +2003,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);