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

View File

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

View File

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

View File

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

View File

@ -10,9 +10,11 @@ from errno import ENOTCONN
from gunicorn.http.unreader import SocketUnreader
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,
LimitRequestLine, LimitRequestHeaders)
LimitRequestLine, LimitRequestHeaders,
)
from gunicorn.http.errors import InvalidProxyLine, ForbiddenProxyRequest
from gunicorn.http.errors import InvalidSchemeHeaders
from gunicorn.util import bytes_to_str, split_request_uri
@ -105,7 +107,7 @@ class Message(object):
header_length += len(curr)
if header_length > self.limit_request_field_size > 0:
raise LimitRequestHeaders("limit request headers "
+ "fields size")
"fields size")
value.append(curr)
value = ''.join(value).rstrip()

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -17,9 +17,11 @@ import gunicorn.http.wsgi as wsgi
import gunicorn.util as util
import gunicorn.workers.base as base
class StopWaiting(Exception):
""" exception raised to stop waiting for a connection """
class SyncWorker(base.Worker):
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.
# Because we never write to this file, pass 0 to switch buffering off.
self._tmp = os.fdopen(fd, 'w+b', 0)
except:
except Exception:
os.close(fd)
raise

11
tox.ini
View File

@ -38,3 +38,14 @@ deps =
commands =
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"
[testenv:pycodestyle]
commands =
pycodestyle gunicorn \
--exclude=gunicorn/six.py
deps =
pycodestyle
[pycodestyle]
max-line-length = 120
ignore = E129,W503,W504,W606