Skip to content

Commit 85dd6cb

Browse files
authored
pythongh-99845: Use size_t type in __sizeof__() methods (python#99846)
The implementation of __sizeof__() methods using _PyObject_SIZE() now use an unsigned type (size_t) to compute the size, rather than a signed type (Py_ssize_t). Cast explicitly signed (Py_ssize_t) values to unsigned type (Py_ssize_t).
1 parent 18a6967 commit 85dd6cb

15 files changed

+85
-108
lines changed

Modules/_collectionsmodule.c

+5-7
Original file line numberDiff line numberDiff line change
@@ -1508,15 +1508,13 @@ deque_init(dequeobject *deque, PyObject *args, PyObject *kwdargs)
15081508
static PyObject *
15091509
deque_sizeof(dequeobject *deque, void *unused)
15101510
{
1511-
Py_ssize_t res;
1512-
Py_ssize_t blocks;
1513-
1514-
res = _PyObject_SIZE(Py_TYPE(deque));
1511+
size_t res = _PyObject_SIZE(Py_TYPE(deque));
1512+
size_t blocks;
15151513
blocks = (size_t)(deque->leftindex + Py_SIZE(deque) + BLOCKLEN - 1) / BLOCKLEN;
1516-
assert(deque->leftindex + Py_SIZE(deque) - 1 ==
1517-
(blocks - 1) * BLOCKLEN + deque->rightindex);
1514+
assert(((size_t)deque->leftindex + (size_t)Py_SIZE(deque) - 1) ==
1515+
((blocks - 1) * BLOCKLEN + (size_t)deque->rightindex));
15181516
res += blocks * sizeof(block);
1519-
return PyLong_FromSsize_t(res);
1517+
return PyLong_FromSize_t(res);
15201518
}
15211519

15221520
PyDoc_STRVAR(sizeof_doc,

Modules/_decimal/_decimal.c

+3-5
Original file line numberDiff line numberDiff line change
@@ -4796,13 +4796,11 @@ dec_reduce(PyObject *self, PyObject *dummy UNUSED)
47964796
static PyObject *
47974797
dec_sizeof(PyObject *v, PyObject *dummy UNUSED)
47984798
{
4799-
Py_ssize_t res;
4800-
4801-
res = _PyObject_SIZE(Py_TYPE(v));
4799+
size_t res = _PyObject_SIZE(Py_TYPE(v));
48024800
if (mpd_isdynamic_data(MPD(v))) {
4803-
res += MPD(v)->alloc * sizeof(mpd_uint_t);
4801+
res += (size_t)MPD(v)->alloc * sizeof(mpd_uint_t);
48044802
}
4805-
return PyLong_FromSsize_t(res);
4803+
return PyLong_FromSize_t(res);
48064804
}
48074805

48084806
/* __trunc__ */

Modules/_elementtree.c

+7-6
Original file line numberDiff line numberDiff line change
@@ -876,19 +876,20 @@ deepcopy(PyObject *object, PyObject *memo)
876876

877877

878878
/*[clinic input]
879-
_elementtree.Element.__sizeof__ -> Py_ssize_t
879+
_elementtree.Element.__sizeof__ -> size_t
880880
881881
[clinic start generated code]*/
882882

883-
static Py_ssize_t
883+
static size_t
884884
_elementtree_Element___sizeof___impl(ElementObject *self)
885-
/*[clinic end generated code: output=bf73867721008000 input=70f4b323d55a17c1]*/
885+
/*[clinic end generated code: output=baae4e7ae9fe04ec input=54e298c501f3e0d0]*/
886886
{
887-
Py_ssize_t result = _PyObject_SIZE(Py_TYPE(self));
887+
size_t result = _PyObject_SIZE(Py_TYPE(self));
888888
if (self->extra) {
889889
result += sizeof(ElementObjectExtra);
890-
if (self->extra->children != self->extra->_children)
891-
result += sizeof(PyObject*) * self->extra->allocated;
890+
if (self->extra->children != self->extra->_children) {
891+
result += (size_t)self->extra->allocated * sizeof(PyObject*);
892+
}
892893
}
893894
return result;
894895
}

Modules/_io/bufferedio.c

+5-6
Original file line numberDiff line numberDiff line change
@@ -389,12 +389,11 @@ buffered_dealloc(buffered *self)
389389
static PyObject *
390390
buffered_sizeof(buffered *self, PyObject *Py_UNUSED(ignored))
391391
{
392-
Py_ssize_t res;
393-
394-
res = _PyObject_SIZE(Py_TYPE(self));
395-
if (self->buffer)
396-
res += self->buffer_size;
397-
return PyLong_FromSsize_t(res);
392+
size_t res = _PyObject_SIZE(Py_TYPE(self));
393+
if (self->buffer) {
394+
res += (size_t)self->buffer_size;
395+
}
396+
return PyLong_FromSize_t(res);
398397
}
399398

400399
static int

Modules/_io/bytesio.c

+4-6
Original file line numberDiff line numberDiff line change
@@ -957,17 +957,15 @@ _io_BytesIO___init___impl(bytesio *self, PyObject *initvalue)
957957
static PyObject *
958958
bytesio_sizeof(bytesio *self, void *unused)
959959
{
960-
Py_ssize_t res;
961-
962-
res = _PyObject_SIZE(Py_TYPE(self));
960+
size_t res = _PyObject_SIZE(Py_TYPE(self));
963961
if (self->buf && !SHARED_BUF(self)) {
964-
Py_ssize_t s = _PySys_GetSizeOf(self->buf);
965-
if (s == -1) {
962+
size_t s = _PySys_GetSizeOf(self->buf);
963+
if (s == (size_t)-1) {
966964
return NULL;
967965
}
968966
res += s;
969967
}
970-
return PyLong_FromSsize_t(res);
968+
return PyLong_FromSize_t(res);
971969
}
972970

973971
static int

Modules/_pickle.c

+12-15
Original file line numberDiff line numberDiff line change
@@ -4575,26 +4575,25 @@ _pickle_Pickler_dump(PicklerObject *self, PyObject *obj)
45754575

45764576
/*[clinic input]
45774577
4578-
_pickle.Pickler.__sizeof__ -> Py_ssize_t
4578+
_pickle.Pickler.__sizeof__ -> size_t
45794579
45804580
Returns size in memory, in bytes.
45814581
[clinic start generated code]*/
45824582

4583-
static Py_ssize_t
4583+
static size_t
45844584
_pickle_Pickler___sizeof___impl(PicklerObject *self)
4585-
/*[clinic end generated code: output=106edb3123f332e1 input=8cbbec9bd5540d42]*/
4585+
/*[clinic end generated code: output=23ad75658d3b59ff input=d8127c8e7012ebd7]*/
45864586
{
4587-
Py_ssize_t res, s;
4588-
4589-
res = _PyObject_SIZE(Py_TYPE(self));
4587+
size_t res = _PyObject_SIZE(Py_TYPE(self));
45904588
if (self->memo != NULL) {
45914589
res += sizeof(PyMemoTable);
45924590
res += self->memo->mt_allocated * sizeof(PyMemoEntry);
45934591
}
45944592
if (self->output_buffer != NULL) {
4595-
s = _PySys_GetSizeOf(self->output_buffer);
4596-
if (s == -1)
4593+
size_t s = _PySys_GetSizeOf(self->output_buffer);
4594+
if (s == (size_t)-1) {
45974595
return -1;
4596+
}
45984597
res += s;
45994598
}
46004599
return res;
@@ -7079,22 +7078,20 @@ _pickle_Unpickler_find_class_impl(UnpicklerObject *self,
70797078

70807079
/*[clinic input]
70817080
7082-
_pickle.Unpickler.__sizeof__ -> Py_ssize_t
7081+
_pickle.Unpickler.__sizeof__ -> size_t
70837082
70847083
Returns size in memory, in bytes.
70857084
[clinic start generated code]*/
70867085

7087-
static Py_ssize_t
7086+
static size_t
70887087
_pickle_Unpickler___sizeof___impl(UnpicklerObject *self)
7089-
/*[clinic end generated code: output=119d9d03ad4c7651 input=13333471fdeedf5e]*/
7088+
/*[clinic end generated code: output=4648d84c228196df input=27180b2b6b524012]*/
70907089
{
7091-
Py_ssize_t res;
7092-
7093-
res = _PyObject_SIZE(Py_TYPE(self));
7090+
size_t res = _PyObject_SIZE(Py_TYPE(self));
70947091
if (self->memo != NULL)
70957092
res += self->memo_size * sizeof(PyObject *);
70967093
if (self->marks != NULL)
7097-
res += self->marks_size * sizeof(Py_ssize_t);
7094+
res += (size_t)self->marks_size * sizeof(Py_ssize_t);
70987095
if (self->input_line != NULL)
70997096
res += strlen(self->input_line) + 1;
71007097
if (self->encoding != NULL)

Modules/_struct.c

+4-6
Original file line numberDiff line numberDiff line change
@@ -2090,13 +2090,11 @@ PyDoc_STRVAR(s_sizeof__doc__,
20902090
static PyObject *
20912091
s_sizeof(PyStructObject *self, void *unused)
20922092
{
2093-
Py_ssize_t size;
2094-
formatcode *code;
2095-
2096-
size = _PyObject_SIZE(Py_TYPE(self)) + sizeof(formatcode);
2097-
for (code = self->s_codes; code->fmtdef != NULL; code++)
2093+
size_t size = _PyObject_SIZE(Py_TYPE(self)) + sizeof(formatcode);
2094+
for (formatcode *code = self->s_codes; code->fmtdef != NULL; code++) {
20982095
size += sizeof(formatcode);
2099-
return PyLong_FromSsize_t(size);
2096+
}
2097+
return PyLong_FromSize_t(size);
21002098
}
21012099

21022100
/* List of functions */

Modules/arraymodule.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -1773,9 +1773,9 @@ static PyObject *
17731773
array_array___sizeof___impl(arrayobject *self)
17741774
/*[clinic end generated code: output=d8e1c61ebbe3eaed input=805586565bf2b3c6]*/
17751775
{
1776-
Py_ssize_t res;
1777-
res = _PyObject_SIZE(Py_TYPE(self)) + self->allocated * self->ob_descr->itemsize;
1778-
return PyLong_FromSsize_t(res);
1776+
size_t res = _PyObject_SIZE(Py_TYPE(self));
1777+
res += (size_t)self->allocated * (size_t)self->ob_descr->itemsize;
1778+
return PyLong_FromSize_t(res);
17791779
}
17801780

17811781

Modules/clinic/_elementtree.c.h

+5-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/clinic/_pickle.c.h

+9-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/itertoolsmodule.c

+13-21
Original file line numberDiff line numberDiff line change
@@ -2484,11 +2484,9 @@ product_dealloc(productobject *lz)
24842484
static PyObject *
24852485
product_sizeof(productobject *lz, void *unused)
24862486
{
2487-
Py_ssize_t res;
2488-
2489-
res = _PyObject_SIZE(Py_TYPE(lz));
2490-
res += PyTuple_GET_SIZE(lz->pools) * sizeof(Py_ssize_t);
2491-
return PyLong_FromSsize_t(res);
2487+
size_t res = _PyObject_SIZE(Py_TYPE(lz));
2488+
res += (size_t)PyTuple_GET_SIZE(lz->pools) * sizeof(Py_ssize_t);
2489+
return PyLong_FromSize_t(res);
24922490
}
24932491

24942492
PyDoc_STRVAR(sizeof_doc, "Returns size in memory, in bytes.");
@@ -2817,11 +2815,9 @@ combinations_dealloc(combinationsobject *co)
28172815
static PyObject *
28182816
combinations_sizeof(combinationsobject *co, void *unused)
28192817
{
2820-
Py_ssize_t res;
2821-
2822-
res = _PyObject_SIZE(Py_TYPE(co));
2823-
res += co->r * sizeof(Py_ssize_t);
2824-
return PyLong_FromSsize_t(res);
2818+
size_t res = _PyObject_SIZE(Py_TYPE(co));
2819+
res += (size_t)co->r * sizeof(Py_ssize_t);
2820+
return PyLong_FromSize_t(res);
28252821
}
28262822

28272823
static int
@@ -3153,11 +3149,9 @@ cwr_dealloc(cwrobject *co)
31533149
static PyObject *
31543150
cwr_sizeof(cwrobject *co, void *unused)
31553151
{
3156-
Py_ssize_t res;
3157-
3158-
res = _PyObject_SIZE(Py_TYPE(co));
3159-
res += co->r * sizeof(Py_ssize_t);
3160-
return PyLong_FromSsize_t(res);
3152+
size_t res = _PyObject_SIZE(Py_TYPE(co));
3153+
res += (size_t)co->r * sizeof(Py_ssize_t);
3154+
return PyLong_FromSize_t(res);
31613155
}
31623156

31633157
static int
@@ -3498,12 +3492,10 @@ permutations_dealloc(permutationsobject *po)
34983492
static PyObject *
34993493
permutations_sizeof(permutationsobject *po, void *unused)
35003494
{
3501-
Py_ssize_t res;
3502-
3503-
res = _PyObject_SIZE(Py_TYPE(po));
3504-
res += PyTuple_GET_SIZE(po->pool) * sizeof(Py_ssize_t);
3505-
res += po->r * sizeof(Py_ssize_t);
3506-
return PyLong_FromSsize_t(res);
3495+
size_t res = _PyObject_SIZE(Py_TYPE(po));
3496+
res += (size_t)PyTuple_GET_SIZE(po->pool) * sizeof(Py_ssize_t);
3497+
res += (size_t)po->r * sizeof(Py_ssize_t);
3498+
return PyLong_FromSize_t(res);
35073499
}
35083500

35093501
static int

Modules/mmapmodule.c

+4-5
Original file line numberDiff line numberDiff line change
@@ -804,12 +804,11 @@ mmap__repr__method(PyObject *self)
804804
static PyObject *
805805
mmap__sizeof__method(mmap_object *self, void *unused)
806806
{
807-
Py_ssize_t res;
808-
809-
res = _PyObject_SIZE(Py_TYPE(self));
810-
if (self->tagname)
807+
size_t res = _PyObject_SIZE(Py_TYPE(self));
808+
if (self->tagname) {
811809
res += strlen(self->tagname) + 1;
812-
return PyLong_FromSsize_t(res);
810+
}
811+
return PyLong_FromSize_t(res);
813812
}
814813
#endif
815814

Objects/bytearrayobject.c

+3-4
Original file line numberDiff line numberDiff line change
@@ -2151,10 +2151,9 @@ static PyObject *
21512151
bytearray_sizeof_impl(PyByteArrayObject *self)
21522152
/*[clinic end generated code: output=738abdd17951c427 input=e27320fd98a4bc5a]*/
21532153
{
2154-
Py_ssize_t res;
2155-
2156-
res = _PyObject_SIZE(Py_TYPE(self)) + self->ob_alloc * sizeof(char);
2157-
return PyLong_FromSsize_t(res);
2154+
size_t res = _PyObject_SIZE(Py_TYPE(self));
2155+
res += (size_t)self->ob_alloc * sizeof(char);
2156+
return PyLong_FromSize_t(res);
21582157
}
21592158

21602159
static PySequenceMethods bytearray_as_sequence = {

Objects/listobject.c

+3-4
Original file line numberDiff line numberDiff line change
@@ -2806,10 +2806,9 @@ static PyObject *
28062806
list___sizeof___impl(PyListObject *self)
28072807
/*[clinic end generated code: output=3417541f95f9a53e input=b8030a5d5ce8a187]*/
28082808
{
2809-
Py_ssize_t res;
2810-
2811-
res = _PyObject_SIZE(Py_TYPE(self)) + self->allocated * sizeof(void*);
2812-
return PyLong_FromSsize_t(res);
2809+
size_t res = _PyObject_SIZE(Py_TYPE(self));
2810+
res += (size_t)self->allocated * sizeof(void*);
2811+
return PyLong_FromSize_t(res);
28132812
}
28142813

28152814
static PyObject *list_iter(PyObject *seq);

0 commit comments

Comments
 (0)