diff options
| author | Aldo Cortesi <aldo@nullcube.com> | 2016-10-17 15:15:22 +1300 | 
|---|---|---|
| committer | Aldo Cortesi <aldo@nullcube.com> | 2016-10-17 15:18:47 +1300 | 
| commit | 8360f70024330eeeb5c53d29e4a05194f872b511 (patch) | |
| tree | 6bbdcfe54fcce1e41660ca07c9470f42debdec5b /netlib/http | |
| parent | 4918feb7252c76c95d85cd8b2b0334a22aaae274 (diff) | |
| download | mitmproxy-8360f70024330eeeb5c53d29e4a05194f872b511.tar.gz mitmproxy-8360f70024330eeeb5c53d29e4a05194f872b511.tar.bz2 mitmproxy-8360f70024330eeeb5c53d29e4a05194f872b511.zip | |
First-order conversion to Python3-only
- Zap various occurrences of Python2 in docs and scripts
- Remove six from netlib, and some other places where obvious project-wide
search and replace works.
Diffstat (limited to 'netlib/http')
| -rw-r--r-- | netlib/http/headers.py | 28 | ||||
| -rw-r--r-- | netlib/http/message.py | 29 | ||||
| -rw-r--r-- | netlib/http/request.py | 26 | ||||
| -rw-r--r-- | netlib/http/response.py | 7 | ||||
| -rw-r--r-- | netlib/http/url.py | 28 | 
5 files changed, 40 insertions, 78 deletions
| diff --git a/netlib/http/headers.py b/netlib/http/headers.py index b55874ca..7d46a88e 100644 --- a/netlib/http/headers.py +++ b/netlib/http/headers.py @@ -3,26 +3,19 @@ from __future__ import absolute_import, print_function, division  import re  import collections -import six  from netlib import multidict  from netlib import strutils  # See also: http://lucumr.pocoo.org/2013/7/2/the-updated-guide-to-unicode/ -if six.PY2:  # pragma: no cover -    def _native(x): -        return x -    def _always_bytes(x): -        strutils.always_bytes(x, "utf-8", "replace")  # raises a TypeError if x != str/bytes/None. -        return x -else: -    # While headers _should_ be ASCII, it's not uncommon for certain headers to be utf-8 encoded. -    def _native(x): -        return x.decode("utf-8", "surrogateescape") +# While headers _should_ be ASCII, it's not uncommon for certain headers to be utf-8 encoded. +def _native(x): +    return x.decode("utf-8", "surrogateescape") -    def _always_bytes(x): -        return strutils.always_bytes(x, "utf-8", "surrogateescape") + +def _always_bytes(x): +    return strutils.always_bytes(x, "utf-8", "surrogateescape")  class Headers(multidict.MultiDict): @@ -93,7 +86,7 @@ class Headers(multidict.MultiDict):          # content_type -> content-type          headers = {              _always_bytes(name).replace(b"_", b"-"): _always_bytes(value) -            for name, value in six.iteritems(headers) +            for name, value in headers.items()          }          self.update(headers) @@ -113,9 +106,6 @@ class Headers(multidict.MultiDict):          else:              return b"" -    if six.PY2:  # pragma: no cover -        __str__ = __bytes__ -      def __delitem__(self, key):          key = _always_bytes(key)          super(Headers, self).__delitem__(key) @@ -167,9 +157,9 @@ class Headers(multidict.MultiDict):          Returns:              The number of replacements made.          """ -        if isinstance(pattern, six.text_type): +        if isinstance(pattern, str):              pattern = strutils.escaped_str_to_bytes(pattern) -        if isinstance(repl, six.text_type): +        if isinstance(repl, str):              repl = strutils.escaped_str_to_bytes(repl)          pattern = re.compile(pattern, flags)          replacements = 0 diff --git a/netlib/http/message.py b/netlib/http/message.py index 0b64d4a6..13f908ca 100644 --- a/netlib/http/message.py +++ b/netlib/http/message.py @@ -3,24 +3,17 @@ from __future__ import absolute_import, print_function, division  import re  import warnings -import six -  from netlib import encoding, strutils, basetypes  from netlib.http import headers -if six.PY2:  # pragma: no cover -    def _native(x): -        return x -    def _always_bytes(x): -        return x -else: -    # While headers _should_ be ASCII, it's not uncommon for certain headers to be utf-8 encoded. -    def _native(x): -        return x.decode("utf-8", "surrogateescape") +# While headers _should_ be ASCII, it's not uncommon for certain headers to be utf-8 encoded. +def _native(x): +    return x.decode("utf-8", "surrogateescape") + -    def _always_bytes(x): -        return strutils.always_bytes(x, "utf-8", "surrogateescape") +def _always_bytes(x): +    return strutils.always_bytes(x, "utf-8", "surrogateescape")  class MessageData(basetypes.Serializable): @@ -194,7 +187,7 @@ class Message(basetypes.Serializable):              return "latin-1"      def get_text(self, strict=True): -        # type: (bool) -> six.text_type +        # type: (bool) -> str          """          The HTTP message body decoded with both content-encoding header (e.g. gzip)          and content-type header charset. @@ -214,7 +207,7 @@ class Message(basetypes.Serializable):          except ValueError:              if strict:                  raise -            return content.decode("utf8", "replace" if six.PY2 else "surrogateescape") +            return content.decode("utf8", "surrogateescape")      def set_text(self, text):          if text is None: @@ -230,7 +223,7 @@ class Message(basetypes.Serializable):              ct[2]["charset"] = "utf-8"              self.headers["content-type"] = headers.assemble_content_type(*ct)              enc = "utf8" -            self.content = text.encode(enc, "replace" if six.PY2 else "surrogateescape") +            self.content = text.encode(enc, "surrogateescape")      text = property(get_text, set_text) @@ -269,9 +262,9 @@ class Message(basetypes.Serializable):          Returns:              The number of replacements made.          """ -        if isinstance(pattern, six.text_type): +        if isinstance(pattern, str):              pattern = strutils.escaped_str_to_bytes(pattern) -        if isinstance(repl, six.text_type): +        if isinstance(repl, str):              repl = strutils.escaped_str_to_bytes(repl)          replacements = 0          if self.content: diff --git a/netlib/http/request.py b/netlib/http/request.py index e0aaa8a9..cccda13e 100644 --- a/netlib/http/request.py +++ b/netlib/http/request.py @@ -1,9 +1,7 @@  from __future__ import absolute_import, print_function, division  import re - -import six -from six.moves import urllib +import urllib  from netlib import multidict  from netlib import strutils @@ -34,19 +32,19 @@ class RequestData(message.MessageData):          timestamp_start=None,          timestamp_end=None      ): -        if isinstance(method, six.text_type): +        if isinstance(method, str):              method = method.encode("ascii", "strict") -        if isinstance(scheme, six.text_type): +        if isinstance(scheme, str):              scheme = scheme.encode("ascii", "strict") -        if isinstance(host, six.text_type): +        if isinstance(host, str):              host = host.encode("idna", "strict") -        if isinstance(path, six.text_type): +        if isinstance(path, str):              path = path.encode("ascii", "strict") -        if isinstance(http_version, six.text_type): +        if isinstance(http_version, str):              http_version = http_version.encode("ascii", "strict")          if not isinstance(headers, nheaders.Headers):              headers = nheaders.Headers(headers) -        if isinstance(content, six.text_type): +        if isinstance(content, str):              raise ValueError("Content must be bytes, not {}".format(type(content).__name__))          self.first_line_format = first_line_format @@ -89,9 +87,9 @@ class Request(message.Message):              Returns:                  The number of replacements made.          """ -        if isinstance(pattern, six.text_type): +        if isinstance(pattern, str):              pattern = strutils.escaped_str_to_bytes(pattern) -        if isinstance(repl, six.text_type): +        if isinstance(repl, str):              repl = strutils.escaped_str_to_bytes(repl)          c = super(Request, self).replace(pattern, repl, flags, count) @@ -147,10 +145,6 @@ class Request(message.Message):          Setting the host attribute also updates the host header, if present.          """ - -        if six.PY2:  # pragma: no cover -            return self.data.host -          if not self.data.host:              return self.data.host          try: @@ -160,7 +154,7 @@ class Request(message.Message):      @host.setter      def host(self, host): -        if isinstance(host, six.text_type): +        if isinstance(host, str):              try:                  # There's no non-strict mode for IDNA encoding.                  # We don't want this operation to fail though, so we try diff --git a/netlib/http/response.py b/netlib/http/response.py index ec19640d..30bec2df 100644 --- a/netlib/http/response.py +++ b/netlib/http/response.py @@ -1,6 +1,5 @@  from __future__ import absolute_import, print_function, division -import six  import time  from email.utils import parsedate_tz, formatdate, mktime_tz  from netlib import human @@ -27,13 +26,13 @@ class ResponseData(message.MessageData):          timestamp_start=None,          timestamp_end=None      ): -        if isinstance(http_version, six.text_type): +        if isinstance(http_version, str):              http_version = http_version.encode("ascii", "strict") -        if isinstance(reason, six.text_type): +        if isinstance(reason, str):              reason = reason.encode("ascii", "strict")          if not isinstance(headers, nheaders.Headers):              headers = nheaders.Headers(headers) -        if isinstance(content, six.text_type): +        if isinstance(content, str):              raise ValueError("Content must be bytes, not {}".format(type(content).__name__))          self.http_version = http_version diff --git a/netlib/http/url.py b/netlib/http/url.py index 076854b9..2878734a 100644 --- a/netlib/http/url.py +++ b/netlib/http/url.py @@ -1,5 +1,4 @@ -import six -from six.moves import urllib +import urllib  from netlib import utils @@ -41,7 +40,7 @@ def parse(url):      if not parsed.hostname:          raise ValueError("No hostname given") -    if isinstance(url, six.binary_type): +    if isinstance(url, bytes):          host = parsed.hostname          # this should not raise a ValueError, @@ -86,20 +85,14 @@ def encode(s):      """          Takes a list of (key, value) tuples and returns a urlencoded string.      """ -    if six.PY2: -        return urllib.parse.urlencode(s, False) -    else: -        return urllib.parse.urlencode(s, False, errors="surrogateescape") +    return urllib.parse.urlencode(s, False, errors="surrogateescape")  def decode(s):      """          Takes a urlencoded string and returns a list of surrogate-escaped (key, value) tuples.      """ -    if six.PY2: -        return urllib.parse.parse_qsl(s, keep_blank_values=True) -    else: -        return urllib.parse.parse_qsl(s, keep_blank_values=True, errors='surrogateescape') +    return urllib.parse.parse_qsl(s, keep_blank_values=True, errors='surrogateescape')  def quote(b, safe="/"): @@ -108,10 +101,7 @@ def quote(b, safe="/"):          An ascii-encodable str.      """      # type: (str) -> str -    if six.PY2: -        return urllib.parse.quote(b, safe=safe) -    else: -        return urllib.parse.quote(b, safe=safe, errors="surrogateescape") +    return urllib.parse.quote(b, safe=safe, errors="surrogateescape")  def unquote(s): @@ -122,11 +112,7 @@ def unquote(s):          A surrogate-escaped str      """      # type: (str) -> str - -    if six.PY2: -        return urllib.parse.unquote(s) -    else: -        return urllib.parse.unquote(s, errors="surrogateescape") +    return urllib.parse.unquote(s, errors="surrogateescape")  def hostport(scheme, host, port): @@ -136,7 +122,7 @@ def hostport(scheme, host, port):      if (port, scheme) in [(80, "http"), (443, "https"), (80, b"http"), (443, b"https")]:          return host      else: -        if isinstance(host, six.binary_type): +        if isinstance(host, bytes):              return b"%s:%d" % (host, port)          else:              return "%s:%d" % (host, port) | 
