Skip to content

Commit

Permalink
Remove unnecessary use of six.binary_type and six.b (urllib3#1440)
Browse files Browse the repository at this point in the history
The 'bytes' type is available on all supported Pythons. On Python 2 it
is an alias of str (same as six). Likewise, the bytes literal is also
available. Reduce unnecessary compatibility shims and use modern Python
idioms. Makes the code more forward compatible with Python 3.
  • Loading branch information
jdufresne authored and sethmlarson committed Sep 14, 2018
1 parent c01c3af commit a345e51
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/urllib3/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,13 @@ def request_chunked(self, method, url, body=None, headers=None):
self.endheaders()

if body is not None:
stringish_types = six.string_types + (six.binary_type,)
stringish_types = six.string_types + (bytes,)
if isinstance(body, stringish_types):
body = (body,)
for chunk in body:
if not chunk:
continue
if not isinstance(chunk, six.binary_type):
if not isinstance(chunk, bytes):
chunk = chunk.encode('utf8')
len_str = hex(len(chunk))[2:]
self.send(len_str.encode('utf-8'))
Expand Down
8 changes: 4 additions & 4 deletions src/urllib3/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
BodyNotHttplibCompatible, ProtocolError, DecodeError, ReadTimeoutError,
ResponseNotChunked, IncompleteRead, InvalidHeader
)
from .packages.six import string_types as basestring, binary_type, PY3
from .packages.six import string_types as basestring, PY3
from .packages.six.moves import http_client as httplib
from .connection import HTTPException, BaseSSLError
from .util.response import is_fp_closed, is_response_to_head
Expand All @@ -23,7 +23,7 @@ class DeflateDecoder(object):

def __init__(self):
self._first_try = True
self._data = binary_type()
self._data = b''
self._obj = zlib.decompressobj()

def __getattr__(self, name):
Expand Down Expand Up @@ -69,7 +69,7 @@ def __getattr__(self, name):
return getattr(self._obj, name)

def decompress(self, data):
ret = binary_type()
ret = b''
if self._state == GzipDecoderState.SWALLOW_DATA or not data:
return ret
while True:
Expand Down Expand Up @@ -159,7 +159,7 @@ def __init__(self, body='', headers=None, status=0, version=0, reason=None,
self.msg = msg
self._request_url = request_url

if body and isinstance(body, (basestring, binary_type)):
if body and isinstance(body, (basestring, bytes)):
self._body = body

self._pool = pool
Expand Down
4 changes: 2 additions & 2 deletions src/urllib3/util/ssl_.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def _const_compare_digest_backport(a, b):
import ipaddress

def inet_pton(_, host):
if isinstance(host, six.binary_type):
if isinstance(host, bytes):
host = host.decode('ascii')
return ipaddress.ip_address(host)

Expand Down Expand Up @@ -363,7 +363,7 @@ def is_ipaddress(hostname):
:param str hostname: Hostname to examine.
:return: True if the hostname is an IP address, False otherwise.
"""
if six.PY3 and isinstance(hostname, six.binary_type):
if six.PY3 and isinstance(hostname, bytes):
# IDN A-label bytes are ASCII compatible.
hostname = hostname.decode('ascii')

Expand Down
5 changes: 2 additions & 3 deletions test/test_ssl.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import mock
import pytest
from six import b
from urllib3.util import ssl_
from urllib3.exceptions import SNIMissingWarning

Expand All @@ -10,15 +9,15 @@
'::',
'127.0.0.1',
'8.8.8.8',
b('127.0.0.1')
b'127.0.0.1'
])
def test_is_ipaddress_true(addr):
assert ssl_.is_ipaddress(addr)


@pytest.mark.parametrize('addr', [
'www.python.org',
b('www.python.org')
b'www.python.org'
])
def test_is_ipaddress_false(addr):
assert not ssl_.is_ipaddress(addr)
Expand Down
3 changes: 1 addition & 2 deletions test/with_dummyserver/test_chunked_transfer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# -*- coding: utf-8 -*-

from urllib3 import HTTPConnectionPool
from urllib3.packages import six
from dummyserver.testcase import SocketDummyServerTestCase


Expand Down Expand Up @@ -50,7 +49,7 @@ def _test_body(self, data):

self.assertIn(b'Transfer-Encoding: chunked', header.split(b'\r\n'))
if data:
bdata = data if isinstance(data, six.binary_type) else data.encode('utf-8')
bdata = data if isinstance(data, bytes) else data.encode('utf-8')
self.assertIn(b'\r\n' + bdata + b'\r\n', body)
self.assertTrue(body.endswith(b'\r\n0\r\n\r\n'))

Expand Down

0 comments on commit a345e51

Please sign in to comment.