Skip to content

Commit 5613f20

Browse files
committed
use ExceptionGroup for trio > 0.22
fixes #172
1 parent d270105 commit 5613f20

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

.github/workflows/ci.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,24 @@ jobs:
2626
- run: make test
2727
- run: make lint
2828

29+
build_and_test_old_trio:
30+
runs-on: ${{ matrix.os }}
31+
strategy:
32+
matrix:
33+
os: [ubuntu-latest]
34+
python-version: ['3.7']
35+
36+
steps:
37+
- uses: actions/checkout@v3
38+
- name: Setup Python
39+
uses: actions/setup-python@v3
40+
with:
41+
python-version: ${{ matrix.python-version }}
42+
cache: 'pip'
43+
cache-dependency-path: 'requirements-min.txt'
44+
- run: pip install . -r requirements-min.txt && pip install trio==0.15.0 pytest-trio==0.7.0
45+
- run: make test
46+
2947
build_and_test_pypy:
3048
runs-on: ${{ matrix.os }}
3149
strategy:

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
keywords='websocket client server trio',
4141
packages=find_packages(exclude=['docs', 'examples', 'tests']),
4242
install_requires=[
43+
'exceptiongroup',
4344
'trio>=0.11',
4445
'wsproto>=0.14',
4546
],

trio_websocket/_impl.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import trio
1515
import trio.abc
16+
from exceptiongroup import BaseExceptionGroup
1617
from wsproto import ConnectionType, WSConnection
1718
from wsproto.connection import ConnectionState
1819
import wsproto.frame_protocol as wsframeproto
@@ -29,6 +30,7 @@
2930
)
3031
import wsproto.utilities
3132

33+
_TRIO_MULTI_ERROR = tuple(map(int, trio.__version__.split('.'))) < (0, 22, 0)
3234

3335
CONN_TIMEOUT = 60 # default connect & disconnect timeout, in seconds
3436
MESSAGE_QUEUE_SIZE = 1
@@ -37,6 +39,10 @@
3739
logger = logging.getLogger('trio-websocket')
3840

3941

42+
def _ignore_cancel(exc):
43+
return None if isinstance(exc, trio.Cancelled) else exc
44+
45+
4046
class _preserve_current_exception:
4147
"""A context manager which should surround an ``__exit__`` or
4248
``__aexit__`` handler or the contents of a ``finally:``
@@ -59,10 +65,13 @@ def __exit__(self, ty, value, tb):
5965
if value is None or not self._armed:
6066
return False
6167

62-
def remove_cancels(exc):
63-
return None if isinstance(exc, trio.Cancelled) else exc
64-
65-
return trio.MultiError.filter(remove_cancels, value) is None # pylint: disable=no-member
68+
if _TRIO_MULTI_ERROR:
69+
filtered_exception = trio.MultiError.filter(_ignore_cancel, value) # pylint: disable=no-member
70+
elif isinstance(value, BaseExceptionGroup):
71+
filtered_exception = value.subgroup(lambda exc: not isinstance(exc, trio.Cancelled))
72+
else:
73+
filtered_exception = _ignore_cancel(value)
74+
return filtered_exception is None
6675

6776

6877
@asynccontextmanager

0 commit comments

Comments
 (0)