https://github.com/python/cpython/commit/a5db6a3351b440a875a5af84a8b2447981356e34
commit: a5db6a3351b440a875a5af84a8b2447981356e34
branch: main
author: Serhiy Storchaka <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2024-01-09T21:41:31+02:00
summary:

gh-113848: Handle CancelledError subclasses in asyncio TaskGroup() and 
timeout() (GH-113850)

files:
A Misc/NEWS.d/next/Library/2024-01-09-12-19-55.gh-issue-113848.kXoCy0.rst
M Lib/asyncio/taskgroups.py
M Lib/asyncio/timeouts.py

diff --git a/Lib/asyncio/taskgroups.py b/Lib/asyncio/taskgroups.py
index cb9c1ce4d7d1d2..e1c56d140bef7d 100644
--- a/Lib/asyncio/taskgroups.py
+++ b/Lib/asyncio/taskgroups.py
@@ -73,8 +73,10 @@ async def __aexit__(self, et, exc, tb):
                 self._base_error is None):
             self._base_error = exc
 
-        propagate_cancellation_error = \
-            exc if et is exceptions.CancelledError else None
+        if et is not None and issubclass(et, exceptions.CancelledError):
+            propagate_cancellation_error = exc
+        else:
+            propagate_cancellation_error = None
         if self._parent_cancel_requested:
             # If this flag is set we *must* call uncancel().
             if self._parent_task.uncancel() == 0:
@@ -133,7 +135,7 @@ async def __aexit__(self, et, exc, tb):
         if propagate_cancellation_error and not self._errors:
             raise propagate_cancellation_error
 
-        if et is not None and et is not exceptions.CancelledError:
+        if et is not None and not issubclass(et, exceptions.CancelledError):
             self._errors.append(exc)
 
         if self._errors:
diff --git a/Lib/asyncio/timeouts.py b/Lib/asyncio/timeouts.py
index 30042abb3ad804..2c5dd295ff5ade 100644
--- a/Lib/asyncio/timeouts.py
+++ b/Lib/asyncio/timeouts.py
@@ -109,10 +109,11 @@ async def __aexit__(
         if self._state is _State.EXPIRING:
             self._state = _State.EXPIRED
 
-            if self._task.uncancel() <= self._cancelling and exc_type is 
exceptions.CancelledError:
-                # Since there are no new cancel requests, we're
-                # handling this.
-                raise TimeoutError from exc_val
+            if self._task.uncancel() <= self._cancelling and exc_type is not 
None:
+                if issubclass(exc_type, exceptions.CancelledError):
+                    # Since there are no new cancel requests, we're
+                    # handling this.
+                    raise TimeoutError from exc_val
         elif self._state is _State.ENTERED:
             self._state = _State.EXITED
 
diff --git 
a/Misc/NEWS.d/next/Library/2024-01-09-12-19-55.gh-issue-113848.kXoCy0.rst 
b/Misc/NEWS.d/next/Library/2024-01-09-12-19-55.gh-issue-113848.kXoCy0.rst
new file mode 100644
index 00000000000000..8d5032ab0201f9
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-01-09-12-19-55.gh-issue-113848.kXoCy0.rst
@@ -0,0 +1,3 @@
+:func:`asyncio.TaskGroup()` and :func:`asyncio.timeout()` context managers
+now handle :exc:`~asyncio.CancelledError` subclasses as well as exact
+:exc:`!CancelledError`.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to