-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix str/bytes mixup in PythonParser.read_response() (#1324)
Calling str() on a bytes object can result in a BytesWarning being emitted and usually indicates a mixup between byte and string handling. Now, in the event of an invalid RESP response, use the repr value of the raw response in the exception message. Can further simplify the bytes/str handling by comparing the first byte as a bytes object instead of converting it to str. The bytes literal is available on all supported Pythons. This removes the need for the compatibility function, byte_to_chr().
- Loading branch information
Showing
3 changed files
with
26 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import mock | ||
import pytest | ||
|
||
from redis.exceptions import InvalidResponse | ||
from redis.utils import HIREDIS_AVAILABLE | ||
|
||
|
||
@pytest.mark.skipif(HIREDIS_AVAILABLE, reason='PythonParser only') | ||
def test_invalid_response(r): | ||
raw = b'x' | ||
parser = r.connection._parser | ||
with mock.patch.object(parser._buffer, 'readline', return_value=raw): | ||
with pytest.raises(InvalidResponse) as cm: | ||
parser.read_response() | ||
assert str(cm.value) == 'Protocol Error: %r' % raw |