Skip to content

Commit ae173f0

Browse files
authored
Enable BytesWarning during test and fix discovered case (#1322)
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))
1 parent 1f857f0 commit ae173f0

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

redis/_compat.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,14 +166,18 @@ def itervalues(x):
166166
def nativestr(x):
167167
return x if isinstance(x, str) else x.decode('utf-8', 'replace')
168168

169+
def safe_unicode(value):
170+
if isinstance(value, bytes):
171+
value = value.decode('utf-8', 'replace')
172+
return str(value)
173+
169174
next = next
170175
unichr = chr
171176
imap = map
172177
izip = zip
173178
xrange = range
174179
basestring = str
175180
unicode = str
176-
safe_unicode = str
177181
long = int
178182
BlockingIOError = BlockingIOError
179183

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ deps =
99
pytest >= 2.7.0
1010
extras =
1111
hiredis: hiredis
12-
commands = {envpython} -m coverage run -m pytest -W always {posargs}
12+
commands = {envpython} -b -m coverage run -m pytest -W always {posargs}
1313

1414
[testenv:pycodestyle]
1515
basepython = python3.6

0 commit comments

Comments
 (0)