Skip to content

Commit a654510

Browse files
committed
Issue #23629: Fix the default __sizeof__ implementation for variable-sized objects.
1 parent ebb8c2d commit a654510

File tree

6 files changed

+22
-29
lines changed

6 files changed

+22
-29
lines changed

Lib/test/test_buffer.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2449,6 +2449,21 @@ def test_memoryview_cast_zero_shape(self):
24492449
self.assertEqual(m.tobytes(), b'')
24502450
self.assertEqual(m.tolist(), [])
24512451

2452+
check_sizeof = support.check_sizeof
2453+
2454+
def test_memoryview_sizeof(self):
2455+
check = self.check_sizeof
2456+
vsize = support.calcvobjsize
2457+
base_struct = 'Pnin 2P2n2i5P 3cP'
2458+
per_dim = '3n'
2459+
2460+
items = list(range(8))
2461+
check(memoryview(b''), vsize(base_struct + 1 * per_dim))
2462+
a = ndarray(items, shape=[2, 4], format="b")
2463+
check(memoryview(a), vsize(base_struct + 2 * per_dim))
2464+
a = ndarray(items, shape=[2, 2, 2], format="b")
2465+
check(memoryview(a), vsize(base_struct + 3 * per_dim))
2466+
24522467
def test_memoryview_struct_module(self):
24532468

24542469
class INT(object):

Lib/test/test_sys.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,9 @@ def test_objecttypes(self):
778778
check(x, vsize('n2Pi') + x.__alloc__())
779779
# bytearray_iterator
780780
check(iter(bytearray()), size('nP'))
781+
# bytes
782+
check(b'', vsize('n') + 1)
783+
check(b'x' * 10, vsize('n') + 11)
781784
# cell
782785
def get_cell():
783786
x = 42
@@ -897,8 +900,6 @@ def get_gen(): yield 1
897900
check(int(PyLong_BASE), vsize('') + 2*self.longdigit)
898901
check(int(PyLong_BASE**2-1), vsize('') + 2*self.longdigit)
899902
check(int(PyLong_BASE**2), vsize('') + 3*self.longdigit)
900-
# memoryview
901-
check(memoryview(b''), size('Pnin 2P2n2i5P 3cPn'))
902903
# module
903904
check(unittest, size('PnPPP'))
904905
# None

Misc/NEWS

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

13+
- Issue #23629: Fix the default __sizeof__ implementation for variable-sized
14+
objects.
15+
1316
Library
1417
-------
1518

Objects/bytesobject.c

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2370,18 +2370,6 @@ bytes_fromhex(PyObject *cls, PyObject *args)
23702370
return NULL;
23712371
}
23722372

2373-
PyDoc_STRVAR(sizeof__doc__,
2374-
"B.__sizeof__() -> size of B in memory, in bytes");
2375-
2376-
static PyObject *
2377-
bytes_sizeof(PyBytesObject *v)
2378-
{
2379-
Py_ssize_t res;
2380-
res = PyBytesObject_SIZE + Py_SIZE(v) * Py_TYPE(v)->tp_itemsize;
2381-
return PyLong_FromSsize_t(res);
2382-
}
2383-
2384-
23852373
static PyObject *
23862374
bytes_getnewargs(PyBytesObject *v)
23872375
{
@@ -2447,8 +2435,6 @@ bytes_methods[] = {
24472435
translate__doc__},
24482436
{"upper", (PyCFunction)stringlib_upper, METH_NOARGS, _Py_upper__doc__},
24492437
{"zfill", (PyCFunction)stringlib_zfill, METH_VARARGS, zfill__doc__},
2450-
{"__sizeof__", (PyCFunction)bytes_sizeof, METH_NOARGS,
2451-
sizeof__doc__},
24522438
{NULL, NULL} /* sentinel */
24532439
};
24542440

Objects/tupleobject.c

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -759,27 +759,15 @@ tuple_getnewargs(PyTupleObject *v)
759759

760760
}
761761

762-
static PyObject *
763-
tuple_sizeof(PyTupleObject *self)
764-
{
765-
Py_ssize_t res;
766-
767-
res = PyTuple_Type.tp_basicsize + Py_SIZE(self) * sizeof(PyObject *);
768-
return PyLong_FromSsize_t(res);
769-
}
770-
771762
PyDoc_STRVAR(index_doc,
772763
"T.index(value, [start, [stop]]) -> integer -- return first index of value.\n"
773764
"Raises ValueError if the value is not present."
774765
);
775766
PyDoc_STRVAR(count_doc,
776767
"T.count(value) -> integer -- return number of occurrences of value");
777-
PyDoc_STRVAR(sizeof_doc,
778-
"T.__sizeof__() -- size of T in memory, in bytes");
779768

780769
static PyMethodDef tuple_methods[] = {
781770
{"__getnewargs__", (PyCFunction)tuple_getnewargs, METH_NOARGS},
782-
{"__sizeof__", (PyCFunction)tuple_sizeof, METH_NOARGS, sizeof_doc},
783771
{"index", (PyCFunction)tupleindex, METH_VARARGS, index_doc},
784772
{"count", (PyCFunction)tuplecount, METH_O, count_doc},
785773
{NULL, NULL} /* sentinel */

Objects/typeobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4258,7 +4258,7 @@ object_sizeof(PyObject *self, PyObject *args)
42584258
res = 0;
42594259
isize = self->ob_type->tp_itemsize;
42604260
if (isize > 0)
4261-
res = Py_SIZE(self->ob_type) * isize;
4261+
res = Py_SIZE(self) * isize;
42624262
res += self->ob_type->tp_basicsize;
42634263

42644264
return PyLong_FromSsize_t(res);

0 commit comments

Comments
 (0)