Skip to content

Commit b247be5

Browse files
committed
Introduce, use a 'never' object
This can be used in cases where `await SOMETHING` is required, but the task cannot be put back on the queue like `await sleep(0)` would do. This passes the test and the docs build (locally)
1 parent f22e949 commit b247be5

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

asyncio/core.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,27 @@ def __next__(self):
7070
raise self.exc
7171

7272

73+
class _Never:
74+
def __init__(self):
75+
self.state = None
76+
self.exc = StopIteration()
77+
78+
def __iter__(self):
79+
return self
80+
81+
def __await__(self):
82+
return self
83+
84+
def __next__(self):
85+
if self.state is not None:
86+
self.state = None
87+
return None
88+
else:
89+
self.exc.__traceback__ = None
90+
raise self.exc
91+
92+
_never = _Never()
93+
7394
# Pause task execution for the given time (integer in milliseconds, uPy extension)
7495
# Use a SingletonGenerator to do it without allocating on the heap
7596
def sleep_ms(t, sgen=SingletonGenerator()):

asyncio/event.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ async def wait(self):
6262
self.waiting.push_head(core.cur_task)
6363
# Set calling task's data to the event's queue so it can be removed if needed
6464
core.cur_task.data = self.waiting
65-
yield
65+
core._never.state = False
66+
await core._never
6667
return True
6768

6869

asyncio/lock.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ async def acquire(self):
6969
# Set calling task's data to the lock's queue so it can be removed if needed
7070
core.cur_task.data = self.waiting
7171
try:
72-
yield
72+
core._never.state = False
73+
await core._never
7374
except core.CancelledError as er:
7475
if self.state == core.cur_task:
7576
# Cancelled while pending on resume, schedule next waiting Task

0 commit comments

Comments
 (0)