diff options
author | Brian Candler <b.candler@pobox.com> | 2019-05-29 23:41:56 +0100 |
---|---|---|
committer | Andy McCurdy <andy@andymccurdy.com> | 2019-05-29 15:41:56 -0700 |
commit | 0eac02d209932742cfb0dfc667e5e9707bfedd46 (patch) | |
tree | a59f745d646441915eff8dfa758c0358efcadf34 | |
parent | b51bfd818ce36cc3ae8591b54c988fbb16eb336d (diff) | |
download | redis-py-0eac02d209932742cfb0dfc667e5e9707bfedd46.tar.gz |
Pass encoding_errors setting to hiredis (>=1.0.0) (#1162)
Pass encoding_errors setting to hiredis (>=1.0.0).
Fixes #1161
-rwxr-xr-x | redis/connection.py | 4 | ||||
-rw-r--r-- | tests/test_encoding.py | 14 |
2 files changed, 18 insertions, 0 deletions
diff --git a/redis/connection.py b/redis/connection.py index a42b33f..a6572cb 100755 --- a/redis/connection.py +++ b/redis/connection.py @@ -41,6 +41,8 @@ if HIREDIS_AVAILABLE: hiredis_version >= StrictVersion('0.1.3') HIREDIS_SUPPORTS_BYTE_BUFFER = \ hiredis_version >= StrictVersion('0.1.4') + HIREDIS_SUPPORTS_ENCODING_ERRORS = \ + hiredis_version >= StrictVersion('1.0.0') if not HIREDIS_SUPPORTS_BYTE_BUFFER: msg = ("redis-py works best with hiredis >= 0.1.4. You're running " @@ -318,6 +320,8 @@ class HiredisParser(BaseParser): if connection.encoder.decode_responses: kwargs['encoding'] = connection.encoder.encoding + if HIREDIS_SUPPORTS_ENCODING_ERRORS: + kwargs['errors'] = connection.encoder.encoding_errors self._reader = hiredis.Reader(**kwargs) self._next_response = False diff --git a/tests/test_encoding.py b/tests/test_encoding.py index 18219a6..3f43006 100644 --- a/tests/test_encoding.py +++ b/tests/test_encoding.py @@ -25,6 +25,20 @@ class TestEncoding(object): assert r.lrange('a', 0, -1) == result +class TestEncodingErrors(object): + def test_ignore(self, request): + r = _get_client(redis.Redis, request=request, decode_responses=True, + encoding_errors='ignore') + r.set('a', b'foo\xff') + assert r.get('a') == 'foo' + + def test_replace(self, request): + r = _get_client(redis.Redis, request=request, decode_responses=True, + encoding_errors='replace') + r.set('a', b'foo\xff') + assert r.get('a') == 'foo\ufffd' + + class TestCommandsAreNotEncoded(object): @pytest.fixture() def r(self, request): |