From adfc2f9be57ec4b8af2b868c650c1ad091e7d293 Mon Sep 17 00:00:00 2001 From: Ewald de Wit Date: Wed, 1 Dec 2021 20:41:05 +0100 Subject: [PATCH] Patch task for Python>=3.7 again, reverts #57, fixes #64 --- nest_asyncio.py | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/nest_asyncio.py b/nest_asyncio.py index bb96b86..4b0fde6 100644 --- a/nest_asyncio.py +++ b/nest_asyncio.py @@ -176,6 +176,17 @@ def _check_running(self): def _patch_task(): """Patch the Task's step and enter/leave methods to make it reentrant.""" + + def step(task, exc=None): + curr_task = curr_tasks.get(task._loop) + try: + step_orig(task, exc) + finally: + if curr_task is None: + curr_tasks.pop(task._loop, None) + else: + curr_tasks[task._loop] = curr_task + Task = asyncio.Task if sys.version_info >= (3, 7, 0): @@ -188,18 +199,9 @@ def leave_task(loop, task): asyncio.tasks._enter_task = enter_task asyncio.tasks._leave_task = leave_task curr_tasks = asyncio.tasks._current_tasks + step_orig = Task._Task__step + Task._Task__step = step else: - - def step(task, exc=None): - curr_task = curr_tasks.get(task._loop) - try: - step_orig(task, exc) - finally: - if curr_task is None: - curr_tasks.pop(task._loop, None) - else: - curr_tasks[task._loop] = curr_task - curr_tasks = Task._current_tasks step_orig = Task._step Task._step = step