From 42ba5298a767352e13896da142131bf116760ae9 Mon Sep 17 00:00:00 2001 From: Jakob Bowyer Date: Tue, 28 Mar 2017 10:50:09 +0100 Subject: [PATCH 1/5] Flake8 cleanup. Setup environmental variables. Skipping broken tests unrelated. --- sanic/response.py | 35 +++++++++++++++------------ setup.py | 43 +++++++++++++++++++++------------ tests/test_payload_too_large.py | 5 +++- tests/test_request_data.py | 2 +- tests/test_request_timeout.py | 11 ++++++--- 5 files changed, 60 insertions(+), 36 deletions(-) diff --git a/sanic/response.py b/sanic/response.py index 38cd68db..d8dadbc8 100644 --- a/sanic/response.py +++ b/sanic/response.py @@ -1,5 +1,6 @@ from mimetypes import guess_type from os import path + try: from ujson import dumps as json_dumps except: @@ -167,12 +168,12 @@ class StreamingHTTPResponse(BaseHTTPResponse): return (b'HTTP/%b %d %b\r\n' b'%b' b'%b\r\n') % ( - version.encode(), - self.status, - status, - timeout_header, - headers - ) + version.encode(), + self.status, + status, + timeout_header, + headers + ) class HTTPResponse(BaseHTTPResponse): @@ -216,14 +217,14 @@ class HTTPResponse(BaseHTTPResponse): b'%b' b'%b\r\n' b'%b') % ( - version.encode(), - self.status, - status, - b'keep-alive' if keep_alive else b'close', - timeout_header, - headers, - self.body - ) + version.encode(), + self.status, + status, + b'keep-alive' if keep_alive else b'close', + timeout_header, + headers, + self.body + ) @property def cookies(self): @@ -331,7 +332,11 @@ def stream( :param headers: Custom Headers. """ return StreamingHTTPResponse( - streaming_fn, headers=headers, content_type=content_type, status=status) + streaming_fn, + headers=headers, + content_type=content_type, + status=status + ) def redirect(to, headers=None, status=302, diff --git a/setup.py b/setup.py index 594c88b9..deb52c27 100644 --- a/setup.py +++ b/setup.py @@ -4,8 +4,10 @@ Sanic import codecs import os import re -from setuptools import setup +from distutils.errors import DistutilsPlatformError +from distutils.util import strtobool +from setuptools import setup with codecs.open(os.path.join(os.path.abspath(os.path.dirname( __file__)), 'sanic', '__init__.py'), 'r', 'latin1') as fp: @@ -15,7 +17,7 @@ with codecs.open(os.path.join(os.path.abspath(os.path.dirname( except IndexError: raise RuntimeError('Unable to determine version.') -setup_kwargs = { +setup_kwargs = { 'name': 'sanic', 'version': version, 'url': 'http://github.com/channelcat/sanic/', @@ -35,23 +37,32 @@ setup_kwargs = { ], } +ujson = 'ujson>=1.35' +uvloop = 'uvloop>=0.5.3' + +requirements = [ + 'httptools>=0.0.9', + uvloop, + ujson, + 'aiofiles>=0.3.0', + 'websockets>=3.2', +] +if strtobool(os.environ.get("SANIC_NO_UJSON", "no")): + print("Installing without uJSON") + requirements.remove(ujson) + +if strtobool(os.environ.get("SANIC_NO_UVLOOP", "no")): + print("Installing without uvLoop") + requirements.remove(uvloop) + try: - normal_requirements = [ - 'httptools>=0.0.9', - 'uvloop>=0.5.3', - 'ujson>=1.35', - 'aiofiles>=0.3.0', - 'websockets>=3.2', - ] - setup_kwargs['install_requires'] = normal_requirements + setup_kwargs['install_requires'] = requirements setup(**setup_kwargs) except DistutilsPlatformError as exception: - windows_requirements = [ - 'httptools>=0.0.9', - 'aiofiles>=0.3.0', - 'websockets>=3.2', - ] - setup_kwargs['install_requires'] = windows_requirements + requirements.remove(ujson) + requirements.remove(uvloop) + print("Installing without uJSON or uvLoop") + setup_kwargs['install_requires'] = requirements setup(**setup_kwargs) # Installation was successful diff --git a/tests/test_payload_too_large.py b/tests/test_payload_too_large.py index a1a58d3d..6c67fd9c 100644 --- a/tests/test_payload_too_large.py +++ b/tests/test_payload_too_large.py @@ -1,6 +1,8 @@ +import pytest + from sanic import Sanic -from sanic.response import text from sanic.exceptions import PayloadTooLarge +from sanic.response import text data_received_app = Sanic('data_received') data_received_app.config.REQUEST_MAX_SIZE = 1 @@ -43,6 +45,7 @@ async def handler3(request): return text('OK') +@pytest.mark.skip # see: https://github.com/channelcat/sanic/issues/598 def test_payload_too_large_at_on_header_default(): data = 'a' * 1000 response = on_header_default_app.test_client.post( diff --git a/tests/test_request_data.py b/tests/test_request_data.py index c874f71d..c2493ffe 100644 --- a/tests/test_request_data.py +++ b/tests/test_request_data.py @@ -2,7 +2,7 @@ import random from sanic import Sanic from sanic.response import json -from ujson import loads +from json import loads def test_storage(): diff --git a/tests/test_request_timeout.py b/tests/test_request_timeout.py index 404aec12..7eba7c5a 100644 --- a/tests/test_request_timeout.py +++ b/tests/test_request_timeout.py @@ -1,8 +1,11 @@ -from sanic import Sanic import asyncio -from sanic.response import text -from sanic.exceptions import RequestTimeout + +import pytest + +from sanic import Sanic from sanic.config import Config +from sanic.exceptions import RequestTimeout +from sanic.response import text Config.REQUEST_TIMEOUT = 1 request_timeout_app = Sanic('test_request_timeout') @@ -20,6 +23,7 @@ def handler_exception(request, exception): return text('Request Timeout from error_handler.', 408) +@pytest.mark.skip # see: https://github.com/channelcat/sanic/issues/598 def test_server_error_request_timeout(): request, response = request_timeout_app.test_client.get('/1') assert response.status == 408 @@ -32,6 +36,7 @@ async def handler_2(request): return text('OK') +@pytest.mark.skip # see: https://github.com/channelcat/sanic/issues/598 def test_default_server_error_request_timeout(): request, response = request_timeout_default_app.test_client.get('/1') assert response.status == 408 From 22699db8550fc06674314bdca7463a4437243972 Mon Sep 17 00:00:00 2001 From: Jakob Bowyer Date: Wed, 29 Mar 2017 09:16:53 +0100 Subject: [PATCH 2/5] Moved skips to seperate pull request --- tests/test_payload_too_large.py | 3 --- tests/test_request_timeout.py | 4 ---- 2 files changed, 7 deletions(-) diff --git a/tests/test_payload_too_large.py b/tests/test_payload_too_large.py index 6c67fd9c..95b5de5b 100644 --- a/tests/test_payload_too_large.py +++ b/tests/test_payload_too_large.py @@ -1,5 +1,3 @@ -import pytest - from sanic import Sanic from sanic.exceptions import PayloadTooLarge from sanic.response import text @@ -45,7 +43,6 @@ async def handler3(request): return text('OK') -@pytest.mark.skip # see: https://github.com/channelcat/sanic/issues/598 def test_payload_too_large_at_on_header_default(): data = 'a' * 1000 response = on_header_default_app.test_client.post( diff --git a/tests/test_request_timeout.py b/tests/test_request_timeout.py index 7eba7c5a..40bc364c 100644 --- a/tests/test_request_timeout.py +++ b/tests/test_request_timeout.py @@ -1,7 +1,5 @@ import asyncio -import pytest - from sanic import Sanic from sanic.config import Config from sanic.exceptions import RequestTimeout @@ -23,7 +21,6 @@ def handler_exception(request, exception): return text('Request Timeout from error_handler.', 408) -@pytest.mark.skip # see: https://github.com/channelcat/sanic/issues/598 def test_server_error_request_timeout(): request, response = request_timeout_app.test_client.get('/1') assert response.status == 408 @@ -36,7 +33,6 @@ async def handler_2(request): return text('OK') -@pytest.mark.skip # see: https://github.com/channelcat/sanic/issues/598 def test_default_server_error_request_timeout(): request, response = request_timeout_default_app.test_client.get('/1') assert response.status == 408 From 1cf730d95746240190531d7aa5d6e43f8d68b3a6 Mon Sep 17 00:00:00 2001 From: Jakob Bowyer Date: Wed, 29 Mar 2017 10:12:24 +0100 Subject: [PATCH 3/5] Added usage documentation for optional installs --- README.rst | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.rst b/README.rst index 3f565f71..878ba24c 100644 --- a/README.rst +++ b/README.rst @@ -59,6 +59,13 @@ Installation - ``python -m pip install sanic`` +To install sanic without uvloop or json using bash, you can provide either or both of these environmental variables +using any truthy string like `'y', 'yes', 't', 'true', 'on', '1'` and setting the NO_X to true will stop that features +installation. + +- ``SANIC_NO_UVLOOP=true SANIC_NO_UJSON=true python -m pip install sanic`` + + Documentation ------------- From daedda8547cdcbc852b8e3e0b283053ce494c209 Mon Sep 17 00:00:00 2001 From: Jakob Bowyer Date: Fri, 31 Mar 2017 08:51:12 +0100 Subject: [PATCH 4/5] Checked out original tests --- sanic/config.py | 1 + 1 file changed, 1 insertion(+) diff --git a/sanic/config.py b/sanic/config.py index 3b9a102a..3aa2189c 100644 --- a/sanic/config.py +++ b/sanic/config.py @@ -1,4 +1,5 @@ import os + import types From edd8770c67f7e2b0938942848f344d3abecc47e8 Mon Sep 17 00:00:00 2001 From: Jakob Bowyer Date: Fri, 31 Mar 2017 08:53:46 +0100 Subject: [PATCH 5/5] Restored tests to upstream/master --- tests/test_request_data.py | 2 +- tests/test_request_timeout.py | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/tests/test_request_data.py b/tests/test_request_data.py index c2493ffe..c874f71d 100644 --- a/tests/test_request_data.py +++ b/tests/test_request_data.py @@ -2,7 +2,7 @@ import random from sanic import Sanic from sanic.response import json -from json import loads +from ujson import loads def test_storage(): diff --git a/tests/test_request_timeout.py b/tests/test_request_timeout.py index 40bc364c..404aec12 100644 --- a/tests/test_request_timeout.py +++ b/tests/test_request_timeout.py @@ -1,9 +1,8 @@ -import asyncio - from sanic import Sanic -from sanic.config import Config -from sanic.exceptions import RequestTimeout +import asyncio from sanic.response import text +from sanic.exceptions import RequestTimeout +from sanic.config import Config Config.REQUEST_TIMEOUT = 1 request_timeout_app = Sanic('test_request_timeout')