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
Prev Previous commit
Next Next commit
Establish global state and add type & exc to it
  • Loading branch information
Erlend E. Aasland committed Nov 3, 2020
commit fd4243a1b952b37793355e84d6d5741be9954722
42 changes: 25 additions & 17 deletions Modules/_queuemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
#include "structmember.h" // PyMemberDef
#include <stddef.h> // offsetof()

/*[clinic input]
module _queue
class _queue.SimpleQueue "simplequeueobject *" "PySimpleQueueType"
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=ec6b5cf35d0220ff]*/

static PyTypeObject *PySimpleQueueType = NULL;
typedef struct {
PyTypeObject *SimpleQueueType;
PyObject *EmptyError;
} simplequeue_state;

static PyObject *EmptyError;
static simplequeue_state global_state;

static simplequeue_state *
simplequeue_get_state()
{
return &global_state;
}

typedef struct {
PyObject_HEAD
Expand All @@ -22,6 +24,11 @@ typedef struct {
PyObject *weakreflist;
} simplequeueobject;

/*[clinic input]
module _queue
class _queue.SimpleQueue "simplequeueobject *" "simplequeue_get_state()->SimpleQueueType"
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=ffce1ca094e64f3a]*/

static void
simplequeue_dealloc(simplequeueobject *self)
Expand Down Expand Up @@ -230,7 +237,7 @@ _queue_SimpleQueue_get_impl(simplequeueobject *self, int block,
}
if (r == PY_LOCK_FAILURE) {
/* Timed out */
PyErr_SetNone(EmptyError);
PyErr_SetNone(simplequeue_get_state()->EmptyError);
return NULL;
}
self->locked = 1;
Expand Down Expand Up @@ -356,32 +363,33 @@ PyMODINIT_FUNC
PyInit__queue(void)
{
PyObject *m;
simplequeue_state *state = simplequeue_get_state();

/* Create the module */
m = PyModule_Create(&queuemodule);
if (m == NULL)
return NULL;

EmptyError = PyErr_NewExceptionWithDoc(
state->EmptyError = PyErr_NewExceptionWithDoc(
"_queue.Empty",
"Exception raised by Queue.get(block=0)/get_nowait().",
NULL, NULL);
if (EmptyError == NULL)
if (state->EmptyError == NULL)
goto error;

Py_INCREF(EmptyError);
if (PyModule_AddObject(m, "Empty", EmptyError) < 0) {
Py_DECREF(EmptyError);
Py_INCREF(state->EmptyError);
if (PyModule_AddObject(m, "Empty", state->EmptyError) < 0) {
Py_DECREF(state->EmptyError);
goto error;
}

PySimpleQueueType = (PyTypeObject *)PyType_FromModuleAndSpec(m,
state->SimpleQueueType = (PyTypeObject *)PyType_FromModuleAndSpec(m,
&simplequeue_spec,
NULL);
if (PySimpleQueueType == NULL) {
if (state->SimpleQueueType == NULL) {
goto error;
}
if (PyModule_AddType(m, PySimpleQueueType) < 0) {
if (PyModule_AddType(m, state->SimpleQueueType) < 0) {
goto error;
}

Expand Down
6 changes: 3 additions & 3 deletions Modules/clinic/_queuemodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.