Skip to content

Commit

Permalink
Enable BytesWarning during test and fix discovered case (#1322)
Browse files Browse the repository at this point in the history
The Python command line argument -b causes Python to emit a warning when
bytes and str usage is mixed. This is generally considered bad practice
as either one or the other is required. Enabling this feature during
tests helps catch them before reaching production.

The warning appeared as:

    tests/test_scripting.py::TestScripting::test_eval_msgpack_pipeline_error_in_lua
      .../redis-py/redis/client.py:3967: BytesWarning: str() on a bytes instance
        cmd = ' '.join(imap(safe_unicode, command))
  • Loading branch information
jdufresne authored Apr 13, 2020
1 parent 1f857f0 commit ae173f0
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
6 changes: 5 additions & 1 deletion redis/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,18 @@ def itervalues(x):
def nativestr(x):
return x if isinstance(x, str) else x.decode('utf-8', 'replace')

def safe_unicode(value):
if isinstance(value, bytes):
value = value.decode('utf-8', 'replace')
return str(value)

next = next
unichr = chr
imap = map
izip = zip
xrange = range
basestring = str
unicode = str
safe_unicode = str
long = int
BlockingIOError = BlockingIOError

Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ deps =
pytest >= 2.7.0
extras =
hiredis: hiredis
commands = {envpython} -m coverage run -m pytest -W always {posargs}
commands = {envpython} -b -m coverage run -m pytest -W always {posargs}

[testenv:pycodestyle]
basepython = python3.6
Expand Down

0 comments on commit ae173f0

Please sign in to comment.