Skip to content
Open
Show file tree
Hide file tree
Changes from 14 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
77 changes: 77 additions & 0 deletions Lib/test/test_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,51 @@ class C:
gc_collect() # For PyPy or other GCs.
self.assertIsNone(wr())

def test_sizeof(self):
q = self.q

empty_size = q.__sizeof__()
self.assertGreater(empty_size, 0, "Empty queue should have non-zero size")

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)

# 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 All @@ -1017,6 +1062,22 @@ def setUp(self):
self.type2test = self.queue._PySimpleQueue
super().setUp()

def test_equality(self):
# Test that SimpleQueue uses object identity, not value equality
q1 = self.type2test()
q2 = self.type2test()

# Different instances should not be equal
self.assertNotEqual(q1, q2)

# Same instance should be equal to itself
self.assertEqual(q1, q1)

# Even with same content, instances should not be equal
q1.put('test')
q2.put('test')
self.assertNotEqual(q1, q2)


@need_c_queue
class CSimpleQueueTest(BaseSimpleQueueTest, unittest.TestCase):
Expand All @@ -1031,6 +1092,22 @@ def test_is_default(self):
self.assertIs(self.type2test, self.queue.SimpleQueue)
self.assertIs(self.type2test, self.queue.SimpleQueue)

def test_equality(self):
# Test that SimpleQueue uses object identity, not value equality
q1 = self.type2test()
q2 = self.type2test()

# Different instances should not be equal
self.assertNotEqual(q1, q2)

# Same instance should be equal to itself
self.assertEqual(q1, q1)

# Even with same content, instances should not be equal
q1.put('test')
q2.put('test')
self.assertNotEqual(q1, q2)

def test_reentrancy(self):
# bpo-14976: put() may be called reentrantly in an asynchronous
# callback.
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 = _PyObject_SIZE(Py_TYPE(self));
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