# to ensure the Queue locks remain stable.
import itertools
import random
+import struct
import threading
import time
import unittest
from test.support import gc_collect, bigmemtest
from test.support import import_helper
from test.support import threading_helper
+from test import support
# queue module depends on threading primitives
threading_helper.requires_working_threading(module=True)
self.assertIs(self.type2test, self.queue.SimpleQueue)
self.assertIs(self.type2test, self.queue.SimpleQueue)
+ def test_simplequeue_sizeof(self):
+ q = self.type2test()
+ basesize = support.calcobjsize('?nnPnnP')
+ support.check_sizeof(self, q, basesize + struct.calcsize(8 * 'P'))
+ for _ in range(1000):
+ q.put(object())
+ support.check_sizeof(self, q, basesize + struct.calcsize(1024 * 'P'))
+
def test_reentrancy(self):
# bpo-14976: put() may be called reentrantly in an asynchronous
# callback.
--- /dev/null
+:mod:`queue`: Fix :meth:`!SimpleQueue.__sizeof__` computation.
return RingBuf_Len(&self->buf);
}
+/*[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)
+/*[clinic end generated code: output=58ce4e3bbc078fd4 input=a3a7f05c9616598f]*/
+{
+ Py_ssize_t res = sizeof(simplequeueobject);
+ res += self->buf.items_cap * sizeof(PyObject *);
+ return res;
+}
+
static int
queue_traverse(PyObject *m, visitproc visit, void *arg)
{
_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 */
exit:
return return_value;
}
-/*[clinic end generated code: output=1d3efe9df89997cf input=a9049054013a1b77]*/
+
+PyDoc_STRVAR(_queue_SimpleQueue___sizeof____doc__,
+"__sizeof__($self, /)\n"
+"--\n"
+"\n"
+"Returns size in memory, in bytes.");
+
+#define _QUEUE_SIMPLEQUEUE___SIZEOF___METHODDEF \
+ {"__sizeof__", (PyCFunction)_queue_SimpleQueue___sizeof__, METH_NOARGS, _queue_SimpleQueue___sizeof____doc__},
+
+static Py_ssize_t
+_queue_SimpleQueue___sizeof___impl(simplequeueobject *self);
+
+static PyObject *
+_queue_SimpleQueue___sizeof__(PyObject *self, PyObject *Py_UNUSED(ignored))
+{
+ PyObject *return_value = NULL;
+ Py_ssize_t _return_value;
+
+ Py_BEGIN_CRITICAL_SECTION(self);
+ _return_value = _queue_SimpleQueue___sizeof___impl((simplequeueobject *)self);
+ Py_END_CRITICAL_SECTION();
+ if ((_return_value == -1) && PyErr_Occurred()) {
+ goto exit;
+ }
+ return_value = PyLong_FromSsize_t(_return_value);
+
+exit:
+ return return_value;
+}
+/*[clinic end generated code: output=4af5d1b1ea31ac7d input=a9049054013a1b77]*/