Skip to content

Commit 8692d26

Browse files
committed
extmod/asyncio: Make current_task raise exception when there is no task.
Matches CPython behaviour. Fixes issue adafruit#11530. Signed-off-by: Damien George <damien@micropython.org>
1 parent 8fdcc25 commit 8692d26

2 files changed

Lines changed: 16 additions & 0 deletions

File tree

‎extmod/asyncio/core.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ def run_until_complete(main_task=None):
164164
dt = max(0, ticks_diff(t.ph_key, ticks()))
165165
elif not _io_queue.map:
166166
# No tasks can be woken so finished running
167+
cur_task = None
167168
return
168169
# print('(poll {})'.format(dt), len(_io_queue.map))
169170
_io_queue.wait_io_event(dt)
@@ -188,6 +189,7 @@ def run_until_complete(main_task=None):
188189
assert t.data is None
189190
# This task is done, check if it's the main task and then loop should stop
190191
if t is main_task:
192+
cur_task = None
191193
if isinstance(er, StopIteration):
192194
return er.value
193195
raise er
@@ -242,6 +244,7 @@ async def _stopper():
242244
pass
243245

244246

247+
cur_task = None
245248
_stop_task = None
246249

247250

@@ -291,6 +294,8 @@ def get_event_loop(runq_len=0, waitq_len=0):
291294

292295

293296
def current_task():
297+
if cur_task is None:
298+
raise RuntimeError("no running event loop")
294299
return cur_task
295300

296301

‎tests/extmod/asyncio_current_task.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,15 @@ async def main():
1919
print(t is result[0])
2020

2121

22+
try:
23+
print(asyncio.current_task())
24+
except RuntimeError:
25+
print("RuntimeError")
26+
27+
2228
asyncio.run(main())
29+
30+
try:
31+
print(asyncio.current_task())
32+
except RuntimeError:
33+
print("RuntimeError")

0 commit comments

Comments
 (0)