Skip to content

Commit fd4243a

Browse files
author
Erlend E. Aasland
committed
Establish global state and add type & exc to it
1 parent 2c605d9 commit fd4243a

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed

Modules/_queuemodule.c

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@
22
#include "structmember.h" // PyMemberDef
33
#include <stddef.h> // offsetof()
44

5-
/*[clinic input]
6-
module _queue
7-
class _queue.SimpleQueue "simplequeueobject *" "PySimpleQueueType"
8-
[clinic start generated code]*/
9-
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=ec6b5cf35d0220ff]*/
10-
11-
static PyTypeObject *PySimpleQueueType = NULL;
5+
typedef struct {
6+
PyTypeObject *SimpleQueueType;
7+
PyObject *EmptyError;
8+
} simplequeue_state;
129

13-
static PyObject *EmptyError;
10+
static simplequeue_state global_state;
1411

12+
static simplequeue_state *
13+
simplequeue_get_state()
14+
{
15+
return &global_state;
16+
}
1517

1618
typedef struct {
1719
PyObject_HEAD
@@ -22,6 +24,11 @@ typedef struct {
2224
PyObject *weakreflist;
2325
} simplequeueobject;
2426

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

2633
static void
2734
simplequeue_dealloc(simplequeueobject *self)
@@ -230,7 +237,7 @@ _queue_SimpleQueue_get_impl(simplequeueobject *self, int block,
230237
}
231238
if (r == PY_LOCK_FAILURE) {
232239
/* Timed out */
233-
PyErr_SetNone(EmptyError);
240+
PyErr_SetNone(simplequeue_get_state()->EmptyError);
234241
return NULL;
235242
}
236243
self->locked = 1;
@@ -356,32 +363,33 @@ PyMODINIT_FUNC
356363
PyInit__queue(void)
357364
{
358365
PyObject *m;
366+
simplequeue_state *state = simplequeue_get_state();
359367

360368
/* Create the module */
361369
m = PyModule_Create(&queuemodule);
362370
if (m == NULL)
363371
return NULL;
364372

365-
EmptyError = PyErr_NewExceptionWithDoc(
373+
state->EmptyError = PyErr_NewExceptionWithDoc(
366374
"_queue.Empty",
367375
"Exception raised by Queue.get(block=0)/get_nowait().",
368376
NULL, NULL);
369-
if (EmptyError == NULL)
377+
if (state->EmptyError == NULL)
370378
goto error;
371379

372-
Py_INCREF(EmptyError);
373-
if (PyModule_AddObject(m, "Empty", EmptyError) < 0) {
374-
Py_DECREF(EmptyError);
380+
Py_INCREF(state->EmptyError);
381+
if (PyModule_AddObject(m, "Empty", state->EmptyError) < 0) {
382+
Py_DECREF(state->EmptyError);
375383
goto error;
376384
}
377385

378-
PySimpleQueueType = (PyTypeObject *)PyType_FromModuleAndSpec(m,
386+
state->SimpleQueueType = (PyTypeObject *)PyType_FromModuleAndSpec(m,
379387
&simplequeue_spec,
380388
NULL);
381-
if (PySimpleQueueType == NULL) {
389+
if (state->SimpleQueueType == NULL) {
382390
goto error;
383391
}
384-
if (PyModule_AddType(m, PySimpleQueueType) < 0) {
392+
if (PyModule_AddType(m, state->SimpleQueueType) < 0) {
385393
goto error;
386394
}
387395

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)