Skip to content

Commit

Permalink
Py310 loop fixup (sanic-org#2294)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
sjsadowski authored and ChihweiLHBird committed Jun 1, 2022
1 parent 27d5ff3 commit d6ade36
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions sanic/models/asgi.py
@@ -1,4 +1,5 @@
import asyncio
import sys

from typing import Any, Awaitable, Callable, MutableMapping, Optional, Union

Expand All @@ -14,10 +15,20 @@

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()
Expand Down

0 comments on commit d6ade36

Please sign in to comment.