mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Issue #28452: Remove _asyncio._init_module function
This commit is contained in:
commit
810d62d73e
2 changed files with 64 additions and 82 deletions
|
@ -431,21 +431,6 @@ def _copy_future_state(source, dest):
|
||||||
dest.set_result(result)
|
dest.set_result(result)
|
||||||
|
|
||||||
|
|
||||||
try:
|
|
||||||
import _asyncio
|
|
||||||
except ImportError:
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
_asyncio._init_module(
|
|
||||||
traceback.extract_stack,
|
|
||||||
events.get_event_loop,
|
|
||||||
_future_repr_info,
|
|
||||||
InvalidStateError,
|
|
||||||
CancelledError)
|
|
||||||
|
|
||||||
Future = _asyncio.Future
|
|
||||||
|
|
||||||
|
|
||||||
def _chain_future(source, destination):
|
def _chain_future(source, destination):
|
||||||
"""Chain two futures so that when one completes, so does the other.
|
"""Chain two futures so that when one completes, so does the other.
|
||||||
|
|
||||||
|
@ -496,3 +481,11 @@ def wrap_future(future, *, loop=None):
|
||||||
new_future = loop.create_future()
|
new_future = loop.create_future()
|
||||||
_chain_future(future, new_future)
|
_chain_future(future, new_future)
|
||||||
return new_future
|
return new_future
|
||||||
|
|
||||||
|
|
||||||
|
try:
|
||||||
|
import _asyncio
|
||||||
|
except ImportError:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
Future = _asyncio.Future
|
||||||
|
|
|
@ -7,7 +7,6 @@ _Py_IDENTIFIER(call_soon);
|
||||||
|
|
||||||
|
|
||||||
/* State of the _asyncio module */
|
/* State of the _asyncio module */
|
||||||
static int _asynciomod_ready;
|
|
||||||
static PyObject *traceback_extract_stack;
|
static PyObject *traceback_extract_stack;
|
||||||
static PyObject *asyncio_get_event_loop;
|
static PyObject *asyncio_get_event_loop;
|
||||||
static PyObject *asyncio_repr_info_func;
|
static PyObject *asyncio_repr_info_func;
|
||||||
|
@ -19,19 +18,6 @@ static PyObject *asyncio_CancelledError;
|
||||||
static PyObject* new_future_iter(PyObject *fut);
|
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 {
|
typedef enum {
|
||||||
STATE_PENDING,
|
STATE_PENDING,
|
||||||
STATE_CANCELLED,
|
STATE_CANCELLED,
|
||||||
|
@ -108,10 +94,6 @@ FutureObj_init(FutureObj *fut, PyObject *args, PyObject *kwds)
|
||||||
PyObject *res = NULL;
|
PyObject *res = NULL;
|
||||||
_Py_IDENTIFIER(get_debug);
|
_Py_IDENTIFIER(get_debug);
|
||||||
|
|
||||||
if (_AsyncioMod_EnsureState()) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|$O", kwlist, &loop)) {
|
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|$O", kwlist, &loop)) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@ -218,10 +200,6 @@ PyDoc_STRVAR(pydoc_exception,
|
||||||
static PyObject *
|
static PyObject *
|
||||||
FutureObj_exception(FutureObj *fut, PyObject *arg)
|
FutureObj_exception(FutureObj *fut, PyObject *arg)
|
||||||
{
|
{
|
||||||
if (_AsyncioMod_EnsureState()) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fut->fut_state == STATE_CANCELLED) {
|
if (fut->fut_state == STATE_CANCELLED) {
|
||||||
PyErr_SetString(asyncio_CancelledError, "");
|
PyErr_SetString(asyncio_CancelledError, "");
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -251,10 +229,6 @@ PyDoc_STRVAR(pydoc_set_result,
|
||||||
static PyObject *
|
static PyObject *
|
||||||
FutureObj_set_result(FutureObj *fut, PyObject *res)
|
FutureObj_set_result(FutureObj *fut, PyObject *res)
|
||||||
{
|
{
|
||||||
if (_AsyncioMod_EnsureState()) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fut->fut_state != STATE_PENDING) {
|
if (fut->fut_state != STATE_PENDING) {
|
||||||
PyErr_SetString(asyncio_InvalidStateError, "invalid state");
|
PyErr_SetString(asyncio_InvalidStateError, "invalid state");
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -282,10 +256,6 @@ FutureObj_set_exception(FutureObj *fut, PyObject *exc)
|
||||||
{
|
{
|
||||||
PyObject *exc_val = NULL;
|
PyObject *exc_val = NULL;
|
||||||
|
|
||||||
if (_AsyncioMod_EnsureState()) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fut->fut_state != STATE_PENDING) {
|
if (fut->fut_state != STATE_PENDING) {
|
||||||
PyErr_SetString(asyncio_InvalidStateError, "invalid state");
|
PyErr_SetString(asyncio_InvalidStateError, "invalid state");
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -949,59 +919,75 @@ new_future_iter(PyObject *fut)
|
||||||
|
|
||||||
/*********************** Module **************************/
|
/*********************** Module **************************/
|
||||||
|
|
||||||
PyDoc_STRVAR(module_doc, "asyncio speedups.\n");
|
static int
|
||||||
|
init_module(void)
|
||||||
PyObject *
|
|
||||||
_init_module(PyObject *self, PyObject *args)
|
|
||||||
{
|
{
|
||||||
PyObject *extract_stack;
|
PyObject *module = NULL;
|
||||||
PyObject *get_event_loop;
|
|
||||||
PyObject *repr_info_func;
|
|
||||||
PyObject *invalidStateError;
|
|
||||||
PyObject *cancelledError;
|
|
||||||
|
|
||||||
if (!PyArg_UnpackTuple(args, "_init_module", 5, 5,
|
module = PyImport_ImportModule("traceback");
|
||||||
&extract_stack,
|
if (module == NULL) {
|
||||||
&get_event_loop,
|
return -1;
|
||||||
&repr_info_func,
|
}
|
||||||
&invalidStateError,
|
// new reference
|
||||||
&cancelledError)) {
|
traceback_extract_stack = PyObject_GetAttrString(module, "extract_stack");
|
||||||
return NULL;
|
if (traceback_extract_stack == NULL) {
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
Py_DECREF(module);
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
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(extract_stack);
|
asyncio_InvalidStateError = PyObject_GetAttrString(module,
|
||||||
Py_XSETREF(traceback_extract_stack, extract_stack);
|
"InvalidStateError");
|
||||||
|
if (asyncio_InvalidStateError == NULL) {
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
Py_INCREF(get_event_loop);
|
asyncio_CancelledError = PyObject_GetAttrString(module, "CancelledError");
|
||||||
Py_XSETREF(asyncio_get_event_loop, get_event_loop);
|
if (asyncio_CancelledError == NULL) {
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
Py_INCREF(repr_info_func);
|
Py_DECREF(module);
|
||||||
Py_XSETREF(asyncio_repr_info_func, repr_info_func);
|
return 0;
|
||||||
|
|
||||||
Py_INCREF(invalidStateError);
|
fail:
|
||||||
Py_XSETREF(asyncio_InvalidStateError, invalidStateError);
|
Py_CLEAR(traceback_extract_stack);
|
||||||
|
Py_CLEAR(asyncio_get_event_loop);
|
||||||
Py_INCREF(cancelledError);
|
Py_CLEAR(asyncio_repr_info_func);
|
||||||
Py_XSETREF(asyncio_CancelledError, cancelledError);
|
Py_CLEAR(asyncio_InvalidStateError);
|
||||||
|
Py_CLEAR(asyncio_CancelledError);
|
||||||
_asynciomod_ready = 1;
|
Py_CLEAR(module);
|
||||||
|
return -1;
|
||||||
Py_RETURN_NONE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static struct PyMethodDef asynciomod_methods[] = {
|
PyDoc_STRVAR(module_doc, "Accelerator module for asyncio");
|
||||||
{"_init_module", _init_module, METH_VARARGS, NULL},
|
|
||||||
{NULL, NULL}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
static struct PyModuleDef _asynciomodule = {
|
static struct PyModuleDef _asynciomodule = {
|
||||||
PyModuleDef_HEAD_INIT, /* m_base */
|
PyModuleDef_HEAD_INIT, /* m_base */
|
||||||
"_asyncio", /* m_name */
|
"_asyncio", /* m_name */
|
||||||
module_doc, /* m_doc */
|
module_doc, /* m_doc */
|
||||||
-1, /* m_size */
|
-1, /* m_size */
|
||||||
asynciomod_methods, /* m_methods */
|
NULL, /* m_methods */
|
||||||
NULL, /* m_slots */
|
NULL, /* m_slots */
|
||||||
NULL, /* m_traverse */
|
NULL, /* m_traverse */
|
||||||
NULL, /* m_clear */
|
NULL, /* m_clear */
|
||||||
|
@ -1012,6 +998,9 @@ static struct PyModuleDef _asynciomodule = {
|
||||||
PyMODINIT_FUNC
|
PyMODINIT_FUNC
|
||||||
PyInit__asyncio(void)
|
PyInit__asyncio(void)
|
||||||
{
|
{
|
||||||
|
if (init_module() < 0) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
if (PyType_Ready(&FutureType) < 0) {
|
if (PyType_Ready(&FutureType) < 0) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue