From 4d03baf997b08be15b458b22c6bc10fea2d2bcae Mon Sep 17 00:00:00 2001 From: Aniket Singh Yadav Date: Wed, 24 Dec 2025 17:19:11 +0530 Subject: [PATCH 1/4] implement __sizeof__ for SimpleQueue to account for ring-buffer storage --- Lib/test/test_queue.py | 11 +++++++++++ Modules/_queuemodule.c | 13 +++++++++++++ 2 files changed, 24 insertions(+) diff --git a/Lib/test/test_queue.py b/Lib/test/test_queue.py index c855fb8fe2b05a..d53787e98e35ea 100644 --- a/Lib/test/test_queue.py +++ b/Lib/test/test_queue.py @@ -9,6 +9,7 @@ from test.support import gc_collect, bigmemtest from test.support import import_helper from test.support import threading_helper +import ctypes # queue module depends on threading primitives threading_helper.requires_working_threading(module=True) @@ -1031,6 +1032,16 @@ def test_is_default(self): self.assertIs(self.type2test, self.queue.SimpleQueue) self.assertIs(self.type2test, self.queue.SimpleQueue) + def test_simplequeue_sizeof_reflects_buffer_growth(self): + q = self.type2test() + before = q.__sizeof__() + for _ in range(1000): + q.put(object()) + after = q.__sizeof__() + self.assertGreater(after, before) + ptr = ctypes.sizeof(ctypes.c_void_p) + self.assertEqual((after - before) % ptr, 0) + def test_reentrancy(self): # bpo-14976: put() may be called reentrantly in an asynchronous # callback. diff --git a/Modules/_queuemodule.c b/Modules/_queuemodule.c index 01235c77bd7db8..614f566649b144 100644 --- a/Modules/_queuemodule.c +++ b/Modules/_queuemodule.c @@ -238,6 +238,18 @@ simplequeue_traverse(PyObject *op, visitproc visit, void *arg) Py_VISIT(Py_TYPE(self)); return 0; } +static PyObject * +simplequeue_sizeof(PyObject *op, PyObject *Py_UNUSED(ignored)) +{ + simplequeueobject *self = simplequeueobject_CAST(op); + Py_ssize_t size = Py_TYPE(self)->tp_basicsize; + + if (self->buf.items != NULL) { + size += (Py_ssize_t)self->buf.items_cap * (Py_ssize_t)sizeof(PyObject *); + } + + return PyLong_FromSsize_t(size); +} /*[clinic input] @classmethod @@ -534,6 +546,7 @@ static PyMethodDef simplequeue_methods[] = { _QUEUE_SIMPLEQUEUE_PUT_METHODDEF _QUEUE_SIMPLEQUEUE_PUT_NOWAIT_METHODDEF _QUEUE_SIMPLEQUEUE_QSIZE_METHODDEF + {"__sizeof__", (PyCFunction)simplequeue_sizeof, METH_NOARGS, NULL}, {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")}, {NULL, NULL} /* sentinel */ From b2b05eab9f846abb1b20fefe1ad7763c00023167 Mon Sep 17 00:00:00 2001 From: Aniket Singh Yadav Date: Wed, 24 Dec 2025 20:04:55 +0530 Subject: [PATCH 2/4] implement __sizeof__ for SimpleQueue to account for ring-buffer storage --- Lib/test/test_queue.py | 9 +++++---- Modules/_queuemodule.c | 24 +++++++++++++++++------- Modules/clinic/_queuemodule.c.h | 32 +++++++++++++++++++++++++++++++- 3 files changed, 53 insertions(+), 12 deletions(-) diff --git a/Lib/test/test_queue.py b/Lib/test/test_queue.py index d53787e98e35ea..3734817eafe5b7 100644 --- a/Lib/test/test_queue.py +++ b/Lib/test/test_queue.py @@ -9,7 +9,8 @@ from test.support import gc_collect, bigmemtest from test.support import import_helper from test.support import threading_helper -import ctypes +import sys +from test import support # queue module depends on threading primitives threading_helper.requires_working_threading(module=True) @@ -1034,12 +1035,12 @@ def test_is_default(self): def test_simplequeue_sizeof_reflects_buffer_growth(self): q = self.type2test() - before = q.__sizeof__() + before = sys.getsizeof(q) for _ in range(1000): q.put(object()) - after = q.__sizeof__() + after = sys.getsizeof(q) self.assertGreater(after, before) - ptr = ctypes.sizeof(ctypes.c_void_p) + ptr = support.calcobjsize(1) - support.calcobjsize(0) self.assertEqual((after - before) % ptr, 0) def test_reentrancy(self): diff --git a/Modules/_queuemodule.c b/Modules/_queuemodule.c index 614f566649b144..7b17b7ad295b38 100644 --- a/Modules/_queuemodule.c +++ b/Modules/_queuemodule.c @@ -238,18 +238,28 @@ simplequeue_traverse(PyObject *op, visitproc visit, void *arg) Py_VISIT(Py_TYPE(self)); return 0; } -static PyObject * -simplequeue_sizeof(PyObject *op, PyObject *Py_UNUSED(ignored)) + +/*[clinic input] +@critical_section +_queue.SimpleQueue.__sizeof__ -> Py_ssize_t + +Returns size in memory, in bytes. +[clinic start generated code]*/ + +static Py_ssize_t +_queue_SimpleQueue___sizeof___impl(simplequeueobject *self) { - simplequeueobject *self = simplequeueobject_CAST(op); Py_ssize_t size = Py_TYPE(self)->tp_basicsize; + PyObject **items = self->buf.items; + Py_ssize_t items_cap = self->buf.items_cap; - if (self->buf.items != NULL) { - size += (Py_ssize_t)self->buf.items_cap * (Py_ssize_t)sizeof(PyObject *); + if (items != NULL) { + size += items_cap * (Py_ssize_t)sizeof(void *); } - return PyLong_FromSsize_t(size); + return size; } +/*[clinic end generated code: output=58ce4e3bbc078fd4 input=a3a7f05c9616598f]*/ /*[clinic input] @classmethod @@ -546,7 +556,7 @@ static PyMethodDef simplequeue_methods[] = { _QUEUE_SIMPLEQUEUE_PUT_METHODDEF _QUEUE_SIMPLEQUEUE_PUT_NOWAIT_METHODDEF _QUEUE_SIMPLEQUEUE_QSIZE_METHODDEF - {"__sizeof__", (PyCFunction)simplequeue_sizeof, METH_NOARGS, NULL}, + _QUEUE_SIMPLEQUEUE___SIZEOF___METHODDEF {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")}, {NULL, NULL} /* sentinel */ diff --git a/Modules/clinic/_queuemodule.c.h b/Modules/clinic/_queuemodule.c.h index 1751d68716ba5f..97b5542241fb0c 100644 --- a/Modules/clinic/_queuemodule.c.h +++ b/Modules/clinic/_queuemodule.c.h @@ -9,6 +9,36 @@ preserve #include "pycore_critical_section.h"// Py_BEGIN_CRITICAL_SECTION() #include "pycore_modsupport.h" // _PyArg_NoKeywords() +PyDoc_STRVAR(_queue_SimpleQueue___sizeof____doc__, +"__sizeof__($self, /)\n" +"--\n" +"\n" +"Returns size in memory, in bytes."); + +#define _QUEUE_SIMPLEQUEUE___SIZEOF___METHODDEF \ + {"__sizeof__", (PyCFunction)_queue_SimpleQueue___sizeof__, METH_NOARGS, _queue_SimpleQueue___sizeof____doc__}, + +static Py_ssize_t +_queue_SimpleQueue___sizeof___impl(simplequeueobject *self); + +static PyObject * +_queue_SimpleQueue___sizeof__(PyObject *self, PyObject *Py_UNUSED(ignored)) +{ + PyObject *return_value = NULL; + Py_ssize_t _return_value; + + Py_BEGIN_CRITICAL_SECTION(self); + _return_value = _queue_SimpleQueue___sizeof___impl((simplequeueobject *)self); + Py_END_CRITICAL_SECTION(); + if ((_return_value == -1) && PyErr_Occurred()) { + goto exit; + } + return_value = PyLong_FromSsize_t(_return_value); + +exit: + return return_value; +} + PyDoc_STRVAR(simplequeue_new__doc__, "SimpleQueue()\n" "--\n" @@ -358,4 +388,4 @@ _queue_SimpleQueue_qsize(PyObject *self, PyObject *Py_UNUSED(ignored)) exit: return return_value; } -/*[clinic end generated code: output=1d3efe9df89997cf input=a9049054013a1b77]*/ +/*[clinic end generated code: output=e6a05dc21a1e9fca input=a9049054013a1b77]*/ From 205fd00ed473c664304ed75766fbb03cfab9e851 Mon Sep 17 00:00:00 2001 From: Aniket Singh Yadav Date: Wed, 24 Dec 2025 20:25:01 +0530 Subject: [PATCH 3/4] implement __sizeof__ for SimpleQueue to account for ring-buffer storage --- Lib/test/test_queue.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_queue.py b/Lib/test/test_queue.py index 3734817eafe5b7..a67f5c51cc00a5 100644 --- a/Lib/test/test_queue.py +++ b/Lib/test/test_queue.py @@ -1040,7 +1040,7 @@ def test_simplequeue_sizeof_reflects_buffer_growth(self): q.put(object()) after = sys.getsizeof(q) self.assertGreater(after, before) - ptr = support.calcobjsize(1) - support.calcobjsize(0) + ptr = support.calcobjsize("P") - support.calcobjsize("") self.assertEqual((after - before) % ptr, 0) def test_reentrancy(self): From 7efcdd59968604edfe346a9666c53ac69e6c55a3 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Fri, 26 Dec 2025 09:47:06 +0000 Subject: [PATCH 4/4] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2025-12-26-09-47-05.gh-issue-140025.zOX58_.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2025-12-26-09-47-05.gh-issue-140025.zOX58_.rst diff --git a/Misc/NEWS.d/next/Library/2025-12-26-09-47-05.gh-issue-140025.zOX58_.rst b/Misc/NEWS.d/next/Library/2025-12-26-09-47-05.gh-issue-140025.zOX58_.rst new file mode 100644 index 00000000000000..f086e70746946a --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-12-26-09-47-05.gh-issue-140025.zOX58_.rst @@ -0,0 +1 @@ +Fix ``queue.SimpleQueue.__sizeof__()`` computation.