Skip to content

Commit

Permalink
Fix flake8 violations
Browse files Browse the repository at this point in the history
Add tox rule to prevent regressions.
  • Loading branch information
jayvdb committed Nov 2, 2015
1 parent 3bd6340 commit cb79107
Show file tree
Hide file tree
Showing 24 changed files with 137 additions and 73 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ env:
- GAE_PYTHONPATH=${HOME}/.cache/google_appengine
- PYTHONWARNINGS=always::DeprecationWarning
matrix:
- TOXENV=flake8-py3
- TOXENV=py26
- TOXENV=py27
- TOXENV=py32
Expand Down
3 changes: 3 additions & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -178,5 +178,8 @@ In chronological order:
* Evan Meagher <https://evanmeagher.net>
* Bugfix related to `memoryview` usage in PyOpenSSL adapter

* John Vandenberg <jayvdb@gmail.com>
* Python 2.6 fixes; pyflakes and pep8 compliance

* [Your name or handle] <[email or website]>
* [Brief summary of your changes]
11 changes: 6 additions & 5 deletions dummyserver/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ def __init__(self, body='', status='200 OK', headers=None):
def __call__(self, request_handler):
status, reason = self.status.split(' ', 1)
request_handler.set_status(int(status), reason)
for header,value in self.headers:
request_handler.add_header(header,value)
for header, value in self.headers:
request_handler.add_header(header, value)

# chunked
if isinstance(self.body, list):
Expand All @@ -48,6 +48,7 @@ def __call__(self, request_handler):

RETRY_TEST_NAMES = collections.defaultdict(int)


class TestingApp(RequestHandler):
"""
Simple app that performs various operations, useful for testing an HTTP
Expand Down Expand Up @@ -136,8 +137,8 @@ def upload(self, request):
files_ = request.files.get(param)

if len(files_) != 1:
return Response("Expected 1 file for '%s', not %d" %(param, len(files_)),
status='400 Bad Request')
return Response("Expected 1 file for '%s', not %d" % (param, len(files_)),
status='400 Bad Request')
file_ = files_[0]

data = file_['body']
Expand Down Expand Up @@ -277,7 +278,7 @@ def _parse_header(line):
value = p[i + 1:].strip()
params.append((name, value))
params = email.utils.decode_params(params)
params.pop(0) # get rid of the dummy again
params.pop(0) # get rid of the dummy again
pdict = {}
for name, value in params:
value = email.utils.collapse_rfc2231_value(value)
Expand Down
9 changes: 5 additions & 4 deletions dummyserver/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,23 @@ def get(self):

def handle_response(response):
if response.error and not isinstance(response.error,
tornado.httpclient.HTTPError):
tornado.httpclient.HTTPError):
self.set_status(500)
self.write('Internal server error:\n' + str(response.error))
self.finish()
else:
self.set_status(response.code)
for header in ('Date', 'Cache-Control', 'Server',
'Content-Type', 'Location'):
'Content-Type', 'Location'):
v = response.headers.get(header)
if v:
self.set_header(header, v)
if response.body:
self.write(response.body)
self.finish()

req = tornado.httpclient.HTTPRequest(url=self.request.uri,
req = tornado.httpclient.HTTPRequest(
url=self.request.uri,
method=self.request.method, body=self.request.body,
headers=self.request.headers, follow_redirects=False,
allow_nonstandard_methods=True)
Expand Down Expand Up @@ -133,5 +134,5 @@ def run_proxy(port, start_ioloop=True):
if len(sys.argv) > 1:
port = int(sys.argv[1])

print ("Starting HTTP proxy on port %d" % port)
print("Starting HTTP proxy on port %d" % port)
run_proxy(port)
1 change: 1 addition & 0 deletions dummyserver/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
NO_SAN_CA = os.path.join(CERTS_PATH, 'cacert.no_san.pem')
DEFAULT_CA_DIR = os.path.join(CERTS_PATH, 'ca_path_test')


def _has_ipv6(host):
""" Returns True if the system can bind an IPv6 address. """
sock = None
Expand Down
7 changes: 4 additions & 3 deletions dummyserver/testcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def _start_server(cls, socket_handler):
@classmethod
def start_response_handler(cls, response, num=1, block_send=None):
ready_event = threading.Event()

def socket_handler(listener):
for _ in range(num):
ready_event.set()
Expand All @@ -61,9 +62,9 @@ def socket_handler(listener):
@classmethod
def start_basic_handler(cls, **kw):
return cls.start_response_handler(
b'HTTP/1.1 200 OK\r\n'
b'Content-Length: 0\r\n'
b'\r\n', **kw)
b'HTTP/1.1 200 OK\r\n'
b'Content-Length: 0\r\n'
b'\r\n', **kw)

@classmethod
def tearDownClass(cls):
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ cover-min-percentage=100
cover-erase=true

[flake8]
exclude=./docs/conf.py,./test/*,./urllib3/packages/*
max-line-length=99

[wheel]
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import re

try:
import setuptools
import setuptools # noqa: unused
except ImportError:
pass # No 'develop' command, oh well.
pass # No 'develop' command, oh well.

base_path = os.path.dirname(__file__)

Expand Down
10 changes: 9 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tox]
envlist = py26, py27, py32, py33, py34, pypy
envlist = flake8-py3, py26, py27, py32, py33, py34, pypy

[testenv]
deps= -r{toxinidir}/dev-requirements.txt
Expand Down Expand Up @@ -27,3 +27,11 @@ commands=
setenv =
PYTHONPATH={env:GAE_PYTHONPATH:}
{[testenv]setenv}

[testenv:flake8-py3]
basepython = python3.4
deps=
flake8
commands=
flake8 --version
flake8
30 changes: 24 additions & 6 deletions urllib3/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
"""
urllib3 - Thread-safe connection pooling and re-using.
"""

__author__ = 'Andrey Petrov (andrey.petrov@shazow.net)'
__license__ = 'MIT'
__version__ = 'dev'

import warnings

from .connectionpool import (
HTTPConnectionPool,
Expand All @@ -32,8 +28,30 @@ class NullHandler(logging.Handler):
def emit(self, record):
pass

__author__ = 'Andrey Petrov (andrey.petrov@shazow.net)'
__license__ = 'MIT'
__version__ = 'dev'

__all__ = (
'HTTPConnectionPool',
'HTTPSConnectionPool',
'PoolManager',
'ProxyManager',
'HTTPResponse',
'Retry',
'Timeout',
'add_stderr_logger',
'connection_from_url',
'disable_warnings',
'encode_multipart_formdata',
'get_host',
'make_headers',
'proxy_from_url',
)

logging.getLogger(__name__).addHandler(NullHandler())


def add_stderr_logger(level=logging.DEBUG):
"""
Helper for quickly adding a StreamHandler to the logger. Useful for
Expand All @@ -55,7 +73,6 @@ def add_stderr_logger(level=logging.DEBUG):
del NullHandler


import warnings
# SecurityWarning's always go off by default.
warnings.simplefilter('always', exceptions.SecurityWarning, append=True)
# SubjectAltNameWarning's should go off once per host
Expand All @@ -64,6 +81,7 @@ def add_stderr_logger(level=logging.DEBUG):
warnings.simplefilter('default', exceptions.InsecurePlatformWarning,
append=True)


def disable_warnings(category=exceptions.HTTPWarning):
"""
Helper for quickly disabling all urllib3 warnings.
Expand Down
4 changes: 2 additions & 2 deletions urllib3/_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def __eq__(self, other):
def __ne__(self, other):
return not self.__eq__(other)

if not PY3: # Python 2
if not PY3: # Python 2
iterkeys = MutableMapping.iterkeys
itervalues = MutableMapping.itervalues

Expand Down Expand Up @@ -304,7 +304,7 @@ def items(self):
return list(self.iteritems())

@classmethod
def from_httplib(cls, message): # Python 2
def from_httplib(cls, message): # Python 2
"""Read headers from a Python 2 httplib message object."""
# python2.7 does not expose a proper API for exporting multiheaders
# efficiently. This function re-reads raw lines from the message
Expand Down
22 changes: 11 additions & 11 deletions urllib3/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,13 @@
from .packages import six

try: # Python 3
from http.client import HTTPConnection as _HTTPConnection, HTTPException
from http.client import HTTPConnection as _HTTPConnection
from http.client import HTTPException # noqa: unused in this module
except ImportError:
from httplib import HTTPConnection as _HTTPConnection, HTTPException


class DummyConnection(object):
"Used to detect a failed ConnectionCls import."
pass

from httplib import HTTPConnection as _HTTPConnection
from httplib import HTTPException # noqa: unused in this module

try: # Compiled with SSL?
HTTPSConnection = DummyConnection
import ssl
BaseSSLError = ssl.SSLError
except (ImportError, AttributeError): # Platform-specific: No SSL.
Expand Down Expand Up @@ -62,6 +57,11 @@ class ConnectionError(Exception):
RECENT_DATE = datetime.date(2014, 1, 1)


class DummyConnection(object):
"""Used to detect a failed ConnectionCls import."""
pass


class HTTPConnection(_HTTPConnection, object):
"""
Based on httplib.HTTPConnection but provides an extra constructor
Expand Down Expand Up @@ -266,8 +266,8 @@ def connect(self):
)
match_hostname(cert, self.assert_hostname or hostname)

self.is_verified = (resolved_cert_reqs == ssl.CERT_REQUIRED
or self.assert_fingerprint is not None)
self.is_verified = (resolved_cert_reqs == ssl.CERT_REQUIRED or
self.assert_fingerprint is not None)


if ssl:
Expand Down
30 changes: 16 additions & 14 deletions urllib3/connectionpool.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
from queue import LifoQueue, Empty, Full
except ImportError:
from Queue import LifoQueue, Empty, Full
import Queue as _ # Platform-specific: Windows
# Queue is imported for side effects on MS Windows
import Queue as _unused_module_Queue # noqa: unused


from .exceptions import (
Expand All @@ -22,7 +23,6 @@
LocationValueError,
MaxRetryError,
ProxyError,
ConnectTimeoutError,
ReadTimeoutError,
SSLError,
TimeoutError,
Expand All @@ -35,7 +35,7 @@
port_by_scheme,
DummyConnection,
HTTPConnection, HTTPSConnection, VerifiedHTTPSConnection,
HTTPException, BaseSSLError, ConnectionError
HTTPException, BaseSSLError,
)
from .request import RequestMethods
from .response import HTTPResponse
Expand All @@ -54,7 +54,7 @@
_Default = object()


## Pool objects
# Pool objects
class ConnectionPool(object):
"""
Base class for all connection pools, such as
Expand Down Expand Up @@ -644,22 +644,24 @@ def urlopen(self, method, url, body=None, headers=None, retries=None,
return response

log.info("Redirecting %s -> %s" % (url, redirect_location))
return self.urlopen(method, redirect_location, body, headers,
retries=retries, redirect=redirect,
assert_same_host=assert_same_host,
timeout=timeout, pool_timeout=pool_timeout,
release_conn=release_conn, **response_kw)
return self.urlopen(
method, redirect_location, body, headers,
retries=retries, redirect=redirect,
assert_same_host=assert_same_host,
timeout=timeout, pool_timeout=pool_timeout,
release_conn=release_conn, **response_kw)

# Check if we should retry the HTTP response.
if retries.is_forced_retry(method, status_code=response.status):
retries = retries.increment(method, url, response=response, _pool=self)
retries.sleep()
log.info("Forced retry: %s" % url)
return self.urlopen(method, url, body, headers,
retries=retries, redirect=redirect,
assert_same_host=assert_same_host,
timeout=timeout, pool_timeout=pool_timeout,
release_conn=release_conn, **response_kw)
return self.urlopen(
method, url, body, headers,
retries=retries, redirect=redirect,
assert_same_host=assert_same_host,
timeout=timeout, pool_timeout=pool_timeout,
release_conn=release_conn, **response_kw)

return response

Expand Down
2 changes: 1 addition & 1 deletion urllib3/contrib/appengine.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def _get_absolute_timeout(self, timeout):
if timeout is Timeout.DEFAULT_TIMEOUT:
return 5 # 5s is the default timeout for URLFetch.
if isinstance(timeout, Timeout):
if not timeout.read is timeout.connect:
if timeout.read is not timeout.connect:
warnings.warn(
"URLFetch does not support granular timeout settings, "
"reverting to total timeout.", AppEnginePlatformWarning)
Expand Down
10 changes: 5 additions & 5 deletions urllib3/contrib/pyopenssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@
_openssl_verify = {
ssl.CERT_NONE: OpenSSL.SSL.VERIFY_NONE,
ssl.CERT_OPTIONAL: OpenSSL.SSL.VERIFY_PEER,
ssl.CERT_REQUIRED: OpenSSL.SSL.VERIFY_PEER
+ OpenSSL.SSL.VERIFY_FAIL_IF_NO_PEER_CERT,
ssl.CERT_REQUIRED:
OpenSSL.SSL.VERIFY_PEER + OpenSSL.SSL.VERIFY_FAIL_IF_NO_PEER_CERT,
}

DEFAULT_SSL_CIPHER_LIST = util.ssl_.DEFAULT_CIPHERS
Expand All @@ -106,7 +106,7 @@ def extract_from_urllib3():
util.HAS_SNI = orig_util_HAS_SNI


### Note: This is a slightly bug-fixed version of same from ndg-httpsclient.
# Note: This is a slightly bug-fixed version of same from ndg-httpsclient.
class SubjectAltName(BaseSubjectAltName):
'''ASN.1 implementation for subjectAltNames support'''

Expand All @@ -117,7 +117,7 @@ class SubjectAltName(BaseSubjectAltName):
constraint.ValueSizeConstraint(1, 1024)


### Note: This is a slightly bug-fixed version of same from ndg-httpsclient.
# Note: This is a slightly bug-fixed version of same from ndg-httpsclient.
def get_subj_alt_name(peer_cert):
# Search through extensions
dns_name = []
Expand Down Expand Up @@ -208,7 +208,7 @@ def _send_until_done(self, data):
def sendall(self, data):
total_sent = 0
while total_sent < len(data):
sent = self._send_until_done(data[total_sent:total_sent+SSL_WRITE_BLOCKSIZE])
sent = self._send_until_done(data[total_sent:total_sent + SSL_WRITE_BLOCKSIZE])
total_sent += sent

def shutdown(self):
Expand Down
Loading

0 comments on commit cb79107

Please sign in to comment.