Skip to content

Commit 2c605d9

Browse files
author
Erlend E. Aasland
committed
Convert queue to use heap types
1 parent bffb137 commit 2c605d9

File tree

2 files changed

+46
-52
lines changed

2 files changed

+46
-52
lines changed

Modules/_queuemodule.c

Lines changed: 43 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
#include "Python.h"
2+
#include "structmember.h" // PyMemberDef
23
#include <stddef.h> // offsetof()
34

45
/*[clinic input]
56
module _queue
6-
class _queue.SimpleQueue "simplequeueobject *" "&PySimpleQueueType"
7+
class _queue.SimpleQueue "simplequeueobject *" "PySimpleQueueType"
78
[clinic start generated code]*/
8-
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=cf49af81bcbbbea6]*/
9+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=ec6b5cf35d0220ff]*/
910

10-
static PyTypeObject PySimpleQueueType; /* forward decl */
11+
static PyTypeObject *PySimpleQueueType = NULL;
1112

1213
static PyObject *EmptyError;
1314

@@ -25,6 +26,8 @@ typedef struct {
2526
static void
2627
simplequeue_dealloc(simplequeueobject *self)
2728
{
29+
PyTypeObject *tp = Py_TYPE(self);
30+
2831
PyObject_GC_UnTrack(self);
2932
if (self->lock != NULL) {
3033
/* Unlock the lock so it's safe to free it */
@@ -36,6 +39,7 @@ simplequeue_dealloc(simplequeueobject *self)
3639
if (self->weakreflist != NULL)
3740
PyObject_ClearWeakRefs((PyObject *) self);
3841
Py_TYPE(self)->tp_free(self);
42+
Py_DECREF(tp);
3943
}
4044

4145
static int
@@ -306,48 +310,26 @@ static PyMethodDef simplequeue_methods[] = {
306310
{NULL, NULL} /* sentinel */
307311
};
308312

313+
static struct PyMemberDef simplequeue_members[] = {
314+
{"__weaklistoffset__", T_PYSSIZET, offsetof(simplequeueobject, weakreflist), READONLY},
315+
{NULL},
316+
};
309317

310-
static PyTypeObject PySimpleQueueType = {
311-
PyVarObject_HEAD_INIT(NULL, 0)
312-
"_queue.SimpleQueue", /*tp_name*/
313-
sizeof(simplequeueobject), /*tp_basicsize*/
314-
0, /*tp_itemsize*/
315-
/* methods */
316-
(destructor)simplequeue_dealloc, /*tp_dealloc*/
317-
0, /*tp_vectorcall_offset*/
318-
0, /*tp_getattr*/
319-
0, /*tp_setattr*/
320-
0, /*tp_as_async*/
321-
0, /*tp_repr*/
322-
0, /*tp_as_number*/
323-
0, /*tp_as_sequence*/
324-
0, /*tp_as_mapping*/
325-
0, /*tp_hash*/
326-
0, /*tp_call*/
327-
0, /*tp_str*/
328-
0, /*tp_getattro*/
329-
0, /*tp_setattro*/
330-
0, /*tp_as_buffer*/
331-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
332-
| Py_TPFLAGS_HAVE_GC, /* tp_flags */
333-
simplequeue_new__doc__, /*tp_doc*/
334-
(traverseproc)simplequeue_traverse, /*tp_traverse*/
335-
0, /*tp_clear*/
336-
0, /*tp_richcompare*/
337-
offsetof(simplequeueobject, weakreflist), /*tp_weaklistoffset*/
338-
0, /*tp_iter*/
339-
0, /*tp_iternext*/
340-
simplequeue_methods, /*tp_methods*/
341-
0, /* tp_members */
342-
0, /* tp_getset */
343-
0, /* tp_base */
344-
0, /* tp_dict */
345-
0, /* tp_descr_get */
346-
0, /* tp_descr_set */
347-
0, /* tp_dictoffset */
348-
0, /* tp_init */
349-
0, /* tp_alloc */
350-
simplequeue_new /* tp_new */
318+
static PyType_Slot simplequeue_slots[] = {
319+
{Py_tp_dealloc, simplequeue_dealloc},
320+
{Py_tp_doc, (void *)simplequeue_new__doc__},
321+
{Py_tp_traverse, simplequeue_traverse},
322+
{Py_tp_members, simplequeue_members},
323+
{Py_tp_methods, simplequeue_methods},
324+
{Py_tp_new, simplequeue_new},
325+
{0, NULL},
326+
};
327+
328+
static PyType_Spec simplequeue_spec = {
329+
.name = "_queue.SimpleQueue",
330+
.basicsize = sizeof(simplequeueobject),
331+
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
332+
.slots = simplequeue_slots,
351333
};
352334

353335

@@ -385,15 +367,27 @@ PyInit__queue(void)
385367
"Exception raised by Queue.get(block=0)/get_nowait().",
386368
NULL, NULL);
387369
if (EmptyError == NULL)
388-
return NULL;
370+
goto error;
389371

390372
Py_INCREF(EmptyError);
391-
if (PyModule_AddObject(m, "Empty", EmptyError) < 0)
392-
return NULL;
373+
if (PyModule_AddObject(m, "Empty", EmptyError) < 0) {
374+
Py_DECREF(EmptyError);
375+
goto error;
376+
}
393377

394-
if (PyModule_AddType(m, &PySimpleQueueType) < 0) {
395-
return NULL;
378+
PySimpleQueueType = (PyTypeObject *)PyType_FromModuleAndSpec(m,
379+
&simplequeue_spec,
380+
NULL);
381+
if (PySimpleQueueType == NULL) {
382+
goto error;
383+
}
384+
if (PyModule_AddType(m, PySimpleQueueType) < 0) {
385+
goto error;
396386
}
397387

398388
return m;
389+
390+
error:
391+
Py_DECREF(m);
392+
return NULL;
399393
}

Modules/clinic/_queuemodule.c.h

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)