Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
12 changes: 12 additions & 0 deletions Lib/test/test_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from test.support import gc_collect, bigmemtest
from test.support import import_helper
from test.support import threading_helper
import sys
from test import support

# queue module depends on threading primitives
threading_helper.requires_working_threading(module=True)
Expand Down Expand Up @@ -1031,6 +1033,16 @@ def test_is_default(self):
self.assertIs(self.type2test, self.queue.SimpleQueue)
self.assertIs(self.type2test, self.queue.SimpleQueue)

def test_simplequeue_sizeof_reflects_buffer_growth(self):
q = self.type2test()
before = sys.getsizeof(q)
for _ in range(1000):
q.put(object())
after = sys.getsizeof(q)
self.assertGreater(after, before)
ptr = support.calcobjsize("P") - support.calcobjsize("")
self.assertEqual((after - before) % ptr, 0)

def test_reentrancy(self):
# bpo-14976: put() may be called reentrantly in an asynchronous
# callback.
Expand Down
23 changes: 23 additions & 0 deletions Modules/_queuemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,28 @@ simplequeue_traverse(PyObject *op, visitproc visit, void *arg)
return 0;
}

/*[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)
{
Py_ssize_t size = Py_TYPE(self)->tp_basicsize;
PyObject **items = self->buf.items;
Py_ssize_t items_cap = self->buf.items_cap;

if (items != NULL) {
size += items_cap * (Py_ssize_t)sizeof(void *);
}

return size;
}
/*[clinic end generated code: output=58ce4e3bbc078fd4 input=a3a7f05c9616598f]*/

/*[clinic input]
@classmethod
_queue.SimpleQueue.__new__ as simplequeue_new
Expand Down Expand Up @@ -534,6 +556,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
32 changes: 31 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