8000 extmod/uasyncio: Fix sleep_ms/sleep so they don't crash if not await'd. · micropython/micropython@8c662b7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8c662b7

Browse files
committed
extmod/uasyncio: Fix sleep_ms/sleep so they don't crash if not await'd.
1 parent 5909e29 commit 8c662b7

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

extmod/uasyncio/core.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,16 @@ class TimeoutError(Exception):
2929
# "Yield" once, then raise StopIteration
3030
class SingletonGenerator:
3131
def __init__(self):
32-
self.state = 0
32+
self.state = None
3333
self.exc = StopIteration()
3434

3535
def __iter__(self):
3636
return self
3737

3838
def __next__(self):
39-
if self.state:
40-
self.state = 0
39+
if self.state is not None:
40+
_task_queue.push_sorted(cur_task, self.state)
41+
self.state = None
4142
return None
4243
else:
4344
self.exc.__traceback__ = None
@@ -47,8 +48,8 @@ def __next__(self):
4748
# Pause task execution for the given time (integer in milliseconds, uPy extension)
4849
# Use a SingletonGenerator to do it without allocating on the heap
4950
def sleep_ms(t, sgen=SingletonGenerator()):
50-
_task_queue.push_sorted(cur_task, ticks_add(ticks(), t))
51-
sgen.state = 1
51+
assert sgen.state is None
52+
sgen.state = ticks_add(ticks(), t)
5253
return sgen
5354

5455

0 commit comments

Comments
 (0)
0