Merge changes into tests branch

This commit is contained in:
prryplatypus 2021-10-23 20:42:21 +02:00
commit 48b0158cd7
No known key found for this signature in database
GPG Key ID: 6687E128FB70819B
4 changed files with 24 additions and 32 deletions

View File

@ -50,7 +50,7 @@ from sanic.asgi import ASGIApp
from sanic.base import BaseSanic from sanic.base import BaseSanic
from sanic.blueprint_group import BlueprintGroup from sanic.blueprint_group import BlueprintGroup
from sanic.blueprints import Blueprint from sanic.blueprints import Blueprint
from sanic.compat import OS_IS_WINDOWS from sanic.compat import OS_IS_WINDOWS, UVLOOP_INSTALLED
from sanic.config import BASE_LOGO, SANIC_PREFIX, Config from sanic.config import BASE_LOGO, SANIC_PREFIX, Config
from sanic.exceptions import ( from sanic.exceptions import (
InvalidUsage, InvalidUsage,
@ -1421,10 +1421,8 @@ class Sanic(BaseSanic, metaclass=TouchUpMeta):
def _configure_event_loop(self): def _configure_event_loop(self):
if self.config.USE_UVLOOP and not OS_IS_WINDOWS: if self.config.USE_UVLOOP and not OS_IS_WINDOWS:
uvloop_success = use_uvloop() if not UVLOOP_INSTALLED:
return error_logger.warning(
if uvloop_success is False:
error_logger.warning(
"You are trying to use uvloop, but uvloop is not " "You are trying to use uvloop, but uvloop is not "
"installed in your system. In order to use uvloop " "installed in your system. In order to use uvloop "
"you must first install it. Otherwise, you can disable " "you must first install it. Otherwise, you can disable "
@ -1433,7 +1431,8 @@ class Sanic(BaseSanic, metaclass=TouchUpMeta):
"to run without using uvloop." "to run without using uvloop."
) )
elif strtobool(os.environ.get("SANIC_NO_UVLOOP", "no")): use_uvloop()
if strtobool(os.environ.get("SANIC_NO_UVLOOP", "no")):
error_logger.warning( error_logger.warning(
"You are running Sanic using uvloop, but the " "You are running Sanic using uvloop, but the "
"'SANIC_NO_UVLOOP' environment variable (used to opt-out " "'SANIC_NO_UVLOOP' environment variable (used to opt-out "

View File

@ -8,6 +8,13 @@ from multidict import CIMultiDict # type: ignore
OS_IS_WINDOWS = os.name == "nt" OS_IS_WINDOWS = os.name == "nt"
UVLOOP_INSTALLED = False
try:
import uvloop # type: ignore # noqa
UVLOOP_INSTALLED = True
except ImportError:
pass
class Header(CIMultiDict): class Header(CIMultiDict):

View File

@ -8,19 +8,14 @@ from sanic.server.runners import serve, serve_multiple, serve_single
def use_uvloop() -> bool: def use_uvloop() -> bool:
""" """
Use uvloop (if available) instead of the default Use uvloop instead of the default asyncio loop.
asyncio loop.
""" """
try: import uvloop # type: ignore
import uvloop # type: ignore
if not isinstance( if not isinstance(
asyncio.get_event_loop_policy(), uvloop.EventLoopPolicy asyncio.get_event_loop_policy(), uvloop.EventLoopPolicy
): ):
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
except ImportError:
return False
return True
__all__ = ( __all__ = (

View File

@ -11,7 +11,7 @@ import pytest
import sanic.app import sanic.app
from sanic import Sanic from sanic import Sanic
from sanic.compat import OS_IS_WINDOWS from sanic.compat import OS_IS_WINDOWS, UVLOOP_INSTALLED
from sanic.config import Config from sanic.config import Config
from sanic.exceptions import SanicException from sanic.exceptions import SanicException
from sanic.response import text from sanic.response import text
@ -22,15 +22,6 @@ def clear_app_registry():
Sanic._app_registry = {} Sanic._app_registry = {}
def uvloop_installed():
try:
import uvloop # noqa
return True
except ImportError:
return False
def test_app_loop_running(app): def test_app_loop_running(app):
@app.get("/test") @app.get("/test")
async def handler(request): async def handler(request):
@ -42,7 +33,7 @@ def test_app_loop_running(app):
def test_create_asyncio_server(app): def test_create_asyncio_server(app):
if not uvloop_installed(): if not UVLOOP_INSTALLED:
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
asyncio_srv_coro = app.create_server(return_asyncio_server=True) asyncio_srv_coro = app.create_server(return_asyncio_server=True)
assert isawaitable(asyncio_srv_coro) assert isawaitable(asyncio_srv_coro)
@ -51,7 +42,7 @@ def test_create_asyncio_server(app):
def test_asyncio_server_no_start_serving(app): def test_asyncio_server_no_start_serving(app):
if not uvloop_installed(): if not UVLOOP_INSTALLED:
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
asyncio_srv_coro = app.create_server( asyncio_srv_coro = app.create_server(
port=43123, port=43123,
@ -63,7 +54,7 @@ def test_asyncio_server_no_start_serving(app):
def test_asyncio_server_start_serving(app): def test_asyncio_server_start_serving(app):
if not uvloop_installed(): if not UVLOOP_INSTALLED:
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
asyncio_srv_coro = app.create_server( asyncio_srv_coro = app.create_server(
port=43124, port=43124,
@ -455,7 +446,7 @@ def test_uvloop_config_enabled(monkeypatch):
err_logger = Mock() err_logger = Mock()
monkeypatch.setattr(sanic.app, "error_logger", err_logger) monkeypatch.setattr(sanic.app, "error_logger", err_logger)
use_uvloop = Mock(return_value=uvloop_installed()) use_uvloop = Mock(return_value=UVLOOP_INSTALLED)
monkeypatch.setattr(sanic.app, "use_uvloop", use_uvloop) monkeypatch.setattr(sanic.app, "use_uvloop", use_uvloop)
@app.get("/1") @app.get("/1")
@ -466,7 +457,7 @@ def test_uvloop_config_enabled(monkeypatch):
use_uvloop.assert_called_once() use_uvloop.assert_called_once()
if not uvloop_installed(): if not UVLOOP_INSTALLED:
err_logger.assert_called_with( err_logger.assert_called_with(
"You are trying to use uvloop, but uvloop is not " "You are trying to use uvloop, but uvloop is not "
"installed in your system. In order to use uvloop " "installed in your system. In order to use uvloop "