Skip to content

Commit

Permalink
Fix flake8 issue WPS336 (explicit str concat)
Browse files Browse the repository at this point in the history
PR #254 by @alephnot0

This change effectively eliminates all instances of WPS336 linter error.

Co-authored-by: RJ Seibert <[email protected]>
Co-authored-by: Sviatoslav Sydorenko <[email protected]>
  • Loading branch information
webknjaz and RJ Seibert authored Dec 27, 2019
2 parents 85a190f + 360627c commit bd0f6d2
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 18 deletions.
19 changes: 12 additions & 7 deletions cheroot/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@
ASTERISK = b'*'
FORWARD_SLASH = b'/'
QUOTED_SLASH = b'%2F'
QUOTED_SLASH_REGEX = re.compile(b'(?i)' + QUOTED_SLASH)
QUOTED_SLASH_REGEX = re.compile(b''.join((b'(?i)', QUOTED_SLASH)))


comma_separated_headers = [
Expand Down Expand Up @@ -467,7 +467,10 @@ def _fetch(self):
chunk_size = line.pop(0)
chunk_size = int(chunk_size, 16)
except ValueError:
raise ValueError('Bad chunked transfer size: ' + repr(chunk_size))
raise ValueError(
'Bad chunked transfer size: {chunk_size!r}'.
format(chunk_size=chunk_size),
)

if chunk_size <= 0:
self.closed = True
Expand Down Expand Up @@ -822,7 +825,7 @@ def read_request_line(self):

# `urlsplit()` above parses "example.com:3128" as path part of URI.
# this is a workaround, which makes it detect netloc correctly
uri_split = urllib.parse.urlsplit(b'//' + uri)
uri_split = urllib.parse.urlsplit(b''.join((b'//', uri)))
_scheme, _authority, _path, _qs, _fragment = uri_split
_port = EMPTY
try:
Expand Down Expand Up @@ -1037,8 +1040,10 @@ def read_request_headers(self):
# Don't use simple_response here, because it emits headers
# we don't want. See
# https://github.com/cherrypy/cherrypy/issues/951
msg = self.server.protocol.encode('ascii')
msg += b' 100 Continue\r\n\r\n'
msg = b''.join((
self.server.protocol.encode('ascii'), SPACE, b'100 Continue',
CRLF, CRLF,
))
try:
self.conn.wfile.write(msg)
except socket.error as ex:
Expand Down Expand Up @@ -1499,7 +1504,7 @@ class HTTPServer:
timeout = 10
"""The timeout in seconds for accepted connections (default 10)."""

version = 'Cheroot/' + __version__
version = 'Cheroot/{version!s}'.format(version=__version__)
"""A version string for the HTTPServer."""

software = None
Expand Down Expand Up @@ -1805,7 +1810,7 @@ def error_log(self, msg='', level=20, traceback=False):
traceback (bool): add traceback to output or not
"""
# Override this in subclasses as desired
sys.stderr.write(msg + '\n')
sys.stderr.write('{msg!s}\n'.format(msg=msg))
sys.stderr.flush()
if traceback:
tblines = traceback_.format_exc()
Expand Down
6 changes: 3 additions & 3 deletions cheroot/test/test_conn.py
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,7 @@ def test_Chunked_Encoding(test_client):

# Try a chunked request that exceeds server.max_request_body_size.
# Note that the delimiters and trailer are included.
body = b'3e3\r\n' + (b'x' * 995) + b'\r\n0\r\n\r\n'
body = b'\r\n'.join((b'3e3', b'x' * 995, b'0', b'', b''))
conn.putrequest('POST', '/upload', skip_host=True)
conn.putheader('Host', conn.host)
conn.putheader('Transfer-Encoding', 'chunked')
Expand Down Expand Up @@ -971,8 +971,8 @@ def test_No_CRLF(test_client, invalid_terminator):
# Initialize a persistent HTTP connection
conn = test_client.get_connection()

# (b'%s' % b'') is not supported in Python 3.4, so just use +
conn.send(b'GET /hello HTTP/1.1' + invalid_terminator)
# (b'%s' % b'') is not supported in Python 3.4, so just use bytes.join()
conn.send(b''.join((b'GET /hello HTTP/1.1', invalid_terminator)))
response = conn.response_class(conn.sock, method='GET')
response.begin()
actual_resp_body = response.read()
Expand Down
8 changes: 4 additions & 4 deletions cheroot/test/test_ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def test_ssl_adapters(
)

resp = requests.get(
'https://' + interface + ':' + str(port) + '/',
'https://{host!s}:{port!s}/'.format(host=interface, port=port),
verify=tls_ca_certificate_pem_path,
)

Expand Down Expand Up @@ -274,7 +274,7 @@ def test_tls_client_auth(

make_https_request = functools.partial(
requests.get,
'https://' + interface + ':' + str(port) + '/',
'https://{host!s}:{port!s}/'.format(host=interface, port=port),

# Server TLS certificate verification:
verify=tls_ca_certificate_pem_path,
Expand Down Expand Up @@ -482,7 +482,7 @@ def test_http_over_https_error(
expect_fallback_response_over_plain_http = False
if expect_fallback_response_over_plain_http:
resp = requests.get(
'http://' + fqdn + ':' + str(port) + '/',
'http://{host!s}:{port!s}/'.format(host=fqdn, port=port),
)
assert resp.status_code == 400
assert resp.text == (
Expand All @@ -493,7 +493,7 @@ def test_http_over_https_error(

with pytest.raises(requests.exceptions.ConnectionError) as ssl_err:
requests.get( # FIXME: make stdlib ssl behave like PyOpenSSL
'http://' + fqdn + ':' + str(port) + '/',
'http://{host!s}:{port!s}/'.format(host=fqdn, port=port),
)

if IS_LINUX:
Expand Down
10 changes: 8 additions & 2 deletions cheroot/workers/threadpool.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,10 @@ def start(self):
for i in range(self.min):
self._threads.append(WorkerThread(self.server))
for worker in self._threads:
worker.setName('CP Server ' + worker.getName())
worker.setName(
'CP Server {worker_name!s}'.
format(worker_name=worker.getName()),
)
worker.start()
for worker in self._threads:
while not worker.ready:
Expand Down Expand Up @@ -223,7 +226,10 @@ def grow(self, amount):

def _spawn_worker(self):
worker = WorkerThread(self.server)
worker.setName('CP Server ' + worker.getName())
worker.setName(
'CP Server {worker_name!s}'.
format(worker_name=worker.getName()),
)
worker.start()
return worker

Expand Down
4 changes: 2 additions & 2 deletions cheroot/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ def get_environ(self):

# Request headers
env.update(
('HTTP_' + bton(k).upper().replace('-', '_'), bton(v))
('HTTP_{!s}'.format(bton(k).upper().replace('-', '_')), bton(v))
for k, v in req.inheaders.items()
)

Expand Down Expand Up @@ -409,7 +409,7 @@ def __call__(self, environ, start_response):
path = environ['PATH_INFO'] or '/'
for p, app in self.apps:
# The apps list should be sorted by length, descending.
if path.startswith(p + '/') or path == p:
if path.startswith('{path!s}/'.format(path=p)) or path == p:
environ = environ.copy()
environ['SCRIPT_NAME'] = environ.get('SCRIPT_NAME', '') + p
environ['PATH_INFO'] = path[len(p):]
Expand Down

0 comments on commit bd0f6d2

Please sign in to comment.