From 8b095fbf7260f59607b2fc34be36ee813654be54 Mon Sep 17 00:00:00 2001 From: nocturn9x Date: Thu, 12 Nov 2020 22:45:00 +0100 Subject: [PATCH] Now exceptions sort of work --- giambio/_core.py | 59 +++++++++++++++++++++--------------------------- tests/server.py | 4 ++-- 2 files changed, 28 insertions(+), 35 deletions(-) diff --git a/giambio/_core.py b/giambio/_core.py index 8c628a4..da5c599 100644 --- a/giambio/_core.py +++ b/giambio/_core.py @@ -62,31 +62,23 @@ class AsyncScheduler: the loop's functionality only trough some fixed entry points, which in turn yield and give execution control to the loop itself.""" - while True: - if not self.selector.get_map() and not any( - [self.paused, self.tasks, self.event_waiting] - ): # If there is nothing to do, just exit - break - if not self.tasks: - if ( - self.paused - ): # If there are no actively running tasks, we try to schedule the asleep ones - try: + try: + while True: + if not self.selector.get_map() and not any( + [self.paused, self.tasks, self.event_waiting] + ): # If there is nothing to do, just exit + break + if not self.tasks: + if ( + self.paused + ): # If there are no actively running tasks, we try to schedule the asleep ones self.check_sleeping() - except BaseException as error: - self.current_task.exc = error - self.reschedule_parent(self.current_task) - if self.selector.get_map(): - try: + if self.selector.get_map(): self.check_io() - except BaseException as error: - self.current_task.exc = error - self.reschedule_parent(self.current_task) - while self.tasks: # While there are tasks to run - self.current_task = ( - self.tasks.popleft() - ) # Sets the currently running task - try: + while self.tasks: # While there are tasks to run + self.current_task = ( + self.tasks.popleft() + ) # Sets the currently running task if self.current_task.status == "cancel": # Deferred cancellation self.current_task.cancelled = True self.current_task.throw(CancelledError(self.current_task)) @@ -99,16 +91,17 @@ class AsyncScheduler: ) # Sneaky method call, thanks to David Beazley for this ;) if self.event_waiting: self.check_events() - except CancelledError as cancelled: - self.tasks.remove(cancelled.args[0]) # Remove the dead task - self.tasks.append(self.current_task) - except StopIteration as e: # Coroutine ends - self.current_task.result = e.args[0] if e.args else None - self.current_task.finished = True - self.reschedule_parent(self.current_task) - except BaseException as error: # Coroutine raised - self.current_task.exc = error - self.reschedule_parent(self.current_task) + except CancelledError as cancelled: + self.tasks.remove(cancelled.args[0]) # Remove the dead task + self.tasks.append(self.current_task) + except StopIteration as e: # Coroutine ends + self.current_task.result = e.args[0] if e.args else None + self.current_task.finished = True + self.reschedule_parent(self.current_task) + except BaseException as error: # Coroutine raised + self.current_task.exc = error + self.reschedule_parent(self.current_task) + self.join(self.current_task) def check_events(self): """Checks for ready or expired events and triggers them""" diff --git a/tests/server.py b/tests/server.py index 869c9fa..cf96515 100644 --- a/tests/server.py +++ b/tests/server.py @@ -41,5 +41,5 @@ async def echo_handler(sock: AsyncSocket, addr: tuple): if __name__ == "__main__": try: sched.start(server(("", 25001))) - except KeyboardInterrupt: # Exceptions propagate! - print("Exiting...") + except giambio.exceptions.GiambioError as wrapper: # Exceptions propagate! + print(f"Exiting due to a {type(wrapper.__cause__).__name__}")