Skip to content

Commit 1b0be05

Browse files
committed
small cleanups
1 parent 16d8d9f commit 1b0be05

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

trio_websocket/_impl.py

+7-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import annotations
22

3-
import copy
43
import sys
54
from collections import OrderedDict
65
from contextlib import asynccontextmanager
@@ -152,14 +151,14 @@ async def open_websocket(
152151
# yield to user code. If only one of those raise a non-cancelled exception
153152
# we will raise that non-cancelled exception.
154153
# If we get multiple cancelled, we raise the user's cancelled.
155-
# If both raise exceptions, we raise the user code's exception with the entire
156-
# exception group as the __cause__.
154+
# If both raise exceptions, we raise the user code's exception with __context__
155+
# set to a group containing internal exception(s) + any user exception __context__
157156
# If we somehow get multiple exceptions, but no user exception, then we raise
158157
# TrioWebsocketInternalError.
159158

160159
# If closing the connection fails, then that will be raised as the top
161160
# exception in the last `finally`. If we encountered exceptions in user code
162-
# or in reader task then they will be set as the `__cause__`.
161+
# or in reader task then they will be set as the `__context__`.
163162

164163

165164
async def _open_connection(nursery: trio.Nursery) -> WebSocketConnection:
@@ -183,6 +182,8 @@ async def _close_connection(connection: WebSocketConnection) -> None:
183182
raise DisconnectionTimeout from None
184183

185184
def _raise(exc: BaseException) -> NoReturn:
185+
"""This helper allows re-raising an exception without __context__ being set."""
186+
# cause does not need special handlng, we simply avoid using `raise .. from ..`
186187
__tracebackhide__ = True
187188
context = exc.__context__
188189
try:
@@ -199,11 +200,7 @@ def _raise(exc: BaseException) -> NoReturn:
199200
# the exception we raise also being inside the group that's set as the context.
200201
# This leads to loss of info unless properly handled.
201202
# See https://github.com/python-trio/flake8-async/issues/298
202-
# We therefore save the exception before raising it, and save our intended context,
203-
# so they can be modified in the `finally`.
204-
exc_to_raise = None
205-
exc_context = None
206-
# by avoiding use of `raise .. from ..` we leave the original __cause__
203+
# We therefore avoid having the exceptiongroup included as either cause or context
207204

208205
try:
209206
async with trio.open_nursery() as new_nursery:
@@ -243,7 +240,7 @@ def _raise(exc: BaseException) -> NoReturn:
243240
_raise(user_error)
244241
# multiple internal Cancelled is not possible afaik
245242
# but if so we just raise one of them
246-
_raise(e.exceptions[0])
243+
_raise(e.exceptions[0]) # pragma: no cover
247244
# raise the non-cancelled exception
248245
_raise(exception_to_raise)
249246

0 commit comments

Comments
 (0)