/* State of the _asyncio module */
-static int _asynciomod_ready;
static PyObject *traceback_extract_stack;
static PyObject *asyncio_get_event_loop;
static PyObject *asyncio_repr_info_func;
static PyObject* new_future_iter(PyObject *fut);
-/* make sure module state is initialized and ready to be used. */
-static int
-_AsyncioMod_EnsureState(void)
-{
- if (!_asynciomod_ready) {
- PyErr_SetString(PyExc_RuntimeError,
- "_asyncio module wasn't properly initialized");
- return -1;
- }
- return 0;
-}
-
-
typedef enum {
STATE_PENDING,
STATE_CANCELLED,
PyObject *res = NULL;
_Py_IDENTIFIER(get_debug);
- if (_AsyncioMod_EnsureState()) {
- return -1;
- }
-
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|$O", kwlist, &loop)) {
return -1;
}
static PyObject *
FutureObj_exception(FutureObj *fut, PyObject *arg)
{
- if (_AsyncioMod_EnsureState()) {
- return NULL;
- }
-
if (fut->fut_state == STATE_CANCELLED) {
PyErr_SetString(asyncio_CancelledError, "");
return NULL;
static PyObject *
FutureObj_set_result(FutureObj *fut, PyObject *res)
{
- if (_AsyncioMod_EnsureState()) {
- return NULL;
- }
-
if (fut->fut_state != STATE_PENDING) {
PyErr_SetString(asyncio_InvalidStateError, "invalid state");
return NULL;
{
PyObject *exc_val = NULL;
- if (_AsyncioMod_EnsureState()) {
- return NULL;
- }
-
if (fut->fut_state != STATE_PENDING) {
PyErr_SetString(asyncio_InvalidStateError, "invalid state");
return NULL;
/*********************** Module **************************/
-PyDoc_STRVAR(module_doc, "asyncio speedups.\n");
-
-PyObject *
-_init_module(PyObject *self, PyObject *args)
+static int
+init_module(void)
{
- PyObject *extract_stack;
- PyObject *get_event_loop;
- PyObject *repr_info_func;
- PyObject *invalidStateError;
- PyObject *cancelledError;
-
- if (!PyArg_UnpackTuple(args, "_init_module", 5, 5,
- &extract_stack,
- &get_event_loop,
- &repr_info_func,
- &invalidStateError,
- &cancelledError)) {
- return NULL;
- }
+ PyObject *module = NULL;
- Py_INCREF(extract_stack);
- Py_XSETREF(traceback_extract_stack, extract_stack);
+ module = PyImport_ImportModule("traceback");
+ if (module == NULL) {
+ return -1;
+ }
+ // new reference
+ traceback_extract_stack = PyObject_GetAttrString(module, "extract_stack");
+ if (traceback_extract_stack == NULL) {
+ goto fail;
+ }
+ Py_DECREF(module);
- Py_INCREF(get_event_loop);
- Py_XSETREF(asyncio_get_event_loop, get_event_loop);
+ module = PyImport_ImportModule("asyncio.events");
+ if (module == NULL) {
+ goto fail;
+ }
+ asyncio_get_event_loop = PyObject_GetAttrString(module, "get_event_loop");
+ if (asyncio_get_event_loop == NULL) {
+ goto fail;
+ }
+ Py_DECREF(module);
- Py_INCREF(repr_info_func);
- Py_XSETREF(asyncio_repr_info_func, repr_info_func);
+ module = PyImport_ImportModule("asyncio.futures");
+ if (module == NULL) {
+ goto fail;
+ }
+ asyncio_repr_info_func = PyObject_GetAttrString(module,
+ "_future_repr_info");
+ if (asyncio_repr_info_func == NULL) {
+ goto fail;
+ }
- Py_INCREF(invalidStateError);
- Py_XSETREF(asyncio_InvalidStateError, invalidStateError);
+ asyncio_InvalidStateError = PyObject_GetAttrString(module,
+ "InvalidStateError");
+ if (asyncio_InvalidStateError == NULL) {
+ goto fail;
+ }
- Py_INCREF(cancelledError);
- Py_XSETREF(asyncio_CancelledError, cancelledError);
+ asyncio_CancelledError = PyObject_GetAttrString(module, "CancelledError");
+ if (asyncio_CancelledError == NULL) {
+ goto fail;
+ }
- _asynciomod_ready = 1;
+ Py_DECREF(module);
+ return 0;
- Py_RETURN_NONE;
+fail:
+ Py_CLEAR(traceback_extract_stack);
+ Py_CLEAR(asyncio_get_event_loop);
+ Py_CLEAR(asyncio_repr_info_func);
+ Py_CLEAR(asyncio_InvalidStateError);
+ Py_CLEAR(asyncio_CancelledError);
+ Py_CLEAR(module);
+ return -1;
}
-static struct PyMethodDef asynciomod_methods[] = {
- {"_init_module", _init_module, METH_VARARGS, NULL},
- {NULL, NULL}
-};
-
+PyDoc_STRVAR(module_doc, "Accelerator module for asyncio");
static struct PyModuleDef _asynciomodule = {
PyModuleDef_HEAD_INIT, /* m_base */
"_asyncio", /* m_name */
module_doc, /* m_doc */
-1, /* m_size */
- asynciomod_methods, /* m_methods */
+ NULL, /* m_methods */
NULL, /* m_slots */
NULL, /* m_traverse */
NULL, /* m_clear */
PyMODINIT_FUNC
PyInit__asyncio(void)
{
+ if (init_module() < 0) {
+ return NULL;
+ }
if (PyType_Ready(&FutureType) < 0) {
return NULL;
}