Skip to content

Commit 85dd6cb

Browse files
authored
gh-99845: Use size_t type in __sizeof__() methods (#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

Lines changed: 5 additions & 7 deletions
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

Lines changed: 3 additions & 5 deletions
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

Lines changed: 7 additions & 6 deletions
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

Lines changed: 5 additions & 6 deletions
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

Lines changed: 4 additions & 6 deletions
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

Lines changed: 12 additions & 15 deletions
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

Lines changed: 4 additions & 6 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/clinic/_pickle.c.h

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy