jonathanslenders / asyncio-redis

Redis client for Python asyncio (PEP 3156)
http://asyncio-redis.readthedocs.org/
Other
552 stars 74 forks source link

Raise UnicodeEncodeError when register a script that contain non-ascii chars. #94

Open huyx opened 8 years ago

huyx commented 8 years ago

There some non-ascii chars in my script, when call register_script, it raises UnicodeEncodeError:

  ...
  File "C:\Python35\lib\site-packages\asyncio_redis\pool.py", line 128, in register_script
    script = yield from self.__getattr__('register_script')(script)
  File "C:\Python35\lib\site-packages\asyncio_redis\protocol.py", line 656, in wrapper
    result = yield from method(protocol_self, *a, **kw)
  File "C:\Python35\lib\site-packages\asyncio_redis\protocol.py", line 1998, in register_script
    sha = yield from self.script_load(script)
  File "C:\Python35\lib\site-packages\asyncio_redis\protocol.py", line 656, in wrapper
    result = yield from method(protocol_self, *a, **kw)
  File "C:\Python35\lib\site-packages\asyncio_redis\protocol.py", line 2056, in script_load
    return self._query(b'script', b'load', script.encode('ascii'))
UnicodeEncodeError: 'ascii' codec can't encode characters in position 6-9: ordinal not in range(128)

I found the code cause this:

    @_query_command
    def script_load(self, script:str) -> str:
        """ Load script, returns sha1 """
        return self._query(b'script', b'load', script.encode('ascii'))

If I changed 'ascii' to 'utf-8', it works fine.

I think it is a bug. thanks.

jonathanslenders commented 8 years ago

This sounds indeed like a bug.

I cannot find any reference in the Redis documentation regarding the encoding of Lua scripts. (The Redis protocol itself is pure binary and in general encoding-agnostic.) But utf-8 is a superset of ascii, and if it works like this, I think probably fix it that way.

jonathanslenders commented 8 years ago

Hi @huyx, this has been fixed in the following commit: https://github.com/jonathanslenders/asyncio-redis/commit/004810da8300d21c07e6e853959802ce7aab1ef0

Thanks for reporting!