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

# queue module depends on threading primitives
threading_helper.requires_working_threading(module=True)
Expand Down Expand Up @@ -1031,6 +1032,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 = q.__sizeof__()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few changes:

  1. Don't use ctypes (no support for that in wasi)
  2. Use support.calcobjsize
  3. Use sys.getsizeof() instead of calling __sizeof__ directly

for _ in range(1000):
q.put(object())
after = q.__sizeof__()
self.assertGreater(after, before)
ptr = ctypes.sizeof(ctypes.c_void_p)
self.assertEqual((after - before) % ptr, 0)

def test_reentrancy(self):
# bpo-14976: put() may be called reentrantly in an asynchronous
# callback.
Expand Down
13 changes: 13 additions & 0 deletions Modules/_queuemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,18 @@ simplequeue_traverse(PyObject *op, visitproc visit, void *arg)
Py_VISIT(Py_TYPE(self));
return 0;
}
static PyObject *
simplequeue_sizeof(PyObject *op, PyObject *Py_UNUSED(ignored))
{
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs a critical section (a lock for the queue). The simplest way is to use "Argument Clinic" like the other functions.

Something like:

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

Returns size in memory, in bytes.
[clinic start generated code]*/

static Py_ssize_t
simplequeue_sizeof_impl(simplequeueobject *self)
/*[clinic end generated code: output=3303f008eaa6a0a5 input=9b51620c76fc4507]*/

Then run make clinic to regenerate the bindings.

simplequeueobject *self = simplequeueobject_CAST(op);
Py_ssize_t size = Py_TYPE(self)->tp_basicsize;

if (self->buf.items != NULL) {
size += (Py_ssize_t)self->buf.items_cap * (Py_ssize_t)sizeof(PyObject *);
}

return PyLong_FromSsize_t(size);
}

/*[clinic input]
@classmethod
Expand Down Expand Up @@ -534,6 +546,7 @@ static PyMethodDef simplequeue_methods[] = {
_QUEUE_SIMPLEQUEUE_PUT_METHODDEF
_QUEUE_SIMPLEQUEUE_PUT_NOWAIT_METHODDEF
_QUEUE_SIMPLEQUEUE_QSIZE_METHODDEF
{"__sizeof__", (PyCFunction)simplequeue_sizeof, METH_NOARGS, NULL},
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll also need to change this to use the Argument Clinic methoddef (like above).

{"__class_getitem__", Py_GenericAlias,
METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
{NULL, NULL} /* sentinel */
Expand Down
Loading