summaryrefslogtreecommitdiffstatshomepage
path: root/extmod/uasyncio/task.py
diff options
context:
space:
mode:
Diffstat (limited to 'extmod/uasyncio/task.py')
-rw-r--r--extmod/uasyncio/task.py19
1 files changed, 16 insertions, 3 deletions
diff --git a/extmod/uasyncio/task.py b/extmod/uasyncio/task.py
index 1788cf0ed0..2420ab7193 100644
--- a/extmod/uasyncio/task.py
+++ b/extmod/uasyncio/task.py
@@ -130,13 +130,16 @@ class Task:
self.ph_rightmost_parent = None # Paring heap
def __iter__(self):
- if not hasattr(self, "waiting"):
+ if self.coro is self:
+ # Signal that the completed-task has been await'ed on.
+ self.waiting = None
+ elif not hasattr(self, "waiting"):
# Lazily allocated head of linked list of Tasks waiting on completion of this task.
self.waiting = TaskQueue()
return self
def __next__(self):
- if not self.coro:
+ if self.coro is self:
# Task finished, raise return value to caller so it can continue.
raise self.data
else:
@@ -147,7 +150,7 @@ class Task:
def cancel(self):
# Check if task is already finished.
- if self.coro is None:
+ if self.coro is self:
return False
# Can't cancel self (not supported yet).
if self is core.cur_task:
@@ -166,3 +169,13 @@ class Task:
core._task_queue.push_head(self)
self.data = core.CancelledError
return True
+
+ def throw(self, value):
+ # This task raised an exception which was uncaught; handle that now.
+ # Set the data because it was cleared by the main scheduling loop.
+ self.data = value
+ if not hasattr(self, "waiting"):
+ # Nothing await'ed on the task so call the exception handler.
+ core._exc_context["exception"] = value
+ core._exc_context["future"] = self
+ core.Loop.call_exception_handler(core._exc_context)