8000 bpo-40077: Convert _queuemodule to use heap types by erlend-aasland · Pull Request #23136 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-40077: Convert _queuemodule to use heap types #23136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 7, 2020
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
Migrate from global to module state
  • Loading branch information
Erlend E. Aasland committed Nov 3, 2020
commit 5966894d25ff4609985951059f928ebd3141746d
54 changes: 31 additions & 23 deletions Modules/_queuemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ typedef struct {
PyObject *EmptyError;
} simplequeue_state;

static simplequeue_state global_state;

static simplequeue_state *
simplequeue_get_state()
simplequeue_get_state(PyObject *module)
{
return &global_state;
simplequeue_state *state = PyModule_GetState(module);
assert(state);
return state;
}
static struct PyModuleDef queuemodule;
#define simplequeue_get_state_by_type(tp) \
(simplequeue_get_state(_PyType_GetModuleByDef(type, &queuemodule)))

typedef struct {
PyObject_HEAD
Expand All @@ -26,9 +29,9 @@ typedef struct {

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

static void
simplequeue_dealloc(simplequeueobject *self)
Expand Down Expand Up @@ -166,6 +169,9 @@ simplequeue_pop_item(simplequeueobject *self)

/*[clinic input]
_queue.SimpleQueue.get

cls: defining_class
/
block: bool = True
timeout: object = None

Expand All @@ -182,9 +188,9 @@ in that case).
[clinic start generated code]*/

static PyObject *
_queue_SimpleQueue_get_impl(simplequeueobject *self, int block,
PyObject *timeout)
/*[clinic end generated code: output=ec82a7157dcccd1a input=4bf691f9f01fa297]*/
_queue_SimpleQueue_get_impl(simplequeueobject *self, PyTypeObject *cls,
int block, PyObject *timeout)
/*[clinic end generated code: output=1969aefa7db63666 input=5fc4d56b9a54757e]*/
{
_PyTime_t endtime = 0;
_PyTime_t timeout_val;
Expand Down Expand Up @@ -236,8 +242,10 @@ _queue_SimpleQueue_get_impl(simplequeueobject *self, int block,
return NULL;
}
if (r == PY_LOCK_FAILURE) {
PyObject *module = PyType_GetModule(cls);
simplequeue_state *state = simplequeue_get_state(module);
/* Timed out */
PyErr_SetNone(simplequeue_get_state()->EmptyError);
PyErr_SetNone(state->EmptyError);
return NULL;
}
self->locked = 1;
Expand All @@ -262,17 +270,21 @@ _queue_SimpleQueue_get_impl(simplequeueobject *self, int block,
/*[clinic input]
_queue.SimpleQueue.get_nowait

cls: defining_class
/

Remove and return an item from the queue without blocking.

Only get an item if one is immediately available. Otherwise
raise the Empty exception.
[clinic start generated code]*/

static PyObject *
_queue_SimpleQueue_get_nowait_impl(simplequeueobject *self)
/*[clinic end generated code: output=a89731a75dbe4937 input=6fe5102db540a1b9]*/
_queue_SimpleQueue_get_nowait_impl(simplequeueobject *self,
PyTypeObject *cls)
/*[clinic end generated code: output=620c58e2750f8b8a input=842f732bf04216d3]*/
{
return _queue_SimpleQueue_get_impl(self, 0, Py_None);
return _queue_SimpleQueue_get_impl(self, cls, 0, Py_None);
}

/*[clinic input]
Expand Down Expand Up @@ -347,29 +359,25 @@ PyDoc_STRVAR(queue_module_doc,
This module is an implementation detail, please do not use it directly.");

static struct PyModuleDef queuemodule = {
PyModuleDef_HEAD_INIT,
"_queue",
queue_module_doc,
-1,
NULL,
NULL,
NULL,
NULL,
NULL
.m_base = PyModuleDef_HEAD_INIT,
.m_name = "_queue",
.m_doc = queue_module_doc,
.m_size = sizeof(simplequeue_state),
};


PyMODINIT_FUNC
PyInit__queue(void)
{
PyObject *m;
simplequeue_state *state = simplequeue_get_state();
simplequeue_state *state;

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

state = simplequeue_get_state(m);
state->EmptyError = PyErr_NewExceptionWithDoc(
"_queue.Empty",
"Exception raised by Queue.get(block=0)/get_nowait().",
Expand Down
58 changes: 27 additions & 31 deletions Modules/clinic/_queuemodule.c.h

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

0