@@ -2047,8 +2047,6 @@ static int task_call_step_soon(asyncio_state *state, TaskObj *, PyObject *);
20472047static PyObject * task_wakeup (TaskObj * , PyObject * );
20482048static PyObject * task_step (asyncio_state * , TaskObj * , PyObject * );
20492049static int task_eager_start (asyncio_state * state , TaskObj * task );
2050- static inline void clear_ts_asyncio_running_task (PyObject * loop );
2051- static inline void set_ts_asyncio_running_task (PyObject * loop , PyObject * task );
20522050
20532051/* ----- Task._step wrapper */
20542052
@@ -2230,7 +2228,7 @@ enter_task(PyObject *loop, PyObject *task)
22302228 PyExc_RuntimeError ,
22312229 "Cannot enter into task %R while another " \
22322230 "task %R is being executed." ,
2233- task , item , NULL );
2231+ task , ts -> asyncio_running_task ? ts -> asyncio_running_task : Py_None , NULL );
22342232 return -1 ;
22352233 }
22362234
@@ -2248,7 +2246,7 @@ leave_task(PyObject *loop, PyObject *task)
22482246 PyExc_RuntimeError ,
22492247 "Cannot enter into task %R while another " \
22502248 "task %R is being executed." ,
2251- task , item , NULL );
2249+ task , ts -> asyncio_running_task ? ts -> asyncio_running_task : Py_None , NULL );
22522250 return -1 ;
22532251 }
22542252 Py_CLEAR (ts -> asyncio_running_task );
@@ -3415,20 +3413,20 @@ task_step(asyncio_state *state, TaskObj *task, PyObject *exc)
34153413{
34163414 PyObject * res ;
34173415
3418- if (enter_task (state , task -> task_loop , (PyObject * )task ) < 0 ) {
3416+ if (enter_task (task -> task_loop , (PyObject * )task ) < 0 ) {
34193417 return NULL ;
34203418 }
34213419
34223420 res = task_step_impl (state , task , exc );
34233421
34243422 if (res == NULL ) {
34253423 PyObject * exc = PyErr_GetRaisedException ();
3426- leave_task (state , task -> task_loop , (PyObject * )task );
3424+ leave_task (task -> task_loop , (PyObject * )task );
34273425 _PyErr_ChainExceptions1 (exc );
34283426 return NULL ;
34293427 }
34303428 else {
3431- if (leave_task (state , task -> task_loop , (PyObject * )task ) < 0 ) {
3429+ if (leave_task (task -> task_loop , (PyObject * )task ) < 0 ) {
34323430 Py_DECREF (res );
34333431 return NULL ;
34343432 }
@@ -3442,7 +3440,7 @@ static int
34423440task_eager_start (asyncio_state * state , TaskObj * task )
34433441{
34443442 assert (task != NULL );
3445- PyObject * prevtask = swap_current_task (state , (PyObject * )task );
3443+ PyObject * prevtask = swap_current_task (task -> task_loop , (PyObject * )task );
34463444 if (prevtask == NULL ) {
34473445 return -1 ;
34483446 }
@@ -3471,7 +3469,7 @@ task_eager_start(asyncio_state *state, TaskObj *task)
34713469 Py_DECREF (stepres );
34723470 }
34733471
3474- PyObject * curtask = swap_current_task (state , task -> task_loop , prevtask );
3472+ PyObject * curtask = swap_current_task (task -> task_loop , prevtask );
34753473 Py_DECREF (prevtask );
34763474 if (curtask == NULL ) {
34773475 retval = -1 ;
@@ -3783,8 +3781,7 @@ static PyObject *
37833781_asyncio__enter_task_impl (PyObject * module , PyObject * loop , PyObject * task )
37843782/*[clinic end generated code: output=a22611c858035b73 input=de1b06dca70d8737]*/
37853783{
3786- asyncio_state * state = get_asyncio_state (module );
3787- if (enter_task (state , loop , task ) < 0 ) {
3784+ if (enter_task (loop , task ) < 0 ) {
37883785 return NULL ;
37893786 }
37903787 Py_RETURN_NONE ;
@@ -3808,8 +3805,7 @@ static PyObject *
38083805_asyncio__leave_task_impl (PyObject * module , PyObject * loop , PyObject * task )
38093806/*[clinic end generated code: output=0ebf6db4b858fb41 input=51296a46313d1ad8]*/
38103807{
3811- asyncio_state * state = get_asyncio_state (module );
3812- if (leave_task (state , loop , task ) < 0 ) {
3808+ if (leave_task (loop , task ) < 0 ) {
38133809 return NULL ;
38143810 }
38153811 Py_RETURN_NONE ;
@@ -3833,7 +3829,7 @@ _asyncio__swap_current_task_impl(PyObject *module, PyObject *loop,
38333829 PyObject * task )
38343830/*[clinic end generated code: output=9f88de958df74c7e input=c9c72208d3d38b6c]*/
38353831{
3836- return swap_current_task (get_asyncio_state ( module ), loop , task );
3832+ return swap_current_task (loop , task );
38373833}
38383834
38393835
@@ -3851,7 +3847,6 @@ _asyncio_current_task_impl(PyObject *module, PyObject *loop)
38513847/*[clinic end generated code: output=fe15ac331a7f981a input=58910f61a5627112]*/
38523848{
38533849 PyObject * ret ;
3854- asyncio_state * state = get_asyncio_state (module );
38553850
38563851 if (loop == Py_None ) {
38573852 loop = _asyncio_get_running_loop_impl (module );
@@ -3869,8 +3864,22 @@ _asyncio_current_task_impl(PyObject *module, PyObject *loop)
38693864 Py_DECREF (loop );
38703865 return ret ;
38713866 }
3872- _PyEval_StopTheWorld (ts -> base .interp );
38733867
3868+ ret = Py_None ;
3869+
3870+ PyInterpreterState * interp = ts -> base .interp ;
3871+ _PyEval_StopTheWorld (interp );
3872+ _Py_FOR_EACH_TSTATE_BEGIN (interp , p ) {
3873+ ts = (_PyThreadStateImpl * )p ;
3874+ if (ts -> asyncio_running_loop == loop ) {
3875+ ret = Py_XNewRef (ts -> asyncio_running_task );
3876+ goto exit ;
3877+ }
3878+ }
3879+ exit :
3880+ _PyEval_StartTheWorld (interp );
3881+ _Py_FOR_EACH_TSTATE_END (interp );
3882+ Py_DECREF (loop );
38743883 return ret ;
38753884}
38763885
@@ -4135,7 +4144,6 @@ module_traverse(PyObject *mod, visitproc visit, void *arg)
41354144
41364145 Py_VISIT (state -> non_asyncio_tasks );
41374146 Py_VISIT (state -> eager_tasks );
4138- Py_VISIT (state -> current_tasks );
41394147 Py_VISIT (state -> iscoroutine_typecache );
41404148
41414149 Py_VISIT (state -> context_kwname );
@@ -4166,7 +4174,6 @@ module_clear(PyObject *mod)
41664174
41674175 Py_CLEAR (state -> non_asyncio_tasks );
41684176 Py_CLEAR (state -> eager_tasks );
4169- Py_CLEAR (state -> current_tasks );
41704177 Py_CLEAR (state -> iscoroutine_typecache );
41714178
41724179 Py_CLEAR (state -> context_kwname );
@@ -4196,11 +4203,6 @@ module_init(asyncio_state *state)
41964203 goto fail ;
41974204 }
41984205
4199- state -> current_tasks = PyDict_New ();
4200- if (state -> current_tasks == NULL ) {
4201- goto fail ;
4202- }
4203-
42044206 state -> iscoroutine_typecache = PySet_New (NULL );
42054207 if (state -> iscoroutine_typecache == NULL ) {
42064208 goto fail ;
@@ -4333,11 +4335,6 @@ module_exec(PyObject *mod)
43334335 return -1 ;
43344336 }
43354337
4336- if (PyModule_AddObjectRef (mod , "_current_tasks" , state -> current_tasks ) < 0 ) {
4337- return -1 ;
4338- }
4339-
4340-
43414338 return 0 ;
43424339}
43434340
0 commit comments