Fix middleware traceback fetching on Python 3.8+, fix ResourceWarnings in TestClient, fix CI build (#1132)

* Add __init__ file for tests.middleware so Mypy 0.800 is happy

* testclient: Tie loop lifetime to thread

* ServerErrorMiddleware: Don't use undocumented TracebackException.exc_traceback attribute
This commit is contained in:
Jamie Hewland 2021-01-31 11:43:07 +00:00 committed by GitHub
parent e4307065ea
commit 62e95b89fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 8 deletions

View File

@ -217,15 +217,15 @@ class ServerErrorMiddleware:
traceback_obj = traceback.TracebackException.from_exception( traceback_obj = traceback.TracebackException.from_exception(
exc, capture_locals=True exc, capture_locals=True
) )
frames = inspect.getinnerframes(
traceback_obj.exc_traceback, limit # type: ignore
)
exc_html = "" exc_html = ""
is_collapsed = False is_collapsed = False
for frame in reversed(frames): exc_traceback = exc.__traceback__
exc_html += self.generate_frame_html(frame, is_collapsed) if exc_traceback is not None:
is_collapsed = True frames = inspect.getinnerframes(exc_traceback, limit)
for frame in reversed(frames):
exc_html += self.generate_frame_html(frame, is_collapsed)
is_collapsed = True
# escape error class and text # escape error class and text
error = ( error = (

View File

@ -268,7 +268,6 @@ class WebSocketTestSession:
self.app = app self.app = app
self.scope = scope self.scope = scope
self.accepted_subprotocol = None self.accepted_subprotocol = None
self._loop = asyncio.new_event_loop()
self._receive_queue = queue.Queue() # type: queue.Queue self._receive_queue = queue.Queue() # type: queue.Queue
self._send_queue = queue.Queue() # type: queue.Queue self._send_queue = queue.Queue() # type: queue.Queue
self._thread = threading.Thread(target=self._run) self._thread = threading.Thread(target=self._run)
@ -293,13 +292,16 @@ class WebSocketTestSession:
""" """
The sub-thread in which the websocket session runs. The sub-thread in which the websocket session runs.
""" """
loop = asyncio.new_event_loop()
scope = self.scope scope = self.scope
receive = self._asgi_receive receive = self._asgi_receive
send = self._asgi_send send = self._asgi_send
try: try:
self._loop.run_until_complete(self.app(scope, receive, send)) loop.run_until_complete(self.app(scope, receive, send))
except BaseException as exc: except BaseException as exc:
self._send_queue.put(exc) self._send_queue.put(exc)
finally:
loop.close()
async def _asgi_receive(self) -> Message: async def _asgi_receive(self) -> Message:
while self._receive_queue.empty(): while self._receive_queue.empty():

View File