From 209579f2805c3a98fdc0385a0954b451dc178fd9 Mon Sep 17 00:00:00 2001 From: Adam Hopkins Date: Wed, 17 Feb 2021 16:10:57 +0200 Subject: [PATCH] Resolve tests --- sanic/app.py | 13 +++++++++++-- sanic/request.py | 5 ++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/sanic/app.py b/sanic/app.py index 1a690db7..028cfe8f 100644 --- a/sanic/app.py +++ b/sanic/app.py @@ -59,7 +59,6 @@ from sanic.server import ( Signal, serve, serve_multiple, - trigger_events, ) from sanic.websocket import ConnectionClosed, WebSocketProtocol @@ -908,7 +907,7 @@ class Sanic(BaseSanic): ) # Trigger before_start events - await trigger_events( + await self.trigger_events( server_settings.get("before_start", []), server_settings.get("loop"), ) @@ -917,6 +916,16 @@ class Sanic(BaseSanic): asyncio_server_kwargs=asyncio_server_kwargs, **server_settings ) + async def trigger_events(self, events, loop): + """Trigger events (functions or async) + :param events: one or more sync or async functions to execute + :param loop: event loop + """ + for event in events: + result = event(loop) + if isawaitable(result): + await result + async def _run_request_middleware(self, request, request_name=None): # The if improves speed. I don't know why named_middleware = self.named_request_middleware.get( diff --git a/sanic/request.py b/sanic/request.py index 6278323b..175d2ef1 100644 --- a/sanic/request.py +++ b/sanic/request.py @@ -147,7 +147,7 @@ class Request: ] = defaultdict(list) self.uri_template: Optional[str] = None self.request_middleware_started = False - self._cookies: Dict[str, str] = {} + self._cookies: Optional[Dict[str, str]] = None self._match_info = {} self.stream: Optional[Http] = None self.endpoint: Optional[str] = None @@ -183,8 +183,7 @@ class Request: response = await self.app._run_response_middleware( self, response, request_name=self.name ) - # Redefining this as a tuple here satisfies mypy - except tuple(*CancelledErrors): + except CancelledErrors: raise except Exception: error_logger.exception(