From a361b345ad6c03860099a4bae09ca08b504311f3 Mon Sep 17 00:00:00 2001 From: Stephen Sadowski Date: Thu, 28 Oct 2021 17:16:23 -0500 Subject: [PATCH] Py310 loop fixup (#2294) * Fixup for 3.8+; Sanic still supports 3.7 where loop is required * Added branching statement to hanle asyncio.Event() loop parameter removal in 3.10, and optional supply in 3.9 --- sanic/models/asgi.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/sanic/models/asgi.py b/sanic/models/asgi.py index 1b707ebc..57b755ee 100644 --- a/sanic/models/asgi.py +++ b/sanic/models/asgi.py @@ -1,4 +1,5 @@ import asyncio +import sys from typing import Any, Awaitable, Callable, MutableMapping, Optional, Union @@ -14,10 +15,20 @@ ASGIReceive = Callable[[], Awaitable[ASGIMessage]] class MockProtocol: def __init__(self, transport: "MockTransport", loop): + # This should be refactored when < 3.8 support is dropped self.transport = transport - self._not_paused = asyncio.Event(loop=loop) - self._not_paused.set() - self._complete = asyncio.Event(loop=loop) + # Fixup for 3.8+; Sanic still supports 3.7 where loop is required + loop = loop if sys.version_info[:2] < (3, 8) else None + # Optional in 3.9, necessary in 3.10 because the parameter "loop" + # was completely removed + if not loop: + self._not_paused = asyncio.Event() + self._not_paused.set() + self._complete = asyncio.Event() + else: + self._not_paused = asyncio.Event(loop=loop) + self._not_paused.set() + self._complete = asyncio.Event(loop=loop) def pause_writing(self) -> None: self._not_paused.clear()