Merge pull request #1784 from tnir/1599-pycodestyle-ci

Enable pycodestyle
This commit is contained in:
Randall Leeds 2019-12-30 12:58:17 -08:00 committed by GitHub
commit e636bf8198
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 117 additions and 78 deletions

View File

@ -142,7 +142,7 @@ class Application(BaseApplication):
continue continue
try: try:
self.cfg.set(k.lower(), v) self.cfg.set(k.lower(), v)
except: except Exception:
print("Invalid value for %s: %s\n" % (k, v), file=sys.stderr) print("Invalid value for %s: %s\n" % (k, v), file=sys.stderr)
sys.stderr.flush() sys.stderr.flush()
raise raise
@ -203,7 +203,7 @@ class Application(BaseApplication):
if self.cfg.check_config: if self.cfg.check_config:
try: try:
self.load() self.load()
except: except Exception:
msg = "\nError while loading the application:\n" msg = "\nError while loading the application:\n"
print(msg, file=sys.stderr) print(msg, file=sys.stderr)
traceback.print_exc() traceback.print_exc()

View File

@ -590,7 +590,7 @@ class Arbiter(object):
print("%s" % e, file=sys.stderr) print("%s" % e, file=sys.stderr)
sys.stderr.flush() sys.stderr.flush()
sys.exit(self.APP_LOAD_ERROR) sys.exit(self.APP_LOAD_ERROR)
except: except Exception:
self.log.exception("Exception in worker process") self.log.exception("Exception in worker process")
if not worker.booted: if not worker.booted:
sys.exit(self.WORKER_BOOT_ERROR) sys.exit(self.WORKER_BOOT_ERROR)
@ -600,7 +600,7 @@ class Arbiter(object):
try: try:
worker.tmp.close() worker.tmp.close()
self.cfg.worker_exit(self, worker) self.cfg.worker_exit(self, worker)
except: except Exception:
self.log.warning("Exception during worker exit:\n%s", self.log.warning("Exception during worker exit:\n%s",
traceback.format_exc()) traceback.format_exc())

View File

@ -93,7 +93,7 @@ class Config(object):
def worker_class_str(self): def worker_class_str(self):
uri = self.settings['worker_class'].get() uri = self.settings['worker_class'].get()
## are we using a threaded worker? # are we using a threaded worker?
is_sync = uri.endswith('SyncWorker') or uri == 'sync' is_sync = uri.endswith('SyncWorker') or uri == 'sync'
if is_sync and self.threads > 1: if is_sync and self.threads > 1:
return "threads" return "threads"
@ -103,7 +103,7 @@ class Config(object):
def worker_class(self): def worker_class(self):
uri = self.settings['worker_class'].get() uri = self.settings['worker_class'].get()
## are we using a threaded worker? # are we using a threaded worker?
is_sync = uri.endswith('SyncWorker') or uri == 'sync' is_sync = uri.endswith('SyncWorker') or uri == 'sync'
if is_sync and self.threads > 1: if is_sync and self.threads > 1:
uri = "gunicorn.workers.gthread.ThreadWorker" uri = "gunicorn.workers.gthread.ThreadWorker"
@ -550,6 +550,7 @@ class ConfigFile(Setting):
prefix. prefix.
""" """
class Bind(Setting): class Bind(Setting):
name = "bind" name = "bind"
action = "append" action = "append"
@ -654,6 +655,7 @@ class WorkerClass(Setting):
``gunicorn.workers.ggevent.GeventWorker``. ``gunicorn.workers.ggevent.GeventWorker``.
""" """
class WorkerThreads(Setting): class WorkerThreads(Setting):
name = "threads" name = "threads"
section = "Worker Processes" section = "Worker Processes"
@ -1025,6 +1027,7 @@ class Daemon(Setting):
background. background.
""" """
class Env(Setting): class Env(Setting):
name = "raw_env" name = "raw_env"
action = "append" action = "append"
@ -1058,6 +1061,7 @@ class Pidfile(Setting):
If not set, no PID file will be written. If not set, no PID file will be written.
""" """
class WorkerTmpDir(Setting): class WorkerTmpDir(Setting):
name = "worker_tmp_dir" name = "worker_tmp_dir"
section = "Server Mechanics" section = "Server Mechanics"
@ -1111,6 +1115,7 @@ class Group(Setting):
change the worker processes group. change the worker processes group.
""" """
class Umask(Setting): class Umask(Setting):
name = "umask" name = "umask"
section = "Server Mechanics" section = "Server Mechanics"
@ -1224,6 +1229,7 @@ class AccessLog(Setting):
``'-'`` means log to stdout. ``'-'`` means log to stdout.
""" """
class DisableRedirectAccessToSyslog(Setting): class DisableRedirectAccessToSyslog(Setting):
name = "disable_redirect_access_to_syslog" name = "disable_redirect_access_to_syslog"
section = "Logging" section = "Logging"
@ -1676,6 +1682,7 @@ class PostWorkerInit(Setting):
Worker. Worker.
""" """
class WorkerInt(Setting): class WorkerInt(Setting):
name = "worker_int" name = "worker_int"
section = "Server Hooks" section = "Server Hooks"
@ -1819,6 +1826,7 @@ class NumWorkersChanged(Setting):
be ``None``. be ``None``.
""" """
class OnExit(Setting): class OnExit(Setting):
name = "on_exit" name = "on_exit"
section = "Server Hooks" section = "Server Hooks"
@ -1899,6 +1907,7 @@ class CertFile(Setting):
SSL certificate file SSL certificate file
""" """
class SSLVersion(Setting): class SSLVersion(Setting):
name = "ssl_version" name = "ssl_version"
section = "SSL" section = "SSL"
@ -1945,6 +1954,7 @@ class SSLVersion(Setting):
constants. constants.
""" """
class CertReqs(Setting): class CertReqs(Setting):
name = "cert_reqs" name = "cert_reqs"
section = "SSL" section = "SSL"
@ -1955,6 +1965,7 @@ class CertReqs(Setting):
Whether client certificate is required (see stdlib ssl module's) Whether client certificate is required (see stdlib ssl module's)
""" """
class CACerts(Setting): class CACerts(Setting):
name = "ca_certs" name = "ca_certs"
section = "SSL" section = "SSL"
@ -1966,6 +1977,7 @@ class CACerts(Setting):
CA certificates file CA certificates file
""" """
class SuppressRaggedEOFs(Setting): class SuppressRaggedEOFs(Setting):
name = "suppress_ragged_eofs" name = "suppress_ragged_eofs"
section = "SSL" section = "SSL"
@ -1977,6 +1989,7 @@ class SuppressRaggedEOFs(Setting):
Suppress ragged EOFs (see stdlib ssl module's) Suppress ragged EOFs (see stdlib ssl module's)
""" """
class DoHandshakeOnConnect(Setting): class DoHandshakeOnConnect(Setting):
name = "do_handshake_on_connect" name = "do_handshake_on_connect"
section = "SSL" section = "SSL"

View File

@ -7,7 +7,7 @@ import base64
import binascii import binascii
import time import time
import logging import logging
logging.Logger.manager.emittedNoHandlerWarning = 1 logging.Logger.manager.emittedNoHandlerWarning = 1 # noqa
from logging.config import dictConfig from logging.config import dictConfig
from logging.config import fileConfig from logging.config import fileConfig
import os import os
@ -213,8 +213,10 @@ class Logger(object):
# set gunicorn.access handler # set gunicorn.access handler
if cfg.accesslog is not None: if cfg.accesslog is not None:
self._set_handler(self.access_log, cfg.accesslog, self._set_handler(
fmt=logging.Formatter(self.access_fmt), stream=sys.stdout) self.access_log, cfg.accesslog,
fmt=logging.Formatter(self.access_fmt), stream=sys.stdout
)
# set syslog handler # set syslog handler
if cfg.syslog: if cfg.syslog:
@ -284,7 +286,8 @@ class Logger(object):
'u': self._get_user(environ) or '-', 'u': self._get_user(environ) or '-',
't': self.now(), 't': self.now(),
'r': "%s %s %s" % (environ['REQUEST_METHOD'], 'r': "%s %s %s" % (environ['REQUEST_METHOD'],
environ['RAW_URI'], environ["SERVER_PROTOCOL"]), environ['RAW_URI'],
environ["SERVER_PROTOCOL"]),
's': status, 's': status,
'm': environ.get('REQUEST_METHOD'), 'm': environ.get('REQUEST_METHOD'),
'U': environ.get('PATH_INFO'), 'U': environ.get('PATH_INFO'),
@ -337,12 +340,13 @@ class Logger(object):
# wrap atoms: # wrap atoms:
# - make sure atoms will be test case insensitively # - make sure atoms will be test case insensitively
# - if atom doesn't exist replace it by '-' # - if atom doesn't exist replace it by '-'
safe_atoms = self.atoms_wrapper_class(self.atoms(resp, req, environ, safe_atoms = self.atoms_wrapper_class(
request_time)) self.atoms(resp, req, environ, request_time)
)
try: try:
self.access_log.info(self.cfg.access_log_format, safe_atoms) self.access_log.info(self.cfg.access_log_format, safe_atoms)
except: except Exception:
self.error(traceback.format_exc()) self.error(traceback.format_exc())
def now(self): def now(self):
@ -361,7 +365,6 @@ class Logger(object):
os.dup2(self.logfile.fileno(), sys.stdout.fileno()) os.dup2(self.logfile.fileno(), sys.stdout.fileno())
os.dup2(self.logfile.fileno(), sys.stderr.fileno()) os.dup2(self.logfile.fileno(), sys.stderr.fileno())
for log in loggers(): for log in loggers():
for handler in log.handlers: for handler in log.handlers:
if isinstance(handler, logging.FileHandler): if isinstance(handler, logging.FileHandler):

View File

@ -10,9 +10,11 @@ from errno import ENOTCONN
from gunicorn.http.unreader import SocketUnreader from gunicorn.http.unreader import SocketUnreader
from gunicorn.http.body import ChunkedReader, LengthReader, EOFReader, Body from gunicorn.http.body import ChunkedReader, LengthReader, EOFReader, Body
from gunicorn.http.errors import (InvalidHeader, InvalidHeaderName, NoMoreData, from gunicorn.http.errors import (
InvalidHeader, InvalidHeaderName, NoMoreData,
InvalidRequestLine, InvalidRequestMethod, InvalidHTTPVersion, InvalidRequestLine, InvalidRequestMethod, InvalidHTTPVersion,
LimitRequestLine, LimitRequestHeaders) LimitRequestLine, LimitRequestHeaders,
)
from gunicorn.http.errors import InvalidProxyLine, ForbiddenProxyRequest from gunicorn.http.errors import InvalidProxyLine, ForbiddenProxyRequest
from gunicorn.http.errors import InvalidSchemeHeaders from gunicorn.http.errors import InvalidSchemeHeaders
from gunicorn.util import bytes_to_str, split_request_uri from gunicorn.util import bytes_to_str, split_request_uri
@ -105,7 +107,7 @@ class Message(object):
header_length += len(curr) header_length += len(curr)
if header_length > self.limit_request_field_size > 0: if header_length > self.limit_request_field_size > 0:
raise LimitRequestHeaders("limit request headers " raise LimitRequestHeaders("limit request headers "
+ "fields size") "fields size")
value.append(curr) value.append(curr)
value = ''.join(value).rstrip() value = ''.join(value).rstrip()

View File

@ -19,6 +19,7 @@ GAUGE_TYPE = "gauge"
COUNTER_TYPE = "counter" COUNTER_TYPE = "counter"
HISTOGRAM_TYPE = "histogram" HISTOGRAM_TYPE = "histogram"
class Statsd(Logger): class Statsd(Logger):
"""statsD-based instrumentation, that passes as a logger """statsD-based instrumentation, that passes as a logger
""" """

View File

@ -57,7 +57,7 @@ class Pidfile(object):
if pid1 == self.pid: if pid1 == self.pid:
os.unlink(self.fname) os.unlink(self.fname)
except: except Exception:
pass pass
def validate(self): def validate(self):

View File

@ -53,6 +53,7 @@ class Reloader(threading.Thread):
self._callback(filename) self._callback(filename)
time.sleep(self._interval) time.sleep(self._interval)
has_inotify = False has_inotify = False
if sys.platform.startswith('linux'): if sys.platform.startswith('linux'):
try: try:

View File

@ -70,7 +70,7 @@ def load_class(uri, default="gunicorn.workers.sync.SyncWorker",
try: try:
return pkg_resources.load_entry_point(dist, section, name) return pkg_resources.load_entry_point(dist, section, name)
except: except Exception:
exc = traceback.format_exc() exc = traceback.format_exc()
msg = "class uri %r invalid or not found: \n\n[%s]" msg = "class uri %r invalid or not found: \n\n[%s]"
raise RuntimeError(msg % (uri, exc)) raise RuntimeError(msg % (uri, exc))
@ -86,9 +86,10 @@ def load_class(uri, default="gunicorn.workers.sync.SyncWorker",
break break
try: try:
return pkg_resources.load_entry_point("gunicorn", return pkg_resources.load_entry_point(
section, uri) "gunicorn", section, uri
except: )
except Exception:
exc = traceback.format_exc() exc = traceback.format_exc()
msg = "class uri %r invalid or not found: \n\n[%s]" msg = "class uri %r invalid or not found: \n\n[%s]"
raise RuntimeError(msg % (uri, exc)) raise RuntimeError(msg % (uri, exc))
@ -260,6 +261,7 @@ def close(sock):
except socket.error: except socket.error:
pass pass
try: try:
from os import closerange from os import closerange
except ImportError: except ImportError:
@ -439,7 +441,7 @@ def getcwd():
cwd = os.environ['PWD'] cwd = os.environ['PWD']
else: else:
cwd = os.getcwd() cwd = os.getcwd()
except: except Exception:
cwd = os.getcwd() cwd = os.getcwd()
return cwd return cwd

View File

@ -28,8 +28,9 @@ from gunicorn.workers.workertmp import WorkerTmp
class Worker(object): class Worker(object):
SIGNALS = [getattr(signal, "SIG%s" % x) SIGNALS = [getattr(signal, "SIG%s" % x) for x in (
for x in "ABRT HUP QUIT INT TERM USR1 USR2 WINCH CHLD".split()] "ABRT HUP QUIT INT TERM USR1 USR2 WINCH CHLD".split()
)]
PIPE = [] PIPE = []
@ -203,12 +204,14 @@ class Worker(object):
def handle_error(self, req, client, addr, exc): def handle_error(self, req, client, addr, exc):
request_start = datetime.now() request_start = datetime.now()
addr = addr or ('', -1) # unix socket case addr = addr or ('', -1) # unix socket case
if isinstance(exc, (InvalidRequestLine, InvalidRequestMethod, if isinstance(exc, (
InvalidRequestLine, InvalidRequestMethod,
InvalidHTTPVersion, InvalidHeader, InvalidHeaderName, InvalidHTTPVersion, InvalidHeader, InvalidHeaderName,
LimitRequestLine, LimitRequestHeaders, LimitRequestLine, LimitRequestHeaders,
InvalidProxyLine, ForbiddenProxyRequest, InvalidProxyLine, ForbiddenProxyRequest,
InvalidSchemeHeaders, InvalidSchemeHeaders,
SSLError)): SSLError,
)):
status_int = 400 status_int = 400
reason = "Bad Request" reason = "Bad Request"
@ -261,7 +264,7 @@ class Worker(object):
try: try:
util.write_error(client, status_int, reason, mesg) util.write_error(client, status_int, reason, mesg)
except: except Exception:
self.log.debug("Failed to send error message.") self.log.debug("Failed to send error message.")
def handle_winch(self, sig, fname): def handle_winch(self, sig, fname):

View File

@ -40,6 +40,7 @@ def _gevent_sendfile(fdout, fdin, offset, nbytes, _os_sendfile=os.sendfile):
else: else:
raise raise
def patch_sendfile(): def patch_sendfile():
setattr(os, "sendfile", _gevent_sendfile) setattr(os, "sendfile", _gevent_sendfile)
@ -129,7 +130,7 @@ class GeventWorker(AsyncWorker):
self.log.warning("Worker graceful timeout (pid:%s)" % self.pid) self.log.warning("Worker graceful timeout (pid:%s)" % self.pid)
for server in servers: for server in servers:
server.stop(timeout=1) server.stop(timeout=1)
except: except Exception:
pass pass
def handle(self, listener, client, addr): def handle(self, listener, client, addr):

View File

@ -123,8 +123,8 @@ class ThreadWorker(base.Worker):
# enqueue the job # enqueue the job
self.enqueue_req(conn) self.enqueue_req(conn)
except EnvironmentError as e: except EnvironmentError as e:
if e.errno not in (errno.EAGAIN, if e.errno not in (errno.EAGAIN, errno.ECONNABORTED,
errno.ECONNABORTED, errno.EWOULDBLOCK): errno.EWOULDBLOCK):
raise raise
def reuse_connection(self, conn, client): def reuse_connection(self, conn, client):
@ -253,7 +253,7 @@ class ThreadWorker(base.Worker):
else: else:
self.nr_conns -= 1 self.nr_conns -= 1
conn.close() conn.close()
except: except Exception:
# an exception happened, make sure to close the # an exception happened, make sure to close the
# socket. # socket.
self.nr_conns -= 1 self.nr_conns -= 1

View File

@ -17,9 +17,11 @@ import gunicorn.http.wsgi as wsgi
import gunicorn.util as util import gunicorn.util as util
import gunicorn.workers.base as base import gunicorn.workers.base as base
class StopWaiting(Exception): class StopWaiting(Exception):
""" exception raised to stop waiting for a connection """ """ exception raised to stop waiting for a connection """
class SyncWorker(base.Worker): class SyncWorker(base.Worker):
def accept(self, listener): def accept(self, listener):

View File

@ -35,7 +35,7 @@ class WorkerTmp(object):
# In Python 3.8, open() emits RuntimeWarning if buffering=1 for binary mode. # In Python 3.8, open() emits RuntimeWarning if buffering=1 for binary mode.
# Because we never write to this file, pass 0 to switch buffering off. # Because we never write to this file, pass 0 to switch buffering off.
self._tmp = os.fdopen(fd, 'w+b', 0) self._tmp = os.fdopen(fd, 'w+b', 0)
except: except Exception:
os.close(fd) os.close(fd)
raise raise

11
tox.ini
View File

@ -38,3 +38,14 @@ deps =
commands = commands =
rst-lint README.rst docs/README.rst rst-lint README.rst docs/README.rst
bash -c "(set -o pipefail; rst-lint --encoding utf-8 docs/source/*.rst | grep -v 'Unknown interpreted text role\|Unknown directive type'); test $? == 1" bash -c "(set -o pipefail; rst-lint --encoding utf-8 docs/source/*.rst | grep -v 'Unknown interpreted text role\|Unknown directive type'); test $? == 1"
[testenv:pycodestyle]
commands =
pycodestyle gunicorn \
--exclude=gunicorn/six.py
deps =
pycodestyle
[pycodestyle]
max-line-length = 120
ignore = E129,W503,W504,W606