tarantool / tarantool-python

Python client library for Tarantool
https://www.tarantool.io
BSD 2-Clause "Simplified" License
100 stars 48 forks source link

fix UnicodeDecodeError when printing tuples with binary data #93

Closed zimnukhov closed 7 years ago

zimnukhov commented 7 years ago

The following script fails when trying to print tuple with raw byte field that is not valid utf8:

import tarantool
conn = tarantool.connect('localhost', 31539, encoding=None)
conn.replace('test_space', ('foo', b'\xff'))
print(conn.select('test_space', 'foo'))

traceback:

  File "/Users/zimnukhov/tmp/tarantool-python/tarantool/response.py", line 232, in __str__
    output.extend(("- ", json.dumps(tpl), "\n"))
  File "/usr/lib/python2.7/json/__init__.py", line 243, in dumps
    return _default_encoder.encode(obj)
  File "/usr/lib/python2.7/json/encoder.py", line 207, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python2.7/json/encoder.py", line 270, in iterencode
    return _iterencode(o, 0)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xff in position 0: invalid start byte

I suggest using repr instead of json to serialize tuples. The above script will print:

- ['foo', '\xff']