https://github.com/python/cpython/commit/f3e069a7ab8b0594508c998da88937e3aab46451
commit: f3e069a7ab8b0594508c998da88937e3aab46451
branch: main
author: Aniket <[email protected]>
committer: kumaraditya303 <[email protected]>
date: 2026-01-08T12:40:25Z
summary:

gh-140025: Fix `queue.SimpleQueue.__sizeof__()` to return correct size (#143137)

files:
A Misc/NEWS.d/next/Library/2026-01-02-09-32-43.gh-issue-140025.zOX58_.rst
M Lib/test/test_queue.py
M Modules/_queuemodule.c
M Modules/clinic/_queuemodule.c.h

diff --git a/Lib/test/test_queue.py b/Lib/test/test_queue.py
index c855fb8fe2b05a..f2898de469e349 100644
--- a/Lib/test/test_queue.py
+++ b/Lib/test/test_queue.py
@@ -2,6 +2,7 @@
 # to ensure the Queue locks remain stable.
 import itertools
 import random
+import struct
 import threading
 import time
 import unittest
@@ -9,6 +10,7 @@
 from test.support import gc_collect, bigmemtest
 from test.support import import_helper
 from test.support import threading_helper
+from test import support
 
 # queue module depends on threading primitives
 threading_helper.requires_working_threading(module=True)
@@ -1031,6 +1033,14 @@ def test_is_default(self):
         self.assertIs(self.type2test, self.queue.SimpleQueue)
         self.assertIs(self.type2test, self.queue.SimpleQueue)
 
+    def test_simplequeue_sizeof(self):
+        q = self.type2test()
+        basesize = support.calcobjsize('?nnPnnP')
+        support.check_sizeof(self, q, basesize + struct.calcsize(8 * 'P'))
+        for _ in range(1000):
+            q.put(object())
+        support.check_sizeof(self, q, basesize + struct.calcsize(1024 * 'P'))
+
     def test_reentrancy(self):
         # bpo-14976: put() may be called reentrantly in an asynchronous
         # callback.
diff --git 
a/Misc/NEWS.d/next/Library/2026-01-02-09-32-43.gh-issue-140025.zOX58_.rst 
b/Misc/NEWS.d/next/Library/2026-01-02-09-32-43.gh-issue-140025.zOX58_.rst
new file mode 100644
index 00000000000000..cb5458f28af667
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-01-02-09-32-43.gh-issue-140025.zOX58_.rst
@@ -0,0 +1 @@
+:mod:`queue`: Fix :meth:`!SimpleQueue.__sizeof__` computation.
diff --git a/Modules/_queuemodule.c b/Modules/_queuemodule.c
index 01235c77bd7db8..a45959346bc1f2 100644
--- a/Modules/_queuemodule.c
+++ b/Modules/_queuemodule.c
@@ -500,6 +500,22 @@ _queue_SimpleQueue_qsize_impl(simplequeueobject *self)
     return RingBuf_Len(&self->buf);
 }
 
+/*[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)
+/*[clinic end generated code: output=58ce4e3bbc078fd4 input=a3a7f05c9616598f]*/
+{
+    Py_ssize_t res = sizeof(simplequeueobject);
+    res += self->buf.items_cap * sizeof(PyObject *);
+    return res;
+}
+
 static int
 queue_traverse(PyObject *m, visitproc visit, void *arg)
 {
@@ -534,6 +550,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..c9482f40acb9d4 100644
--- a/Modules/clinic/_queuemodule.c.h
+++ b/Modules/clinic/_queuemodule.c.h
@@ -358,4 +358,34 @@ _queue_SimpleQueue_qsize(PyObject *self, PyObject 
*Py_UNUSED(ignored))
 exit:
     return return_value;
 }
-/*[clinic end generated code: output=1d3efe9df89997cf input=a9049054013a1b77]*/
+
+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;
+}
+/*[clinic end generated code: output=4af5d1b1ea31ac7d input=a9049054013a1b77]*/

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to