From a654510150cb738b61033c32e30bd4be9f0ed6ed Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Tue, 10 Mar 2015 22:32:00 +0100 Subject: [PATCH] Issue #23629: Fix the default __sizeof__ implementation for variable-sized objects. --- Lib/test/test_buffer.py | 15 +++++++++++++++ Lib/test/test_sys.py | 5 +++-- Misc/NEWS | 3 +++ Objects/bytesobject.c | 14 -------------- Objects/tupleobject.c | 12 ------------ Objects/typeobject.c | 2 +- 6 files changed, 22 insertions(+), 29 deletions(-) diff --git a/Lib/test/test_buffer.py b/Lib/test/test_buffer.py index 1667847a9df..e84f3e4e263 100644 --- a/Lib/test/test_buffer.py +++ b/Lib/test/test_buffer.py @@ -2449,6 +2449,21 @@ class TestBufferProtocol(unittest.TestCase): self.assertEqual(m.tobytes(), b'') self.assertEqual(m.tolist(), []) + check_sizeof = support.check_sizeof + + def test_memoryview_sizeof(self): + check = self.check_sizeof + vsize = support.calcvobjsize + base_struct = 'Pnin 2P2n2i5P 3cP' + per_dim = '3n' + + items = list(range(8)) + check(memoryview(b''), vsize(base_struct + 1 * per_dim)) + a = ndarray(items, shape=[2, 4], format="b") + check(memoryview(a), vsize(base_struct + 2 * per_dim)) + a = ndarray(items, shape=[2, 2, 2], format="b") + check(memoryview(a), vsize(base_struct + 3 * per_dim)) + def test_memoryview_struct_module(self): class INT(object): diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py index 2f66eb71d2e..a6d926f7ded 100644 --- a/Lib/test/test_sys.py +++ b/Lib/test/test_sys.py @@ -778,6 +778,9 @@ class SizeofTest(unittest.TestCase): check(x, vsize('n2Pi') + x.__alloc__()) # bytearray_iterator check(iter(bytearray()), size('nP')) + # bytes + check(b'', vsize('n') + 1) + check(b'x' * 10, vsize('n') + 11) # cell def get_cell(): x = 42 @@ -897,8 +900,6 @@ class SizeofTest(unittest.TestCase): check(int(PyLong_BASE), vsize('') + 2*self.longdigit) check(int(PyLong_BASE**2-1), vsize('') + 2*self.longdigit) check(int(PyLong_BASE**2), vsize('') + 3*self.longdigit) - # memoryview - check(memoryview(b''), size('Pnin 2P2n2i5P 3cPn')) # module check(unittest, size('PnPPP')) # None diff --git a/Misc/NEWS b/Misc/NEWS index 197e87d1433..e52a480f9e2 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -10,6 +10,9 @@ Release date: tba Core and Builtins ----------------- +- Issue #23629: Fix the default __sizeof__ implementation for variable-sized + objects. + Library ------- diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c index e56dbed3551..b9b49acebec 100644 --- a/Objects/bytesobject.c +++ b/Objects/bytesobject.c @@ -2370,18 +2370,6 @@ bytes_fromhex(PyObject *cls, PyObject *args) return NULL; } -PyDoc_STRVAR(sizeof__doc__, -"B.__sizeof__() -> size of B in memory, in bytes"); - -static PyObject * -bytes_sizeof(PyBytesObject *v) -{ - Py_ssize_t res; - res = PyBytesObject_SIZE + Py_SIZE(v) * Py_TYPE(v)->tp_itemsize; - return PyLong_FromSsize_t(res); -} - - static PyObject * bytes_getnewargs(PyBytesObject *v) { @@ -2447,8 +2435,6 @@ bytes_methods[] = { translate__doc__}, {"upper", (PyCFunction)stringlib_upper, METH_NOARGS, _Py_upper__doc__}, {"zfill", (PyCFunction)stringlib_zfill, METH_VARARGS, zfill__doc__}, - {"__sizeof__", (PyCFunction)bytes_sizeof, METH_NOARGS, - sizeof__doc__}, {NULL, NULL} /* sentinel */ }; diff --git a/Objects/tupleobject.c b/Objects/tupleobject.c index 6fd4db3ae01..8c00210de18 100644 --- a/Objects/tupleobject.c +++ b/Objects/tupleobject.c @@ -759,27 +759,15 @@ tuple_getnewargs(PyTupleObject *v) } -static PyObject * -tuple_sizeof(PyTupleObject *self) -{ - Py_ssize_t res; - - res = PyTuple_Type.tp_basicsize + Py_SIZE(self) * sizeof(PyObject *); - return PyLong_FromSsize_t(res); -} - PyDoc_STRVAR(index_doc, "T.index(value, [start, [stop]]) -> integer -- return first index of value.\n" "Raises ValueError if the value is not present." ); PyDoc_STRVAR(count_doc, "T.count(value) -> integer -- return number of occurrences of value"); -PyDoc_STRVAR(sizeof_doc, -"T.__sizeof__() -- size of T in memory, in bytes"); static PyMethodDef tuple_methods[] = { {"__getnewargs__", (PyCFunction)tuple_getnewargs, METH_NOARGS}, - {"__sizeof__", (PyCFunction)tuple_sizeof, METH_NOARGS, sizeof_doc}, {"index", (PyCFunction)tupleindex, METH_VARARGS, index_doc}, {"count", (PyCFunction)tuplecount, METH_O, count_doc}, {NULL, NULL} /* sentinel */ diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 482a7a5267f..cf589112090 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -4258,7 +4258,7 @@ object_sizeof(PyObject *self, PyObject *args) res = 0; isize = self->ob_type->tp_itemsize; if (isize > 0) - res = Py_SIZE(self->ob_type) * isize; + res = Py_SIZE(self) * isize; res += self->ob_type->tp_basicsize; return PyLong_FromSsize_t(res);