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.
This commit is contained in:
Serhiy Storchaka 2015-12-19 20:05:25 +02:00
parent efd7b34d7c
commit 5c4064e8bd
19 changed files with 54 additions and 22 deletions

View 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);
}

View 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);

View 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

View 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);
}

View 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)

View 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);