Fixing errors reported by pycodestyle

This commit is contained in:
samypr100 2023-01-22 21:20:11 -05:00
parent 48eda22a4b
commit 2ea4699fe7
No known key found for this signature in database
GPG Key ID: E1717AA77760955D
11 changed files with 86 additions and 84 deletions

View File

@ -154,7 +154,7 @@ class Arbiter(object):
self.LISTENERS = sock.create_sockets(self.cfg, self.log, fds) self.LISTENERS = sock.create_sockets(self.cfg, self.log, fds)
listeners_str = ",".join([str(l) for l in self.LISTENERS]) listeners_str = ",".join([str(lnr) for lnr in self.LISTENERS])
self.log.debug("Arbiter booted") self.log.debug("Arbiter booted")
self.log.info("Listening at: %s (%s)", listeners_str, self.pid) self.log.info("Listening at: %s (%s)", listeners_str, self.pid)
self.log.info("Using worker: %s", self.cfg.worker_class_str) self.log.info("Using worker: %s", self.cfg.worker_class_str)
@ -421,7 +421,7 @@ class Arbiter(object):
environ['LISTEN_FDS'] = str(len(self.LISTENERS)) environ['LISTEN_FDS'] = str(len(self.LISTENERS))
else: else:
environ['GUNICORN_FD'] = ','.join( environ['GUNICORN_FD'] = ','.join(
str(l.fileno()) for l in self.LISTENERS) str(lnr.fileno()) for lnr in self.LISTENERS)
os.chdir(self.START_CTX['cwd']) os.chdir(self.START_CTX['cwd'])
@ -454,11 +454,11 @@ class Arbiter(object):
# do we need to change listener ? # do we need to change listener ?
if old_address != self.cfg.address: if old_address != self.cfg.address:
# close all listeners # close all listeners
for l in self.LISTENERS: for lnr in self.LISTENERS:
l.close() lnr.close()
# init new listeners # init new listeners
self.LISTENERS = sock.create_sockets(self.cfg, self.log) self.LISTENERS = sock.create_sockets(self.cfg, self.log)
listeners_str = ",".join([str(l) for l in self.LISTENERS]) listeners_str = ",".join([str(lnr) for lnr in self.LISTENERS])
self.log.info("Listening at: %s", listeners_str) self.log.info("Listening at: %s", listeners_str)
# do some actions on reload # do some actions on reload

View File

@ -448,7 +448,7 @@ def validate_callable(arity):
raise TypeError(str(e)) raise TypeError(str(e))
except AttributeError: except AttributeError:
raise TypeError("Can not load '%s' from '%s'" raise TypeError("Can not load '%s' from '%s'"
"" % (obj_name, mod_name)) "" % (obj_name, mod_name))
if not callable(val): if not callable(val):
raise TypeError("Value is not callable: %s" % val) raise TypeError("Value is not callable: %s" % val)
if arity != -1 and arity != util.get_arity(val): if arity != -1 and arity != util.get_arity(val):
@ -563,6 +563,7 @@ class ConfigFile(Setting):
prefix. prefix.
""" """
class WSGIApp(Setting): class WSGIApp(Setting):
name = "wsgi_app" name = "wsgi_app"
section = "Config File" section = "Config File"
@ -575,6 +576,7 @@ class WSGIApp(Setting):
.. versionadded:: 20.1.0 .. versionadded:: 20.1.0
""" """
class Bind(Setting): class Bind(Setting):
name = "bind" name = "bind"
action = "append" action = "append"
@ -1273,69 +1275,70 @@ class ForwardedAllowIPS(Setting):
By default, the value of the ``FORWARDED_ALLOW_IPS`` environment By default, the value of the ``FORWARDED_ALLOW_IPS`` environment
variable. If it is not defined, the default is ``"127.0.0.1"``. variable. If it is not defined, the default is ``"127.0.0.1"``.
.. note:: .. note::
The interplay between the request headers, the value of ``forwarded_allow_ips``, and the value of The interplay between the request headers, the value of ``forwarded_allow_ips``, and the value of
``secure_scheme_headers`` is complex. Various scenarios are documented below to further elaborate. In each case, we ``secure_scheme_headers`` is complex. Various scenarios are documented below to further elaborate.
have a request from the remote address 134.213.44.18, and the default value of ``secure_scheme_headers``: In each case, we have a request from the remote address 134.213.44.18, and the default value of
``secure_scheme_headers``:
.. code:: .. code::
secure_scheme_headers = { secure_scheme_headers = {
'X-FORWARDED-PROTOCOL': 'ssl', 'X-FORWARDED-PROTOCOL': 'ssl',
'X-FORWARDED-PROTO': 'https', 'X-FORWARDED-PROTO': 'https',
'X-FORWARDED-SSL': 'on' 'X-FORWARDED-SSL': 'on'
} }
.. list-table:: .. list-table::
:header-rows: 1 :header-rows: 1
:align: center :align: center
:widths: auto :widths: auto
* - ``forwarded-allow-ips`` * - ``forwarded-allow-ips``
- Secure Request Headers - Secure Request Headers
- Result - Result
- Explanation - Explanation
* - .. code:: * - .. code::
["127.0.0.1"] ["127.0.0.1"]
- .. code:: - .. code::
X-Forwarded-Proto: https X-Forwarded-Proto: https
- .. code:: - .. code::
wsgi.url_scheme = "http" wsgi.url_scheme = "http"
- IP address was not allowed - IP address was not allowed
* - .. code:: * - .. code::
"*" "*"
- <none> - <none>
- .. code:: - .. code::
wsgi.url_scheme = "http" wsgi.url_scheme = "http"
- IP address allowed, but no secure headers provided - IP address allowed, but no secure headers provided
* - .. code:: * - .. code::
"*" "*"
- .. code:: - .. code::
X-Forwarded-Proto: https X-Forwarded-Proto: https
- .. code:: - .. code::
wsgi.url_scheme = "https" wsgi.url_scheme = "https"
- IP address allowed, one request header matched - IP address allowed, one request header matched
* - .. code:: * - .. code::
["134.213.44.18"] ["134.213.44.18"]
- .. code:: - .. code::
X-Forwarded-Ssl: on X-Forwarded-Ssl: on
X-Forwarded-Proto: http X-Forwarded-Proto: http
- ``InvalidSchemeHeaders()`` raised - ``InvalidSchemeHeaders()`` raised
- IP address allowed, but the two secure headers disagreed on if HTTPS was used - IP address allowed, but the two secure headers disagreed on if HTTPS was used
""" """
@ -1617,6 +1620,7 @@ class StatsdHost(Setting):
.. versionadded:: 19.1 .. versionadded:: 19.1
""" """
# Datadog Statsd (dogstatsd) tags. https://docs.datadoghq.com/developers/dogstatsd/ # Datadog Statsd (dogstatsd) tags. https://docs.datadoghq.com/developers/dogstatsd/
class DogstatsdTags(Setting): class DogstatsdTags(Setting):
name = "dogstatsd_tags" name = "dogstatsd_tags"
@ -1632,6 +1636,7 @@ class DogstatsdTags(Setting):
.. versionadded:: 20 .. versionadded:: 20
""" """
class StatsdPrefix(Setting): class StatsdPrefix(Setting):
name = "statsd_prefix" name = "statsd_prefix"
section = "Logging" section = "Logging"

View File

@ -28,7 +28,7 @@ class Spew(object):
if '__file__' in frame.f_globals: if '__file__' in frame.f_globals:
filename = frame.f_globals['__file__'] filename = frame.f_globals['__file__']
if (filename.endswith('.pyc') or if (filename.endswith('.pyc') or
filename.endswith('.pyo')): filename.endswith('.pyo')):
filename = filename[:-1] filename = filename[:-1]
name = frame.f_globals['__name__'] name = frame.f_globals['__name__']
line = linecache.getline(filename, lineno) line = linecache.getline(filename, lineno)

View File

@ -44,46 +44,45 @@ SYSLOG_FACILITIES = {
"local7": 23 "local7": 23
} }
CONFIG_DEFAULTS = dict( CONFIG_DEFAULTS = dict(
version=1, version=1,
disable_existing_loggers=False, disable_existing_loggers=False,
root={"level": "INFO", "handlers": ["console"]}, root={"level": "INFO", "handlers": ["console"]},
loggers={ loggers={
"gunicorn.error": { "gunicorn.error": {
"level": "INFO", "level": "INFO",
"handlers": ["error_console"], "handlers": ["error_console"],
"propagate": True, "propagate": True,
"qualname": "gunicorn.error" "qualname": "gunicorn.error"
}, },
"gunicorn.access": { "gunicorn.access": {
"level": "INFO", "level": "INFO",
"handlers": ["console"], "handlers": ["console"],
"propagate": True, "propagate": True,
"qualname": "gunicorn.access" "qualname": "gunicorn.access"
}
},
handlers={
"console": {
"class": "logging.StreamHandler",
"formatter": "generic",
"stream": "ext://sys.stdout"
},
"error_console": {
"class": "logging.StreamHandler",
"formatter": "generic",
"stream": "ext://sys.stderr"
},
},
formatters={
"generic": {
"format": "%(asctime)s [%(process)d] [%(levelname)s] %(message)s",
"datefmt": "[%Y-%m-%d %H:%M:%S %z]",
"class": "logging.Formatter"
}
} }
},
handlers={
"console": {
"class": "logging.StreamHandler",
"formatter": "generic",
"stream": "ext://sys.stdout"
},
"error_console": {
"class": "logging.StreamHandler",
"formatter": "generic",
"stream": "ext://sys.stderr"
},
},
formatters={
"generic": {
"format": "%(asctime)s [%(process)d] [%(levelname)s] %(message)s",
"datefmt": "[%Y-%m-%d %H:%M:%S %z]",
"class": "logging.Formatter"
}
}
) )
@ -299,7 +298,7 @@ class Logger(object):
'a': environ.get('HTTP_USER_AGENT', '-'), 'a': environ.get('HTTP_USER_AGENT', '-'),
'T': request_time.seconds, 'T': request_time.seconds,
'D': (request_time.seconds * 1000000) + request_time.microseconds, 'D': (request_time.seconds * 1000000) + request_time.microseconds,
'M': (request_time.seconds * 1000) + int(request_time.microseconds/1000), 'M': (request_time.seconds * 1000) + int(request_time.microseconds / 1000),
'L': "%d.%06d" % (request_time.seconds, request_time.microseconds), 'L': "%d.%06d" % (request_time.seconds, request_time.microseconds),
'p': "<%s>" % os.getpid() 'p': "<%s>" % os.getpid()
} }
@ -437,7 +436,7 @@ class Logger(object):
# finally setup the syslog handler # finally setup the syslog handler
h = logging.handlers.SysLogHandler(address=addr, h = logging.handlers.SysLogHandler(address=addr,
facility=facility, socktype=socktype) facility=facility, socktype=socktype)
h.setFormatter(fmt) h.setFormatter(fmt)
h._gunicorn = True h._gunicorn = True

View File

@ -41,7 +41,7 @@ class Message(object):
# set headers limits # set headers limits
self.limit_request_fields = cfg.limit_request_fields self.limit_request_fields = cfg.limit_request_fields
if (self.limit_request_fields <= 0 if (self.limit_request_fields <= 0
or self.limit_request_fields > MAX_HEADERS): or self.limit_request_fields > MAX_HEADERS):
self.limit_request_fields = MAX_HEADERS self.limit_request_fields = MAX_HEADERS
self.limit_request_field_size = cfg.limit_request_field_size self.limit_request_field_size = cfg.limit_request_field_size
if self.limit_request_field_size < 0: if self.limit_request_field_size < 0:
@ -71,7 +71,7 @@ class Message(object):
secure_scheme_headers = {} secure_scheme_headers = {}
if ('*' in cfg.forwarded_allow_ips or if ('*' in cfg.forwarded_allow_ips or
not isinstance(self.peer_addr, tuple) not isinstance(self.peer_addr, tuple)
or self.peer_addr[0] in cfg.forwarded_allow_ips): or self.peer_addr[0] in cfg.forwarded_allow_ips):
secure_scheme_headers = cfg.secure_scheme_headers secure_scheme_headers = cfg.secure_scheme_headers
# Parse headers into key/value pairs paying attention # Parse headers into key/value pairs paying attention
@ -173,7 +173,7 @@ class Request(Message):
# get max request line size # get max request line size
self.limit_request_line = cfg.limit_request_line self.limit_request_line = cfg.limit_request_line
if (self.limit_request_line < 0 if (self.limit_request_line < 0
or self.limit_request_line >= MAX_REQUEST_LINE): or self.limit_request_line >= MAX_REQUEST_LINE):
self.limit_request_line = MAX_REQUEST_LINE self.limit_request_line = MAX_REQUEST_LINE
self.req_number = req_number self.req_number = req_number
@ -276,7 +276,7 @@ class Request(Message):
# check in allow list # check in allow list
if ("*" not in self.cfg.proxy_allow_ips and if ("*" not in self.cfg.proxy_allow_ips and
isinstance(self.peer_addr, tuple) and isinstance(self.peer_addr, tuple) and
self.peer_addr[0] not in self.cfg.proxy_allow_ips): self.peer_addr[0] not in self.cfg.proxy_allow_ips):
raise ForbiddenProxyRequest(self.peer_addr[0]) raise ForbiddenProxyRequest(self.peer_addr[0])
def parse_proxy_protocol(self, line): def parse_proxy_protocol(self, line):

View File

@ -39,7 +39,7 @@ class BaseSocket(object):
def set_options(self, sock, bound=False): def set_options(self, sock, bound=False):
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
if (self.conf.reuse_port if (self.conf.reuse_port
and hasattr(socket, 'SO_REUSEPORT')): # pragma: no cover and hasattr(socket, 'SO_REUSEPORT')): # pragma: no cover
try: try:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
except socket.error as err: except socket.error as err:

View File

@ -58,7 +58,6 @@ def sd_notify(state, logger, unset_environment=False):
child processes. child processes.
""" """
addr = os.environ.get('NOTIFY_SOCKET') addr = os.environ.get('NOTIFY_SOCKET')
if addr is None: if addr is None:
# not run in a service, just a noop # not run in a service, just a noop
@ -69,7 +68,7 @@ def sd_notify(state, logger, unset_environment=False):
addr = '\0' + addr[1:] addr = '\0' + addr[1:]
sock.connect(addr) sock.connect(addr)
sock.sendall(state.encode('utf-8')) sock.sendall(state.encode('utf-8'))
except: except Exception:
logger.debug("Exception while invoking sd_notify()", exc_info=True) logger.debug("Exception while invoking sd_notify()", exc_info=True)
finally: finally:
if unset_environment: if unset_environment:

View File

@ -97,7 +97,7 @@ def load_class(uri, default="gunicorn.workers.sync.SyncWorker",
try: try:
mod = importlib.import_module('.'.join(components)) mod = importlib.import_module('.'.join(components))
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))

View File

@ -66,7 +66,6 @@ def _eventlet_socket_sendfile(self, file, offset=0, count=None):
file.seek(offset + total_sent) file.seek(offset + total_sent)
def _eventlet_serve(sock, handle, concurrency): def _eventlet_serve(sock, handle, concurrency):
""" """
Serve requests forever. Serve requests forever.

View File

@ -41,7 +41,7 @@ class GeventWorker(AsyncWorker):
sockets = [] sockets = []
for s in self.sockets: for s in self.sockets:
sockets.append(socket.socket(s.FAMILY, socket.SOCK_STREAM, sockets.append(socket.socket(s.FAMILY, socket.SOCK_STREAM,
fileno=s.sock.fileno())) fileno=s.sock.fileno()))
self.sockets = sockets self.sockets = sockets
def notify(self): def notify(self):

View File

@ -108,7 +108,7 @@ class TornadoWorker(Worker):
if tornado.version_info[0] < 6: if tornado.version_info[0] < 6:
if not isinstance(app, tornado.web.Application) or \ if not isinstance(app, tornado.web.Application) or \
isinstance(app, tornado.wsgi.WSGIApplication): isinstance(app, tornado.wsgi.WSGIApplication):
app = WSGIContainer(app) app = WSGIContainer(app)
elif not isinstance(app, WSGIContainer): elif not isinstance(app, WSGIContainer):
app = WSGIContainer(app) app = WSGIContainer(app)