From db68c43de11014e68c1b79554f2dec2bbccb117a Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Thu, 1 Feb 2018 21:02:37 -0800 Subject: [PATCH] Fix imports and use of six --- .travis.yml | 2 +- daphne/http_protocol.py | 13 ++++--------- daphne/ws_protocol.py | 8 +++----- tests/http_strategies.py | 3 +-- 4 files changed, 9 insertions(+), 17 deletions(-) diff --git a/.travis.yml b/.travis.yml index f69f590..30cfe42 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,6 +17,6 @@ install: script: - pytest - flake8 - - isort --check-only --recursive daphne tests + - isort --check-only --diff --recursive daphne tests - unify --check-only --recursive --quote \" daphne tests diff --git a/daphne/http_protocol.py b/daphne/http_protocol.py index aca0c68..2b93133 100755 --- a/daphne/http_protocol.py +++ b/daphne/http_protocol.py @@ -1,15 +1,13 @@ import logging import time import traceback +from urllib.parse import unquote -import six from twisted.internet.interfaces import IProtocolNegotiationFactory from twisted.protocols.policies import ProtocolWrapper from twisted.web import http from zope.interface import implementer -from six.moves.urllib_parse import unquote - from .utils import parse_x_forwarded_for logger = logging.getLogger(__name__) @@ -68,8 +66,8 @@ class WebRequest(http.Request): if hasattr(self.client, "host") and hasattr(self.client, "port"): # client.host and host.host are byte strings in Python 2, but spec # requires unicode string. - self.client_addr = [six.text_type(self.client.host), self.client.port] - self.server_addr = [six.text_type(self.host.host), self.host.port] + self.client_addr = [str(self.client.host), self.client.port] + self.server_addr = [str(self.host.host), self.host.port] else: self.client_addr = None self.server_addr = None @@ -202,9 +200,6 @@ class WebRequest(http.Request): self.setResponseCode(message["status"]) # Write headers for header, value in message.get("headers", {}): - # Shim code from old ASGI version, can be removed after a while - if isinstance(header, six.text_type): - header = header.encode("latin1") self.responseHeaders.addRawHeader(header, value) logger.debug("HTTP %s response started for %s", message["status"], self.client_addr) elif message["type"] == "http.response.body": @@ -283,7 +278,7 @@ class WebRequest(http.Request): self.handle_reply({ "type": "http.response.body", "body": (self.error_template % { - "title": six.text_type(status) + " " + status_text.decode("ascii"), + "title": str(status) + " " + status_text.decode("ascii"), "body": body, }).encode("utf8"), }) diff --git a/daphne/ws_protocol.py b/daphne/ws_protocol.py index 9dfa339..ffb91eb 100755 --- a/daphne/ws_protocol.py +++ b/daphne/ws_protocol.py @@ -1,13 +1,11 @@ import logging import time import traceback +from urllib.parse import unquote -import six from autobahn.twisted.websocket import ConnectionDeny, WebSocketServerFactory, WebSocketServerProtocol from twisted.internet import defer -from six.moves.urllib_parse import unquote - from .utils import parse_x_forwarded_for logger = logging.getLogger(__name__) @@ -44,8 +42,8 @@ class WebSocketProtocol(WebSocketServerProtocol): peer = self.transport.getPeer() host = self.transport.getHost() if hasattr(peer, "host") and hasattr(peer, "port"): - self.client_addr = [six.text_type(peer.host), peer.port] - self.server_addr = [six.text_type(host.host), host.port] + self.client_addr = [str(peer.host), peer.port] + self.server_addr = [str(host.host), host.port] else: self.client_addr = None self.server_addr = None diff --git a/tests/http_strategies.py b/tests/http_strategies.py index 4789c05..f5a79d7 100644 --- a/tests/http_strategies.py +++ b/tests/http_strategies.py @@ -1,9 +1,8 @@ import string +from urllib import parse from hypothesis import strategies -from six.moves.urllib import parse - HTTP_METHODS = ["OPTIONS", "GET", "HEAD", "POST", "PUT", "DELETE", "TRACE", "CONNECT"] # Unicode characters of the "Letter" category