Skip to content

Commit f3e069a

Browse files
authored
gh-140025: Fix queue.SimpleQueue.__sizeof__() to return correct size (#143137)
1 parent c07e5ec commit f3e069a

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed

Lib/test/test_queue.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
# to ensure the Queue locks remain stable.
33
import itertools
44
import random
5+
import struct
56
import threading
67
import time
78
import unittest
89
import weakref
910
from test.support import gc_collect, bigmemtest
1011
from test.support import import_helper
1112
from test.support import threading_helper
13+
from test import support
1214

1315
# queue module depends on threading primitives
1416
threading_helper.requires_working_threading(module=True)
@@ -1031,6 +1033,14 @@ def test_is_default(self):
10311033
self.assertIs(self.type2test, self.queue.SimpleQueue)
10321034
self.assertIs(self.type2test, self.queue.SimpleQueue)
10331035

1036+
def test_simplequeue_sizeof(self):
1037+
q = self.type2test()
1038+
basesize = support.calcobjsize('?nnPnnP')
1039+
support.check_sizeof(self, q, basesize + struct.calcsize(8 * 'P'))
1040+
for _ in range(1000):
1041+
q.put(object())
1042+
support.check_sizeof(self, q, basesize + struct.calcsize(1024 * 'P'))
1043+
10341044
def test_reentrancy(self):
10351045
# bpo-14976: put() may be called reentrantly in an asynchronous
10361046
# callback.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:mod:`queue`: Fix :meth:`!SimpleQueue.__sizeof__` computation.

Modules/_queuemodule.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,22 @@ _queue_SimpleQueue_qsize_impl(simplequeueobject *self)
500500
return RingBuf_Len(&self->buf);
501501
}
502502

503+
/*[clinic input]
504+
@critical_section
505+
_queue.SimpleQueue.__sizeof__ -> Py_ssize_t
506+
507+
Returns size in memory, in bytes.
508+
[clinic start generated code]*/
509+
510+
static Py_ssize_t
511+
_queue_SimpleQueue___sizeof___impl(simplequeueobject *self)
512+
/*[clinic end generated code: output=58ce4e3bbc078fd4 input=a3a7f05c9616598f]*/
513+
{
514+
Py_ssize_t res = sizeof(simplequeueobject);
515+
res += self->buf.items_cap * sizeof(PyObject *);
516+
return res;
517+
}
518+
503519
static int
504520
queue_traverse(PyObject *m, visitproc visit, void *arg)
505521
{
@@ -534,6 +550,7 @@ static PyMethodDef simplequeue_methods[] = {
534550
_QUEUE_SIMPLEQUEUE_PUT_METHODDEF
535551
_QUEUE_SIMPLEQUEUE_PUT_NOWAIT_METHODDEF
536552
_QUEUE_SIMPLEQUEUE_QSIZE_METHODDEF
553+
_QUEUE_SIMPLEQUEUE___SIZEOF___METHODDEF
537554
{"__class_getitem__", Py_GenericAlias,
538555
METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
539556
{NULL, NULL} /* sentinel */

Modules/clinic/_queuemodule.c.h

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

0 commit comments

Comments
 (0)