From b2e34d2ba4e032746599bd200ac9cf956564a4da Mon Sep 17 00:00:00 2001 From: Quentin Pradet Date: Fri, 24 Aug 2018 16:52:14 +0400 Subject: [PATCH] Remove more Python 2.6 code (#1430) --- dev-requirements.txt | 4 +++- src/urllib3/connection.py | 10 ++-------- src/urllib3/connectionpool.py | 2 +- .../ssl_match_hostname/_implementation.py | 3 +-- test/test_response.py | 17 ----------------- test/with_dummyserver/test_https.py | 8 +++++++- test/with_dummyserver/test_socketlevel.py | 10 +--------- 7 files changed, 15 insertions(+), 39 deletions(-) diff --git a/dev-requirements.txt b/dev-requirements.txt index 9a2a962a6e..abbe781a09 100644 --- a/dev-requirements.txt +++ b/dev-requirements.txt @@ -8,4 +8,6 @@ PySocks==1.6.8 pkginfo==1.4.2 pytest-timeout==1.3.1 pytest==3.6.4 -gcp-devrel-py-tools==0.0.15 \ No newline at end of file +# https://github.com/GoogleCloudPlatform/python-repo-tools/issues/23 +pylint<2.0;python_version<="2.7" +gcp-devrel-py-tools==0.0.15 diff --git a/src/urllib3/connection.py b/src/urllib3/connection.py index 7c9a9f3b93..f8bc8b6b73 100644 --- a/src/urllib3/connection.py +++ b/src/urllib3/connection.py @@ -171,10 +171,7 @@ def _new_conn(self): def _prepare_conn(self, conn): self.sock = conn - # the _tunnel_host attribute was added in python 2.6.3 (via - # http://hg.python.org/cpython/rev/0f57b30a152f) so pythons 2.6(0-2) do - # not have them. - if getattr(self, '_tunnel_host', None): + if self._tunnel_host: # TODO: Fix tunnel so it doesn't depend on self.sock state. self._tunnel() # Mark this connection as not reusable @@ -302,12 +299,9 @@ def set_cert(self, key_file=None, cert_file=None, def connect(self): # Add certificate verification conn = self._new_conn() - hostname = self.host - if getattr(self, '_tunnel_host', None): - # _tunnel_host was added in Python 2.6.3 - # (See: http://hg.python.org/cpython/rev/0f57b30a152f) + if self._tunnel_host: self.sock = conn # Calls self._set_hostport(), so self.host is # self._tunnel_host below. diff --git a/src/urllib3/connectionpool.py b/src/urllib3/connectionpool.py index 8e58fa743f..f7a8f193d1 100644 --- a/src/urllib3/connectionpool.py +++ b/src/urllib3/connectionpool.py @@ -313,7 +313,7 @@ def _raise_timeout(self, err, url, timeout_value): # Catch possible read timeouts thrown as SSL errors. If not the # case, rethrow the original. We need to do this because of: # http://bugs.python.org/issue10272 - if 'timed out' in str(err) or 'did not complete (read)' in str(err): # Python 2.6 + if 'timed out' in str(err) or 'did not complete (read)' in str(err): # Python < 2.7.4 raise ReadTimeoutError(self, url, "Read timed out. (read timeout=%s)" % timeout_value) def _make_request(self, conn, method, url, timeout=_Default, chunked=False, diff --git a/src/urllib3/packages/ssl_match_hostname/_implementation.py b/src/urllib3/packages/ssl_match_hostname/_implementation.py index 1fd42f38ae..d6e66c0196 100644 --- a/src/urllib3/packages/ssl_match_hostname/_implementation.py +++ b/src/urllib3/packages/ssl_match_hostname/_implementation.py @@ -9,8 +9,7 @@ # ipaddress has been backported to 2.6+ in pypi. If it is installed on the # system, use it to handle IPAddress ServerAltnames (this was added in # python-3.5) otherwise only do DNS matching. This allows -# backports.ssl_match_hostname to continue to be used all the way back to -# python-2.4. +# backports.ssl_match_hostname to continue to be used in Python 2.7. try: import ipaddress except ImportError: diff --git a/test/test_response.py b/test/test_response.py index 0b347c25c8..79a879003e 100644 --- a/test/test_response.py +++ b/test/test_response.py @@ -291,23 +291,6 @@ def test_io_bufferedreader(self): while not br.closed: br.read(5) - def test_io_readinto(self): - # This test is necessary because in py2.6, `readinto` doesn't get called - # in `test_io_bufferedreader` like it does for all the other python - # versions. Probably this is because the `io` module in py2.6 is an - # old version that has a different underlying implementation. - - fp = BytesIO(b'foo') - resp = HTTPResponse(fp, preload_content=False) - - barr = bytearray(3) - assert resp.readinto(barr) == 3 - assert b'foo' == barr - - # The reader should already be empty, so this should read nothing. - assert resp.readinto(barr) == 0 - assert b'foo' == barr - def test_streaming(self): fp = BytesIO(b'foo') resp = HTTPResponse(fp, preload_content=False) diff --git a/test/with_dummyserver/test_https.py b/test/with_dummyserver/test_https.py index 41c1d5a723..082ede96f8 100644 --- a/test/with_dummyserver/test_https.py +++ b/test/with_dummyserver/test_https.py @@ -38,6 +38,7 @@ SystemTimeWarning, InsecurePlatformWarning, MaxRetryError, + ProtocolError, ) from urllib3.packages import six from urllib3.util.timeout import Timeout @@ -101,7 +102,12 @@ def test_client_no_intermediate(self): 'invalid certificate chain' in str(e) or 'unknown Cert Authority' in str(e) or # https://github.com/urllib3/urllib3/issues/1422 - 'connection closed via error' in str(e)): + 'connection closed via error' in str(e) or + 'WSAECONNRESET' in str(e)): + raise + except ProtocolError as e: + # https://github.com/urllib3/urllib3/issues/1422 + if not ('An existing connection was forcibly closed by the remote host' in str(e)): raise def test_verified(self): diff --git a/test/with_dummyserver/test_socketlevel.py b/test/with_dummyserver/test_socketlevel.py index a1896cfd34..ec2f01d227 100644 --- a/test/with_dummyserver/test_socketlevel.py +++ b/test/with_dummyserver/test_socketlevel.py @@ -639,13 +639,6 @@ def socket_handler(listener): def test_closing_response_actually_closes_connection(self): done_closing = Event() complete = Event() - # The insane use of this variable here is to get around the fact that - # Python 2.6 does not support returning a value from Event.wait(). This - # means we can't tell if an event timed out, so we can't use the timing - # out of the 'complete' event to determine the success or failure of - # the test. Python 2 also doesn't have the nonlocal statement, so we - # can't write directly to this variable, only mutate it. Hence: list. - successful = [] def socket_handler(listener): sock = listener.accept()[0] @@ -667,7 +660,6 @@ def socket_handler(listener): sock.settimeout(1) new_data = sock.recv(65536) self.assertFalse(new_data) - successful.append(True) sock.close() complete.set() @@ -680,7 +672,7 @@ def socket_handler(listener): response.close() done_closing.set() # wait until the socket in our pool gets closed - complete.wait(timeout=1) + successful = complete.wait(timeout=1) if not successful: self.fail("Timed out waiting for connection close")