简体   繁体   中英

UnicodeDecodeError in Python Redis when a connection error occurs

I'm writing some unit tests for testing a connection to Redis. At some point I expect the connection to fail and Python Redis to raise a RedisConnectionError .

What happens though is that the underlying socket connection fails and indeed raise an error (WSACONNECTIONREFUSED) but the text message uses my locale setting: the message is in french.

This seems to cause trouble to Python Redis as it apparently tries to report that error to the upper layers executing this piece of code:

def _error_message(self, exception):
    # args for socket.error can either be (errno, "message")
    # or just "message"
    if len(exception.args) == 1:
        return "Error connecting to %s:%s. %s." % \
            (self.host, self.port, exception.args[0])
    else:
        return "Error %s connecting to %s:%s. %s." % \
            (exception.args[0], self.host, self.port, exception.args[1])

Which causes a UnicodeDecodeError, like so:

File "F:\environment\lib\site-packages\redis-2.8.1.a-py2.7.egg\redis\connection.py", line 312, in send_command
  self.send_packed_command(self.pack_command(*args))
File "F:\environment\lib\site-packages\redis-2.8.1.a-py2.7.egg\redis\connection.py", line 294, in send_packed_command
  self.connect()
File "F:\environment\lib\site-packages\redis-2.8.1.a-py2.7.egg\redis\connection.py", line 236, in connect
  raise ConnectionError(self._error_message(e))
File "F:\environment\lib\site-packages\redis-2.8.1.a-py2.7.egg\redis\connection.py", line 261, in _error_message
  (exception.args[0], self.host, self.port, exception.args[1])
UnicodeDecodeError: 'ascii' codec can't decode byte 0x92 in position 18: ordinal not in range(128)

Indeed, one can see the actual error message is:

'Aucune connexion n\x92a pu \xeatre \xe9tablie car l\x92ordinateur cible l\x92a express\xe9ment refus\xe9e'

This seems weird to me, as I'm likely not the only person on earth using Python Redis which a non-english locale. Yet I couldn't find anybody else on the Internet facing that same issue.

I already tried to change the locale using setlocale() right before the call but the message remained in french.

What solutions do you see there ?

Check the types of your exception arguments. If one of them is Unicode and one is not that error will occur:

>>> '%s %s' % ('\x92',u'\x92')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0x92 in position 0: ordinal not in range(128)
>>> '%s %s' % (u'\x92','\x92')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0x92 in position 0: ordinal not in range(128)
>>> '%s %s' % ('\x92','\x92')   # Doesn't occur if both are byte strings
'\x92 \x92'
>>> '%s %s' % (u'\x92',u'\x92') # or both Unicode strings.
u'\x92 \x92'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM