Issue #23629: Fix the default __sizeof__ implementation for variable-… · pythoncapi/cpython@63afdaa · GitHub
Skip to content

Commit 63afdaa

Browse files
committed
Issue python#23629: Fix the default __sizeof__ implementation for variable-sized objects.
2 parents 12541dc + a654510 commit 63afdaa

6 files changed

Lines changed: 22 additions & 52 deletions

File tree

Lib/test/test_buffer.py

Lines changed: 15 additions & 0 deletions

Lib/test/test_sys.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,9 @@ def test_objecttypes(self):
846846
check(x, vsize('n2Pi') + x.__alloc__())
847847
# bytearray_iterator
848848
check(iter(bytearray()), size('nP'))
849+
# bytes
850+
check(b'', vsize('n') + 1)
851+
check(b'x' * 10, vsize('n') + 11)
849852
# cell
850853
def get_cell():
851854
x = 42
@@ -965,8 +968,6 @@ def get_gen(): yield 1
965968
check(int(PyLong_BASE), vsize('') + 2*self.longdigit)
966969
check(int(PyLong_BASE**2-1), vsize('') + 2*self.longdigit)
967970
check(int(PyLong_BASE**2), vsize('') + 3*self.longdigit)
968-
# memoryview
969-
check(memoryview(b''), size('Pnin 2P2n2i5P Pn'))
970971
# module
971972
check(unittest, size('PnPPP'))
972973
# None

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ Release date: 2015-03-28
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 & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3463,42 +3463,6 @@ bytes_fromhex_impl(PyTypeObject *type, PyObject *string)
34633463
return NULL;
34643464
}
34653465

3466-
/*[clinic input]
3467-
bytes.__sizeof__ as bytes_sizeof
3468-
3469-
self: self(type="PyBytesObject *")
3470-
3471-
Returns the size of the bytes object in memory, in bytes.
3472-
[clinic start generated code]*/
3473-
3474-
PyDoc_STRVAR(bytes_sizeof__doc__,
3475-
"__sizeof__($self, /)\n"
3476-
"--\n"
3477-
"\n"
3478-
"Returns the size of the bytes object in memory, in bytes.");
3479-
3480-
#define BYTES_SIZEOF_METHODDEF \
3481-
{"__sizeof__", (PyCFunction)bytes_sizeof, METH_NOARGS, bytes_sizeof__doc__},
3482-
3483-
static PyObject *
3484-
bytes_sizeof_impl(PyBytesObject *self);
3485-
3486-
static PyObject *
3487-
bytes_sizeof(PyBytesObject *self, PyObject *Py_UNUSED(ignored))
3488-
{
3489-
return bytes_sizeof_impl(self);
3490-
}
3491-
3492-
static PyObject *
3493-
bytes_sizeof_impl(PyBytesObject *self)
3494-
/*[clinic end generated code: output=44933279343f24ae input=bee4c64bb42078ed]*/
3495-
{
3496-
Py_ssize_t res;
3497-
res = PyBytesObject_SIZE + Py_SIZE(self) * Py_TYPE(self)->tp_itemsize;
3498-
return PyLong_FromSsize_t(res);
3499-
}
3500-
3501-
35023466
static PyObject *
35033467
bytes_getnewargs(PyBytesObject *v)
35043468
{
@@ -3559,7 +3523,6 @@ bytes_methods[] = {
35593523
BYTES_TRANSLATE_METHODDEF
35603524
{"upper", (PyCFunction)stringlib_upper, METH_NOARGS, _Py_upper__doc__},
35613525
{"zfill", (PyCFunction)stringlib_zfill, METH_VARARGS, zfill__doc__},
3562-
BYTES_SIZEOF_METHODDEF
35633526
{NULL, NULL} /* sentinel */
35643527
};
35653528

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

0 commit comments

Comments
 (0)