Skip to content
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions Lib/test/test_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,59 @@ class C:
gc_collect() # For PyPy or other GCs.
self.assertIsNone(wr())

def test_sizeof(self):
# Test that __sizeof__() accounts for underlying data structure
q = self.q

# Get the size of an empty queue
empty_size = q.__sizeof__()
self.assertGreater(empty_size, 0, "Empty queue should have non-zero size")

# Size should include basic object structure
# For C implementation, this includes the ring buffer array
# For Python implementation, this includes the underlying deque

# Add items within initial capacity (if applicable)
for i in range(8):
q.put(object())

size_after_8 = q.__sizeof__()
# Size may or may not change depending on implementation
# C implementation: no change (still within initial ring buffer capacity)
# Python implementation: may change (deque growth, but __sizeof__ may not reflect it)

# Add one more item to potentially trigger growth
q.put(object()) # Now 9 items

size_after_9 = q.__sizeof__()
self.assertGreaterEqual(size_after_9, size_after_8,
"Size should not decrease when adding items")

# Test with a larger number of items
large_q = self.type2test()
for i in range(1000):
large_q.put(object())

large_size = large_q.__sizeof__()

# For C implementation, size should grow with capacity
# For Python implementation, __sizeof__ will not account for underlying deque
# (this is documented behavior on CPython; PyPy doesn't support __sizeof__ at all)
if self.__class__.__name__ == 'CSimpleQueueTest':
# This is the C implementation
self.assertGreater(large_size, empty_size,
"C SimpleQueue with many items should be larger than empty queue")

# Verify size is reasonable (should be proportional to capacity)
# For very large queues, size should be significantly larger
self.assertGreater(large_size, empty_size * 2,
"Large C SimpleQueue should be at least 2x size of empty queue")
else:
# This is the Python implementation
# The Python implementation doesn't override __sizeof__
# so it only accounts for the object itself, not the underlying deque
self.assertGreater(large_size, 0, "Python SimpleQueue should have positive size")


class PySimpleQueueTest(BaseSimpleQueueTest, unittest.TestCase):

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix ``queue.SimpleQueue.__sizeof__()`` computation.
15 changes: 15 additions & 0 deletions Modules/_queuemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,20 @@ _queue_SimpleQueue_qsize_impl(simplequeueobject *self)
return RingBuf_Len(&self->buf);
}

/*[clinic input]
@critical_section
_queue.SimpleQueue.__sizeof__ -> Py_ssize_t
[clinic start generated code]*/

static Py_ssize_t
_queue_SimpleQueue___sizeof___impl(simplequeueobject *self)
/*[clinic end generated code: output=58ce4e3bbc078fd4 input=40a793cdf1c78c30]*/
{
Py_ssize_t size = Py_TYPE(self)->tp_basicsize;
size += self->buf.items_cap * sizeof(PyObject *);
return size;
}

static int
queue_traverse(PyObject *m, visitproc visit, void *arg)
{
Expand Down Expand Up @@ -534,6 +548,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 */
Expand Down
31 changes: 30 additions & 1 deletion Modules/clinic/_queuemodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading