Skip to content

UnicodeDecodeError occurs when there is pickle binary data with decode_response=True #809

Closed
@xbynet

Description

@xbynet

the thing is that:

>>> from redis import StrictRedis
>>> r=StrictRedis(decode_responses=True)
>>> r.set('data1',{'aa':11})
>>> r.get('data1')
throw UnicodeDecodeError: 'utf-8' codec can't decode byte...

I know it can r.set('data1',json.dumps({'aa':11})) or use r.hmset('data1',{'aa':11}) to slove, but that is example to describe it. there is more complex scene like session pickle to redis or other meanwhile also want to use decode_responses=True.
but if i don't use decode_responses=True. i have to do manual decode() everytime.
So,what can i do if there is pickle data when use decode_responses=True.

My solve is change the connection.py about line 279:

       if isinstance(response, bytes) and self.encoding:
            import pickle
            from pickle import UnpicklingError
            try:
                response = pickle.loads(response)
            except UnpicklingError as e:
                response = response.decode(self.encoding)
        return response

so,can this merge into master or there is better resolution?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions