diff --git a/Lib/test/test_queue.py b/Lib/test/test_queue.py index c855fb8fe2b05a..a67f5c51cc00a5 100644 --- a/Lib/test/test_queue.py +++ b/Lib/test/test_queue.py @@ -9,6 +9,8 @@ from test.support import gc_collect, bigmemtest from test.support import import_helper from test.support import threading_helper +import sys +from test import support # queue module depends on threading primitives threading_helper.requires_working_threading(module=True) @@ -1031,6 +1033,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 = sys.getsizeof(q) + for _ in range(1000): + q.put(object()) + after = sys.getsizeof(q) + self.assertGreater(after, before) + ptr = support.calcobjsize("P") - support.calcobjsize("") + self.assertEqual((after - before) % ptr, 0) + def test_reentrancy(self): # bpo-14976: put() may be called reentrantly in an asynchronous # callback. 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. diff --git a/Modules/_queuemodule.c b/Modules/_queuemodule.c index 01235c77bd7db8..7b17b7ad295b38 100644 --- a/Modules/_queuemodule.c +++ b/Modules/_queuemodule.c @@ -239,6 +239,28 @@ simplequeue_traverse(PyObject *op, visitproc visit, void *arg) return 0; } +/*[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) +{ + Py_ssize_t size = Py_TYPE(self)->tp_basicsize; + PyObject **items = self->buf.items; + Py_ssize_t items_cap = self->buf.items_cap; + + if (items != NULL) { + size += items_cap * (Py_ssize_t)sizeof(void *); + } + + return size; +} +/*[clinic end generated code: output=58ce4e3bbc078fd4 input=a3a7f05c9616598f]*/ + /*[clinic input] @classmethod _queue.SimpleQueue.__new__ as simplequeue_new @@ -534,6 +556,7 @@ static PyMethodDef simplequeue_methods[] = { _QUEUE_SIMPLEQUEUE_PUT_METHODDEF _QUEUE_SIMPLEQUEUE_PUT_NOWAIT_METHODDEF _QUEUE_SIMPLEQUEUE_QSIZE_METHODDEF + _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]*/