From 529b01d4b3123e882a5a956fa3f98e60d60d4d87 Mon Sep 17 00:00:00 2001 From: James Ward Date: Fri, 10 Nov 2023 19:36:14 -0500 Subject: [PATCH 1/4] chore: move CancelledError and InvalidStateError to tasks --- asyncio/core.py | 12 ++++++------ asyncio/task.py | 12 ++++++++++++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/asyncio/core.py b/asyncio/core.py index e31c1ce..3e08464 100644 --- a/asyncio/core.py +++ b/asyncio/core.py @@ -24,17 +24,17 @@ except: from .task import TaskQueue, Task +# Depending on the version of CircuitPython, these errors may exist in the build-in C code +# even if _asyncio exists +try: + from _asyncio import CancelledError, InvalidStateError +except: + from .task import CancelledError, InvalidStateError ################################################################################ # Exceptions -class CancelledError(BaseException): - """Injected into a task when calling `Task.cancel()`""" - - pass - - class TimeoutError(Exception): """Raised when waiting for a task longer than the specified timeout.""" diff --git a/asyncio/task.py b/asyncio/task.py index 2e3a6db..d9d6743 100644 --- a/asyncio/task.py +++ b/asyncio/task.py @@ -21,6 +21,18 @@ from . import core +class CancelledError(BaseException): + """Injected into a task when calling `Task.cancel()`""" + + pass + + +class InvalidStateError(Exception): + """Can be raised in situations like setting a result value for a task object that already has a result value set.""" + + pass + + # pairing-heap meld of 2 heaps; O(1) def ph_meld(h1, h2): if h1 is None: From 516dc86e16234ad52bbfbb3e788f1665898589ba Mon Sep 17 00:00:00 2001 From: James Ward Date: Mon, 13 Nov 2023 19:36:49 -0500 Subject: [PATCH 2/4] chore: clearer comment and tighter error handling --- asyncio/core.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/asyncio/core.py b/asyncio/core.py index 3e08464..0201116 100644 --- a/asyncio/core.py +++ b/asyncio/core.py @@ -21,14 +21,15 @@ # Import TaskQueue and Task, preferring built-in C code over Python code try: from _asyncio import TaskQueue, Task -except: +except ImportError: from .task import TaskQueue, Task -# Depending on the version of CircuitPython, these errors may exist in the build-in C code -# even if _asyncio exists +# Depending on the release of CircuitPython these errors may or may not +# exist in the C implementation of `_asyncio`. However, when they +# do exist, must be preferred over the Python code. try: from _asyncio import CancelledError, InvalidStateError -except: +except (ImportError, AttributeError): from .task import CancelledError, InvalidStateError ################################################################################ From 3fd605969cdb2a96f96d5fb82f73dabfeba91e86 Mon Sep 17 00:00:00 2001 From: James Ward Date: Mon, 13 Nov 2023 19:45:04 -0500 Subject: [PATCH 3/4] chore: fix grammar in comment about imports --- asyncio/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/asyncio/core.py b/asyncio/core.py index 0201116..2e360a7 100644 --- a/asyncio/core.py +++ b/asyncio/core.py @@ -26,7 +26,7 @@ # Depending on the release of CircuitPython these errors may or may not # exist in the C implementation of `_asyncio`. However, when they -# do exist, must be preferred over the Python code. +# do exist, they must be preferred over the Python code. try: from _asyncio import CancelledError, InvalidStateError except (ImportError, AttributeError): From 3921b204900accd7829c5f5d96885febf60b0584 Mon Sep 17 00:00:00 2001 From: James Ward Date: Mon, 13 Nov 2023 20:06:22 -0500 Subject: [PATCH 4/4] chore: move errors into core under the import catch block --- asyncio/core.py | 14 +++++++++++--- asyncio/task.py | 12 ------------ 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/asyncio/core.py b/asyncio/core.py index 2e360a7..8dc1eb4 100644 --- a/asyncio/core.py +++ b/asyncio/core.py @@ -24,16 +24,24 @@ except ImportError: from .task import TaskQueue, Task +################################################################################ +# Exceptions + + # Depending on the release of CircuitPython these errors may or may not # exist in the C implementation of `_asyncio`. However, when they # do exist, they must be preferred over the Python code. try: from _asyncio import CancelledError, InvalidStateError except (ImportError, AttributeError): - from .task import CancelledError, InvalidStateError + class CancelledError(BaseException): + """Injected into a task when calling `Task.cancel()`""" + pass -################################################################################ -# Exceptions + + class InvalidStateError(Exception): + """Can be raised in situations like setting a result value for a task object that already has a result value set.""" + pass class TimeoutError(Exception): diff --git a/asyncio/task.py b/asyncio/task.py index d9d6743..2e3a6db 100644 --- a/asyncio/task.py +++ b/asyncio/task.py @@ -21,18 +21,6 @@ from . import core -class CancelledError(BaseException): - """Injected into a task when calling `Task.cancel()`""" - - pass - - -class InvalidStateError(Exception): - """Can be raised in situations like setting a result value for a task object that already has a result value set.""" - - pass - - # pairing-heap meld of 2 heaps; O(1) def ph_meld(h1, h2): if h1 is None: