Skip to content

Commit

Permalink
💅 Address all instances of BytesWarning
Browse files Browse the repository at this point in the history
Things like byte-to-int comparisons and interpolation of bytes into
str were present in the code base. This patch fixes that by making
sure that the variables are always of compatible types in these cases.
  • Loading branch information
webknjaz committed Mar 12, 2024
1 parent 0fd16f0 commit 541018b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
6 changes: 5 additions & 1 deletion cheroot/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,11 @@ def __call__(self, rfile, hdict=None): # noqa: C901 # FIXME
if not line.endswith(CRLF):
raise ValueError('HTTP requires CRLF terminators')

if line[0] in (SPACE, TAB):
if line[:1] in (SPACE, TAB):
# NOTE: `type(line[0]) is int` and `type(line[:1]) is bytes`.
# NOTE: The former causes a the following warning:
# NOTE: `BytesWarning('Comparison between bytes and int')`
# NOTE: The latter is equivalent and does not.
# It's a continuation line.
v = line.strip()
else:
Expand Down
7 changes: 4 additions & 3 deletions cheroot/test/test_conn.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ def upload(req, resp):
"'POST' != request.method %r" %
req.environ['REQUEST_METHOD'],
)
return "thanks for '%s'" % req.environ['wsgi.input'].read()
input_contents = req.environ['wsgi.input'].read().decode('utf-8')
return f"thanks for '{input_contents !s}'"

def custom_204(req, resp):
"""Render response with status 204."""
Expand Down Expand Up @@ -917,7 +918,7 @@ def test_100_Continue(test_client):
status_line, _actual_headers, actual_resp_body = webtest.shb(response)
actual_status = int(status_line[:3])
assert actual_status == 200
expected_resp_body = ("thanks for '%s'" % body).encode()
expected_resp_body = f"thanks for '{body.decode() !s}'".encode()
assert actual_resp_body == expected_resp_body
conn.close()

Expand Down Expand Up @@ -987,7 +988,7 @@ def test_readall_or_close(test_client, max_request_body_size):
status_line, actual_headers, actual_resp_body = webtest.shb(response)
actual_status = int(status_line[:3])
assert actual_status == 200
expected_resp_body = ("thanks for '%s'" % body).encode()
expected_resp_body = f"thanks for '{body.decode() !s}'".encode()
assert actual_resp_body == expected_resp_body
conn.close()

Expand Down

0 comments on commit 541018b

Please sign in to comment.