bpo-34755: Add few minor optimizations in _asynciomodule.c. (GH-9455)

This commit is contained in:
Serhiy Storchaka 2018-09-21 09:11:32 +03:00 committed by GitHub
parent 91e6c8717b
commit fb3e9c00ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 120 additions and 162 deletions

View File

@ -10,6 +10,7 @@ module _asyncio
/* identifiers used from some functions */
_Py_IDENTIFIER(__asyncio_running_event_loop__);
_Py_IDENTIFIER(_asyncio_future_blocking);
_Py_IDENTIFIER(add_done_callback);
_Py_IDENTIFIER(_all_tasks_compat);
_Py_IDENTIFIER(call_soon);
@ -22,7 +23,6 @@ _Py_IDENTIFIER(throw);
/* State of the _asyncio module */
static PyObject *asyncio_mod;
static PyObject *inspect_isgenerator;
static PyObject *traceback_extract_stack;
static PyObject *asyncio_get_event_loop_policy;
static PyObject *asyncio_future_repr_info_func;
@ -1304,13 +1304,8 @@ FutureObj_repr(FutureObj *fut)
return NULL;
}
PyObject *rstr = NULL;
PyObject *type_name = PyObject_GetAttrString((PyObject*)Py_TYPE(fut),
"__name__");
if (type_name != NULL) {
rstr = PyUnicode_FromFormat("<%S %U>", type_name, rinfo_s);
Py_DECREF(type_name);
}
PyObject *rstr = PyUnicode_FromFormat("<%s %U>",
_PyType_Name(Py_TYPE(fut)), rinfo_s);
Py_DECREF(rinfo_s);
return rstr;
}
@ -1326,7 +1321,6 @@ FutureObj_finalize(FutureObj *fut)
PyObject *error_type, *error_value, *error_traceback;
PyObject *context;
PyObject *type_name;
PyObject *message = NULL;
PyObject *func;
@ -1344,14 +1338,8 @@ FutureObj_finalize(FutureObj *fut)
goto finally;
}
type_name = PyObject_GetAttrString((PyObject*)Py_TYPE(fut), "__name__");
if (type_name == NULL) {
goto finally;
}
message = PyUnicode_FromFormat(
"%S exception was never retrieved", type_name);
Py_DECREF(type_name);
"%s exception was never retrieved", _PyType_Name(Py_TYPE(fut)));
if (message == NULL) {
goto finally;
}
@ -1543,7 +1531,7 @@ static PyObject *
FutureIter_send(futureiterobject *self, PyObject *unused)
{
/* Future.__iter__ doesn't care about values that are pushed to the
* generator, it just returns "self.result().
* generator, it just returns self.result().
*/
return FutureIter_iternext(self);
}
@ -2702,7 +2690,10 @@ set_exception:
goto different_loop;
}
if (fut->fut_blocking) {
if (!fut->fut_blocking) {
goto yield_insteadof_yf;
}
fut->fut_blocking = 0;
/* result.add_done_callback(task._wakeup) */
@ -2735,26 +2726,21 @@ set_exception:
Py_RETURN_NONE;
}
else {
goto yield_insteadof_yf;
/* Check if `result` is None */
if (result == Py_None) {
/* Bare yield relinquishes control for one event loop iteration. */
if (task_call_step_soon(task, NULL)) {
goto fail;
}
return result;
}
/* Check if `result` is a Future-compatible object */
o = PyObject_GetAttrString(result, "_asyncio_future_blocking");
if (o == NULL) {
if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
PyErr_Clear();
}
else {
if (_PyObject_LookupAttrId(result, &PyId__asyncio_future_blocking, &o) < 0) {
goto fail;
}
}
else {
if (o == Py_None) {
Py_DECREF(o);
}
else {
if (o != NULL && o != Py_None) {
/* `result` is a Future-compatible object */
PyObject *wrapper;
PyObject *res;
@ -2774,14 +2760,15 @@ set_exception:
Py_DECREF(oloop);
goto different_loop;
}
else {
Py_DECREF(oloop);
if (!blocking) {
goto yield_insteadof_yf;
}
if (blocking) {
/* result._asyncio_future_blocking = False */
if (PyObject_SetAttrString(
result, "_asyncio_future_blocking", Py_False) == -1) {
if (_PyObject_SetAttrId(
result, &PyId__asyncio_future_blocking, Py_False) == -1) {
goto fail;
}
@ -2831,34 +2818,14 @@ set_exception:
Py_RETURN_NONE;
}
else {
goto yield_insteadof_yf;
}
}
}
/* Check if `result` is None */
if (result == Py_None) {
/* Bare yield relinquishes control for one event loop iteration. */
if (task_call_step_soon(task, NULL)) {
goto fail;
}
return result;
}
Py_XDECREF(o);
/* Check if `result` is a generator */
o = PyObject_CallFunctionObjArgs(inspect_isgenerator, result, NULL);
if (o == NULL) {
/* An exception in inspect.isgenerator */
res = PyObject_IsInstance(result, (PyObject*)&PyGen_Type);
if (res < 0) {
goto fail;
}
res = PyObject_IsTrue(o);
Py_DECREF(o);
if (res == -1) {
/* An exception while checking if 'val' is True */
goto fail;
}
if (res == 1) {
if (res) {
/* `result` is a generator */
o = task_set_error_soon(
task, PyExc_RuntimeError,
@ -3237,7 +3204,6 @@ static void
module_free(void *m)
{
Py_CLEAR(asyncio_mod);
Py_CLEAR(inspect_isgenerator);
Py_CLEAR(traceback_extract_stack);
Py_CLEAR(asyncio_future_repr_info_func);
Py_CLEAR(asyncio_get_event_loop_policy);
@ -3278,15 +3244,10 @@ module_init(void)
}
context_kwname = PyTuple_New(1);
context_kwname = Py_BuildValue("(s)", "context");
if (context_kwname == NULL) {
goto fail;
}
PyObject *context_str = PyUnicode_FromString("context");
if (context_str == NULL) {
goto fail;
}
PyTuple_SET_ITEM(context_kwname, 0, context_str);
#define WITH_MOD(NAME) \
Py_CLEAR(module); \
@ -3319,9 +3280,6 @@ module_init(void)
WITH_MOD("asyncio.coroutines")
GET_MOD_ATTR(asyncio_iscoroutine_func, "iscoroutine")
WITH_MOD("inspect")
GET_MOD_ATTR(inspect_isgenerator, "isgenerator")
WITH_MOD("traceback")
GET_MOD_ATTR(traceback_extract_stack, "extract_stack")