Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
54 changes: 54 additions & 0 deletions Lib/test/test_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,60 @@ 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 list

# Add items within initial capacity (if applicable)
# For C SimpleQueue, initial capacity is 8 items
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 (list growth)

# 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__ may not account for underlying list
# (this is a known limitation of the Python implementation)
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 properly implement __sizeof__
# but we can at least verify it returns a positive number
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
18 changes: 18 additions & 0 deletions Modules/_queuemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,23 @@ _queue_SimpleQueue_qsize_impl(simplequeueobject *self)
return RingBuf_Len(&self->buf);
}

/*[clinic input]
@critical_section
_queue.SimpleQueue.__sizeof__ -> Py_ssize_t
Return size of queue in bytes, including underlying data structure.
[clinic start generated code]*/

static Py_ssize_t
_queue_SimpleQueue___sizeof___impl(simplequeueobject *self)
/*[clinic end generated code: output=58ce4e3bbc078fd4 input=7b9d000cdcb71b7d]*/
{
Py_ssize_t size = Py_TYPE(self)->tp_basicsize;
// Add size of the ring buffer items array
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 +551,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
30 changes: 29 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