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.blueprint_group import BlueprintGroup
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.exceptions import (
InvalidUsage,
@ -1421,10 +1421,8 @@ class Sanic(BaseSanic, metaclass=TouchUpMeta):
def _configure_event_loop(self):
if self.config.USE_UVLOOP and not OS_IS_WINDOWS:
uvloop_success = use_uvloop()
if uvloop_success is False:
error_logger.warning(
if not UVLOOP_INSTALLED:
return error_logger.warning(
"You are trying to use uvloop, but uvloop is not "
"installed in your system. In order to use uvloop "
"you must first install it. Otherwise, you can disable "
@ -1433,7 +1431,8 @@ class Sanic(BaseSanic, metaclass=TouchUpMeta):
"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(
"You are running Sanic using uvloop, but the "
"'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"
UVLOOP_INSTALLED = False
try:
import uvloop # type: ignore # noqa
UVLOOP_INSTALLED = True
except ImportError:
pass
class Header(CIMultiDict):

View File

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

View File

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