Skip to content

Commit

Permalink
Merge branch 'master' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
ahopkins authored Dec 25, 2020
2 parents 01d2a2a + ac1331e commit 3833744
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 128 deletions.
10 changes: 5 additions & 5 deletions .appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ environment:
PYTHON_VERSION: "3.8.x"
PYTHON_ARCH: "64"

- TOXENV: py39-no-ext
PYTHON: "C:\\Python39-x64\\python"
PYTHONPATH: "C:\\Python39-x64"
PYTHON_VERSION: "3.9.x"
PYTHON_ARCH: "64"
# - TOXENV: py39-no-ext
# PYTHON: "C:\\Python39-x64\\python"
# PYTHONPATH: "C:\\Python39-x64"
# PYTHON_VERSION: "3.9.x"
# PYTHON_ARCH: "64"

init: SET "PATH=%PYTHON%;%PYTHON%\\Scripts;%PATH%"

Expand Down
4 changes: 0 additions & 4 deletions docs/sanic/testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@ More information about
the available arguments to `httpx` can be found
[in the documentation for `httpx <https://www.encode.io/httpx/>`_.

Additionally, Sanic has an asynchronous testing client. The difference is that the async client will not stand up an
instance of your application, but will instead reach inside it using ASGI. All listeners and middleware are still
executed.

.. code-block:: python
@pytest.mark.asyncio
async def test_index_returns_200():
Expand Down
5 changes: 4 additions & 1 deletion examples/run_asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from sanic import Sanic, response


app = Sanic(__name__)


Expand Down Expand Up @@ -42,7 +43,9 @@ async def handler_file(request):

@app.route("/file_stream")
async def handler_file_stream(request):
return await response.file_stream(Path("../") / "setup.py", chunk_size=1024)
return await response.file_stream(
Path("../") / "setup.py", chunk_size=1024
)


@app.route("/stream", stream=True)
Expand Down
52 changes: 0 additions & 52 deletions sanic/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -714,28 +714,6 @@ def blueprint(self, blueprint, **options):
self._blueprint_order.append(blueprint)
blueprint.register(self, options)

def register_blueprint(self, *args, **kwargs):
"""
Proxy method provided for invoking the :func:`blueprint` method
.. note::
To be deprecated in 1.0. Use :func:`blueprint` instead.
:param args: Blueprint object or (list, tuple) thereof
:param kwargs: option dictionary with blueprint defaults
:return: None
"""

if self.debug:
warnings.simplefilter("default")
warnings.warn(
"Use of register_blueprint will be deprecated in "
"version 1.0. Please use the blueprint method"
" instead",
DeprecationWarning,
)
return self.blueprint(*args, **kwargs)

def url_for(self, view_name: str, **kwargs):
r"""Build a URL based on a view name and the values provided.
Expand Down Expand Up @@ -1026,7 +1004,6 @@ def run(
workers: int = 1,
protocol: Optional[Type[Protocol]] = None,
backlog: int = 100,
stop_event: Any = None,
register_sys_signals: bool = True,
access_log: Optional[bool] = None,
unix: Optional[str] = None,
Expand Down Expand Up @@ -1056,9 +1033,6 @@ def run(
:param backlog: a number of unaccepted connections that the system
will allow before refusing new connections
:type backlog: int
:param stop_event: event to be triggered
before stopping the app - deprecated
:type stop_event: None
:param register_sys_signals: Register SIG* events
:type register_sys_signals: bool
:param access_log: Enables writing access logs (slows server)
Expand Down Expand Up @@ -1086,13 +1060,6 @@ def run(
protocol = (
WebSocketProtocol if self.websocket_enabled else HttpProtocol
)
if stop_event is not None:
if debug:
warnings.simplefilter("default")
warnings.warn(
"stop_event will be removed from future versions.",
DeprecationWarning,
)
# if access_log is passed explicitly change config.ACCESS_LOG
if access_log is not None:
self.config.ACCESS_LOG = access_log
Expand Down Expand Up @@ -1149,7 +1116,6 @@ async def create_server(
sock: Optional[socket] = None,
protocol: Type[Protocol] = None,
backlog: int = 100,
stop_event: Any = None,
access_log: Optional[bool] = None,
unix: Optional[str] = None,
return_asyncio_server=False,
Expand Down Expand Up @@ -1182,9 +1148,6 @@ async def create_server(
:param backlog: a number of unaccepted connections that the system
will allow before refusing new connections
:type backlog: int
:param stop_event: event to be triggered
before stopping the app - deprecated
:type stop_event: None
:param access_log: Enables writing access logs (slows server)
:type access_log: bool
:param return_asyncio_server: flag that defines whether there's a need
Expand All @@ -1204,13 +1167,6 @@ async def create_server(
protocol = (
WebSocketProtocol if self.websocket_enabled else HttpProtocol
)
if stop_event is not None:
if debug:
warnings.simplefilter("default")
warnings.warn(
"stop_event will be removed from future versions.",
DeprecationWarning,
)
# if access_log is passed explicitly change config.ACCESS_LOG
if access_log is not None:
self.config.ACCESS_LOG = access_log
Expand Down Expand Up @@ -1292,7 +1248,6 @@ def _helper(
loop=None,
protocol=HttpProtocol,
backlog=100,
stop_event=None,
register_sys_signals=True,
run_async=False,
auto_reload=False,
Expand All @@ -1307,13 +1262,6 @@ def _helper(
context = create_default_context(purpose=Purpose.CLIENT_AUTH)
context.load_cert_chain(cert, keyfile=key)
ssl = context
if stop_event is not None:
if debug:
warnings.simplefilter("default")
warnings.warn(
"stop_event will be removed from future versions.",
DeprecationWarning,
)
if self.config.PROXIES_COUNT and self.config.PROXIES_COUNT < 0:
raise ValueError(
"PROXIES_COUNT cannot be negative. "
Expand Down
9 changes: 6 additions & 3 deletions sanic/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,18 @@ def __repr__(self):
return f"<{class_name}: {self.method} {self.path}>"

def body_init(self):
""".. deprecated:: 20.3"""
""".. deprecated:: 20.3
To be removed in 21.3"""
self.body = []

def body_push(self, data):
""".. deprecated:: 20.3"""
""".. deprecated:: 20.3
To be removed in 21.3"""
self.body.append(data)

def body_finish(self):
""".. deprecated:: 20.3"""
""".. deprecated:: 20.3
To be removed in 21.3"""
self.body = b"".join(self.body)

async def receive_body(self):
Expand Down
31 changes: 7 additions & 24 deletions sanic/response.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import warnings

from functools import partial
from mimetypes import guess_type
from os import path
Expand All @@ -26,6 +24,8 @@ def __init__(self):
self.asgi = False

def _encode_body(self, data):
if data is None:
return b""
return data.encode() if hasattr(data, "encode") else data

def _parse_headers(self):
Expand All @@ -45,7 +45,7 @@ def get_headers(
body=b"",
):
""".. deprecated:: 20.3:
This function is not public API and will be removed."""
This function is not public API and will be removed in 21.3."""

# self.headers get priority over content_type
if self.content_type and "Content-Type" not in self.headers:
Expand Down Expand Up @@ -149,22 +149,15 @@ def __init__(
status=200,
headers=None,
content_type=None,
body_bytes=b"",
):
super().__init__()

self.content_type = content_type
self.body = body_bytes if body is None else self._encode_body(body)
self.body = self._encode_body(body)
self.status = status
self.headers = Header(headers or {})
self._cookies = None

if body_bytes:
warnings.warn(
"Parameter `body_bytes` is deprecated, use `body` instead",
DeprecationWarning,
)

def output(self, version="1.1", keep_alive=False, keep_alive_timeout=None):
body = b""
if has_message_body(self.status):
Expand Down Expand Up @@ -228,20 +221,10 @@ def text(
:param content_type: the content type (string) of the response
"""
if not isinstance(body, str):
warnings.warn(
"Types other than str will be deprecated in future versions for"
f" response.text, got type {type(body).__name__})",
DeprecationWarning,
raise TypeError(
f"Bad body type. Expected str, got {type(body).__name__})"
)
# Type conversions are deprecated and quite b0rked but still supported for
# text() until applications get fixed. This try-except should be removed.
try:
# Avoid repr(body).encode() b0rkage for body that is already encoded.
# memoryview used only to test bytes-ishness.
with memoryview(body):
pass
except TypeError:
body = f"{body}" # no-op if body is already str

return HTTPResponse(
body, status=status, headers=headers, content_type=content_type
)
Expand Down
14 changes: 10 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os
import re
import sys

from distutils.util import strtobool

from setuptools import setup
Expand All @@ -24,6 +25,7 @@ def initialize_options(self):

def run_tests(self):
import shlex

import pytest

errno = pytest.main(shlex.split(self.pytest_args))
Expand All @@ -38,7 +40,9 @@ def open_local(paths, mode="r", encoding="utf8"):

with open_local(["sanic", "__version__.py"], encoding="latin1") as fp:
try:
version = re.findall(r"^__version__ = \"([^']+)\"\r?$", fp.read(), re.M)[0]
version = re.findall(
r"^__version__ = \"([^']+)\"\r?$", fp.read(), re.M
)[0]
except IndexError:
raise RuntimeError("Unable to determine version.")

Expand Down Expand Up @@ -72,7 +76,9 @@ def open_local(paths, mode="r", encoding="utf8"):
"entry_points": {"console_scripts": ["sanic = sanic.__main__:main"]},
}

env_dependency = '; sys_platform != "win32" ' 'and implementation_name == "cpython"'
env_dependency = (
'; sys_platform != "win32" ' 'and implementation_name == "cpython"'
)
ujson = "ujson>=1.35" + env_dependency
uvloop = "uvloop>=0.5.3" + env_dependency

Expand All @@ -89,9 +95,9 @@ def open_local(paths, mode="r", encoding="utf8"):
tests_require = [
"pytest==5.2.1",
"multidict>=5.0,<6.0",
"gunicorn",
"gunicorn==20.0.4",
"pytest-cov",
"httpcore==0.3.0",
"httpcore==0.11.*",
"beautifulsoup4",
uvloop,
ujson,
Expand Down
15 changes: 0 additions & 15 deletions tests/test_blueprints.py
Original file line number Diff line number Diff line change
Expand Up @@ -825,21 +825,6 @@ def test_duplicate_blueprint(app):
)


@pytest.mark.parametrize("debug", [True, False, None])
def test_register_blueprint(app, debug):
bp = Blueprint("bp")

app.debug = debug
with pytest.warns(DeprecationWarning) as record:
app.register_blueprint(bp)

assert record[0].message.args[0] == (
"Use of register_blueprint will be deprecated in "
"version 1.0. Please use the blueprint method"
" instead"
)


def test_strict_slashes_behavior_adoption(app):
app.strict_slashes = True

Expand Down
17 changes: 2 additions & 15 deletions tests/test_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ async def hello_route(request):
return text(random_num)

request, response = app.test_client.get("/hello")
assert response.text == str(random_num)
assert response.status == 500
assert b"Internal Server Error" in response.body


async def sample_streaming_fn(response):
Expand Down Expand Up @@ -624,17 +625,3 @@ def handler(request):
request, response = app.test_client.get("/test")
assert response.content_type is None
assert response.body == b""


def test_response_body_bytes_deprecated(app):
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")

HTTPResponse(body_bytes=b"bytes")

assert len(w) == 1
assert issubclass(w[0].category, DeprecationWarning)
assert (
"Parameter `body_bytes` is deprecated, use `body` instead"
in str(w[0].message)
)
10 changes: 5 additions & 5 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ setenv =
{py36,py37,py38,py39,pyNightly}-no-ext: SANIC_NO_UJSON=1
{py36,py37,py38,py39,pyNightly}-no-ext: SANIC_NO_UVLOOP=1
deps =
coverage
coverage==5.3
pytest==5.2.1
pytest-cov
pytest-sanic
pytest-sugar
pytest-benchmark
pytest-dependency
httpcore==0.3.0
httpcore==0.11.*
httpx==0.15.4
chardet<=2.3.0
chardet==3.*
beautifulsoup4
gunicorn
gunicorn==20.0.4
uvicorn
websockets>=8.1,<9.0
commands =
Expand Down Expand Up @@ -76,7 +76,7 @@ deps =
recommonmark>=0.5.0
docutils
pygments
gunicorn
gunicorn==20.0.4

commands =
make docs-test

0 comments on commit 3833744

Please sign in to comment.