diff options
author | Frankie Dintino <fdintino@theatlantic.com> | 2012-02-08 15:33:39 -0500 |
---|---|---|
committer | Frankie Dintino <fdintino@theatlantic.com> | 2012-02-08 15:33:39 -0500 |
commit | a99b650071ff83368d1b3b1d75d8870a682a2390 (patch) | |
tree | df65f1e1744d4c1b440fe066d7a1cf4340553cf7 /redis/connection.py | |
parent | c56b9e16473aa8e9cd797e483745cb2f99877233 (diff) | |
download | redis-py-a99b650071ff83368d1b3b1d75d8870a682a2390.tar.gz |
Have PythonParser handle result encoding similarly to hiredis.Reader.
If the connection encoding is set to utf-8, pass the socket read results
through decode()
Diffstat (limited to 'redis/connection.py')
-rw-r--r-- | redis/connection.py | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/redis/connection.py b/redis/connection.py index f5b7de6..a8dc494 100644 --- a/redis/connection.py +++ b/redis/connection.py @@ -22,6 +22,7 @@ except ImportError: class PythonParser(object): "Plain Python parsing class" MAX_READ_LENGTH = 1000000 + encoding = None def __init__(self): self._fp = None @@ -35,6 +36,7 @@ class PythonParser(object): def on_connect(self, connection): "Called when the socket connects" self._fp = connection._sock.makefile('r') + self.encoding = connection.encoding def on_disconnect(self): "Called when the socket disconnects" @@ -80,6 +82,9 @@ class PythonParser(object): byte, response = response[0], response[1:] + if byte not in ('-', '+', ':', '$', '*'): + raise InvalidResponse("Protocol Error") + # server returned an error if byte == '-': if response.startswith('ERR '): @@ -91,24 +96,25 @@ class PythonParser(object): raise ConnectionError("Redis is loading data into memory") # single value elif byte == '+': - return response + pass # int value elif byte == ':': - return long(response) + response = long(response) # bulk response elif byte == '$': length = int(response) if length == -1: return None response = self.read(length) - return response # multi-bulk response elif byte == '*': length = int(response) if length == -1: return None - return [self.read_response() for i in xrange(length)] - raise InvalidResponse("Protocol Error") + response = [self.read_response() for i in xrange(length)] + if isinstance(response, str) and self.encoding: + response = response.decode(self.encoding) + return response class HiredisParser(object): "Parser class for connections using Hiredis" |