Skip to content

Commit

Permalink
Merge pull request #2928 from samypr100/pycodestyle-fixup
Browse files Browse the repository at this point in the history
Fixing errors reported by pycodestyle
  • Loading branch information
benoitc committed Jan 26, 2023
2 parents bba59ae + 2ea4699 commit 8fa57d0
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 84 deletions.
10 changes: 5 additions & 5 deletions gunicorn/arbiter.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def start(self):

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.info("Listening at: %s (%s)", listeners_str, self.pid)
self.log.info("Using worker: %s", self.cfg.worker_class_str)
Expand Down Expand Up @@ -421,7 +421,7 @@ def reexec(self):
environ['LISTEN_FDS'] = str(len(self.LISTENERS))
else:
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'])

Expand Down Expand Up @@ -454,11 +454,11 @@ def reload(self):
# do we need to change listener ?
if old_address != self.cfg.address:
# close all listeners
for l in self.LISTENERS:
l.close()
for lnr in self.LISTENERS:
lnr.close()
# init new listeners
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)

# do some actions on reload
Expand Down
63 changes: 34 additions & 29 deletions gunicorn/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ def _validate_callable(val):
raise TypeError(str(e))
except AttributeError:
raise TypeError("Can not load '%s' from '%s'"
"" % (obj_name, mod_name))
"" % (obj_name, mod_name))
if not callable(val):
raise TypeError("Value is not callable: %s" % val)
if arity != -1 and arity != util.get_arity(val):
Expand Down Expand Up @@ -563,6 +563,7 @@ class ConfigFile(Setting):
prefix.
"""


class WSGIApp(Setting):
name = "wsgi_app"
section = "Config File"
Expand All @@ -575,6 +576,7 @@ class WSGIApp(Setting):
.. versionadded:: 20.1.0
"""


class Bind(Setting):
name = "bind"
action = "append"
Expand Down Expand Up @@ -1273,69 +1275,70 @@ class ForwardedAllowIPS(Setting):
By default, the value of the ``FORWARDED_ALLOW_IPS`` environment
variable. If it is not defined, the default is ``"127.0.0.1"``.
.. note::
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
have a request from the remote address 134.213.44.18, and the default value of ``secure_scheme_headers``:
``secure_scheme_headers`` is complex. Various scenarios are documented below to further elaborate.
In each case, we have a request from the remote address 134.213.44.18, and the default value of
``secure_scheme_headers``:
.. code::
secure_scheme_headers = {
'X-FORWARDED-PROTOCOL': 'ssl',
'X-FORWARDED-PROTO': 'https',
'X-FORWARDED-SSL': 'on'
}
.. list-table::
.. list-table::
:header-rows: 1
:align: center
:widths: auto
* - ``forwarded-allow-ips``
- Secure Request Headers
- Result
- Explanation
* - .. code::
* - .. code::
["127.0.0.1"]
- .. code::
X-Forwarded-Proto: https
- .. code::
- .. code::
wsgi.url_scheme = "http"
- IP address was not allowed
* - .. code::
* - .. code::
"*"
- <none>
- .. code::
- .. code::
wsgi.url_scheme = "http"
- IP address allowed, but no secure headers provided
* - .. code::
* - .. code::
"*"
- .. code::
X-Forwarded-Proto: https
- .. code::
- .. code::
wsgi.url_scheme = "https"
- IP address allowed, one request header matched
* - .. code::
* - .. code::
["134.213.44.18"]
- .. code::
X-Forwarded-Ssl: on
X-Forwarded-Proto: http
- ``InvalidSchemeHeaders()`` raised
- IP address allowed, but the two secure headers disagreed on if HTTPS was used
"""

Expand Down Expand Up @@ -1617,6 +1620,7 @@ class StatsdHost(Setting):
.. versionadded:: 19.1
"""


# Datadog Statsd (dogstatsd) tags. https://docs.datadoghq.com/developers/dogstatsd/
class DogstatsdTags(Setting):
name = "dogstatsd_tags"
Expand All @@ -1632,6 +1636,7 @@ class DogstatsdTags(Setting):
.. versionadded:: 20
"""


class StatsdPrefix(Setting):
name = "statsd_prefix"
section = "Logging"
Expand Down
2 changes: 1 addition & 1 deletion gunicorn/debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def __call__(self, frame, event, arg):
if '__file__' in frame.f_globals:
filename = frame.f_globals['__file__']
if (filename.endswith('.pyc') or
filename.endswith('.pyo')):
filename.endswith('.pyo')):
filename = filename[:-1]
name = frame.f_globals['__name__']
line = linecache.getline(filename, lineno)
Expand Down
75 changes: 37 additions & 38 deletions gunicorn/glogging.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,46 +44,45 @@
"local7": 23
}


CONFIG_DEFAULTS = dict(
version=1,
disable_existing_loggers=False,

root={"level": "INFO", "handlers": ["console"]},
loggers={
"gunicorn.error": {
"level": "INFO",
"handlers": ["error_console"],
"propagate": True,
"qualname": "gunicorn.error"
},

"gunicorn.access": {
"level": "INFO",
"handlers": ["console"],
"propagate": True,
"qualname": "gunicorn.access"
}
version=1,
disable_existing_loggers=False,

root={"level": "INFO", "handlers": ["console"]},
loggers={
"gunicorn.error": {
"level": "INFO",
"handlers": ["error_console"],
"propagate": True,
"qualname": "gunicorn.error"
},
handlers={
"console": {
"class": "logging.StreamHandler",
"formatter": "generic",
"stream": "ext://sys.stdout"
},
"error_console": {
"class": "logging.StreamHandler",
"formatter": "generic",
"stream": "ext://sys.stderr"
},

"gunicorn.access": {
"level": "INFO",
"handlers": ["console"],
"propagate": True,
"qualname": "gunicorn.access"
}
},
handlers={
"console": {
"class": "logging.StreamHandler",
"formatter": "generic",
"stream": "ext://sys.stdout"
},
formatters={
"generic": {
"format": "%(asctime)s [%(process)d] [%(levelname)s] %(message)s",
"datefmt": "[%Y-%m-%d %H:%M:%S %z]",
"class": "logging.Formatter"
}
"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"
}
}
)


Expand Down Expand Up @@ -299,7 +298,7 @@ def atoms(self, resp, req, environ, request_time):
'a': environ.get('HTTP_USER_AGENT', '-'),
'T': request_time.seconds,
'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),
'p': "<%s>" % os.getpid()
}
Expand Down Expand Up @@ -437,7 +436,7 @@ def _set_syslog_handler(self, log, cfg, fmt, name):

# finally setup the syslog handler
h = logging.handlers.SysLogHandler(address=addr,
facility=facility, socktype=socktype)
facility=facility, socktype=socktype)

h.setFormatter(fmt)
h._gunicorn = True
Expand Down
8 changes: 4 additions & 4 deletions gunicorn/http/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def __init__(self, cfg, unreader, peer_addr):
# set headers limits
self.limit_request_fields = cfg.limit_request_fields
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_field_size = cfg.limit_request_field_size
if self.limit_request_field_size < 0:
Expand Down Expand Up @@ -71,7 +71,7 @@ def parse_headers(self, data):
secure_scheme_headers = {}
if ('*' in cfg.forwarded_allow_ips or
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

# Parse headers into key/value pairs paying attention
Expand Down Expand Up @@ -173,7 +173,7 @@ def __init__(self, cfg, unreader, peer_addr, req_number=1):
# get max request line size
self.limit_request_line = cfg.limit_request_line
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.req_number = req_number
Expand Down Expand Up @@ -276,7 +276,7 @@ def proxy_protocol_access_check(self):
# check in allow list
if ("*" not in self.cfg.proxy_allow_ips 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])

def parse_proxy_protocol(self, line):
Expand Down
2 changes: 1 addition & 1 deletion gunicorn/sock.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def __getattr__(self, name):
def set_options(self, sock, bound=False):
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
if (self.conf.reuse_port
and hasattr(socket, 'SO_REUSEPORT')): # pragma: no cover
and hasattr(socket, 'SO_REUSEPORT')): # pragma: no cover
try:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
except socket.error as err:
Expand Down
3 changes: 1 addition & 2 deletions gunicorn/systemd.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ def sd_notify(state, logger, unset_environment=False):
child processes.
"""


addr = os.environ.get('NOTIFY_SOCKET')
if addr is None:
# not run in a service, just a noop
Expand All @@ -69,7 +68,7 @@ def sd_notify(state, logger, unset_environment=False):
addr = '\0' + addr[1:]
sock.connect(addr)
sock.sendall(state.encode('utf-8'))
except:
except Exception:
logger.debug("Exception while invoking sd_notify()", exc_info=True)
finally:
if unset_environment:
Expand Down
2 changes: 1 addition & 1 deletion gunicorn/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def load_class(uri, default="gunicorn.workers.sync.SyncWorker",

try:
mod = importlib.import_module('.'.join(components))
except:
except Exception:
exc = traceback.format_exc()
msg = "class uri %r invalid or not found: \n\n[%s]"
raise RuntimeError(msg % (uri, exc))
Expand Down
1 change: 0 additions & 1 deletion gunicorn/workers/geventlet.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ def _eventlet_socket_sendfile(self, file, offset=0, count=None):
file.seek(offset + total_sent)



def _eventlet_serve(sock, handle, concurrency):
"""
Serve requests forever.
Expand Down
2 changes: 1 addition & 1 deletion gunicorn/workers/ggevent.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def patch(self):
sockets = []
for s in self.sockets:
sockets.append(socket.socket(s.FAMILY, socket.SOCK_STREAM,
fileno=s.sock.fileno()))
fileno=s.sock.fileno()))
self.sockets = sockets

def notify(self):
Expand Down
2 changes: 1 addition & 1 deletion gunicorn/workers/gtornado.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def run(self):

if tornado.version_info[0] < 6:
if not isinstance(app, tornado.web.Application) or \
isinstance(app, tornado.wsgi.WSGIApplication):
isinstance(app, tornado.wsgi.WSGIApplication):
app = WSGIContainer(app)
elif not isinstance(app, WSGIContainer):
app = WSGIContainer(app)
Expand Down

0 comments on commit 8fa57d0

Please sign in to comment.