Differences in parsing with decode_responses=True, with and without hiredis
See original GitHub issueI ran into a similar problem as #809: I have a redis connection using decode_repsonses=True, but I am also using a 3rd-party package (RQ) that pickles some data and deep in that library I get UnicodeDecodeErrors. I’m kind of stuck because of that library’s use of pickled data.
However, I thought to install hiredis-py to see if it handled parsing differently and it does.
So, with this:
import redis
import pickle
rdb = redis.StrictRedis(decode_responses=True)
data = pickle.dumps(dict(hello='world'))
rdb.set('foo', data)
rdb.get('foo')
With hiredis installed I see:
b'\x80\x03}q\x00X\x05\x00\x00\x00helloq\x01X\x05\x00\x00\x00worldq\x02s.'
Without hiredis installed, I get:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte
Looking at hiredis-py code, I see the equivalent of this:
if encoding:
try:
data = data.decode(encoding)
except ValueError:
pass
I’m wishing redis-py did the same. I’d love to see the following change to connection.py:
def decode(self, value, force=False):
"Return a unicode string from the byte representation"
if (self.decode_responses or force) and isinstance(value, bytes):
try:
value = value.decode(self.encoding, self.encoding_errors)
except ValueError:
# Ignore encoding and return bytes
pass
return value
Considering using hiredis comes highly recommended, I’d love to see this base difference in parsing resolved.
Thanks!
Issue Analytics
- State:
- Created 5 years ago
- Reactions:5
- Comments:18 (9 by maintainers)
Top Results From Across the Web
Differences in parsing with decode_responses=True ... - GitHub
I ran into a similar problem as #809: I have a redis connection using decode_repsonses=True, but I am also using a 3rd-party package...
Read more >Hiredis | Redis
The following code creates a connection to Redis using hiredis' synchronous API: #include "hiredis.h" redisContext *c = redisConnect("hostname", port); if ...
Read more >NodeJS using redis - installing with hiredis vs without?
For production I would seriously consider using hiredis parser because it performs better. For testing you do not need it.
Read more >Use hiredis to increase performance of reading large sets
hiredis -rb uses the hiredis C client library to optimize read/writes to Redis. ... members = JSON.parse(redis.get(string_key)) } x.compare! end ...
Read more >hiredis - PyPI
Python extension that wraps protocol parsing code in hiredis. ... When the buffer does not contain a full reply, gets returns False ....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@andymccurdy Stumbled upon this thread when investigating my own bug with decoding responses with binary data. Is the addition of a
r.responses_as_bytescontext manager still on the roadmap or have you switched approaches?The proposed change (redis/hiredis-py#82) to hiredis-py has been accepted and merged. Future versions of hiredis-py will behave sanely and no longer silently discard decoding errors by default.