diff options
author | Chayim <chayim@users.noreply.github.com> | 2021-11-25 14:18:33 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-25 14:18:33 +0200 |
commit | d7b56103ed4d0ba9c05c74ca5580c72fcb70c09c (patch) | |
tree | 943b5df36eea7d2e37d50be626158461ecf41485 | |
parent | 8991370332e818cf9886cec6adc1858189b04bd6 (diff) | |
download | redis-py-d7b56103ed4d0ba9c05c74ca5580c72fcb70c09c.tar.gz |
Adding support for non-decodable commands (#1731)
-rwxr-xr-x | redis/client.py | 8 | ||||
-rw-r--r-- | redis/commands/core.py | 5 | ||||
-rwxr-xr-x | redis/connection.py | 15 | ||||
-rw-r--r-- | redis/sentinel.py | 4 |
4 files changed, 22 insertions, 10 deletions
diff --git a/redis/client.py b/redis/client.py index 143ed88..30a5c3c 100755 --- a/redis/client.py +++ b/redis/client.py @@ -27,6 +27,9 @@ from redis.utils import safe_str, str_if_bytes SYM_EMPTY = b'' EMPTY_RESPONSE = 'EMPTY_RESPONSE' +# some responses (ie. dump) are binary, and just meant to never be decoded +NEVER_DECODE = 'NEVER_DECODE' + def timestamp_to_datetime(response): "Converts a unix timestamp to a Python datetime object" @@ -1106,7 +1109,10 @@ class Redis(RedisModuleCommands, CoreCommands, SentinelCommands, object): def parse_response(self, connection, command_name, **options): "Parses a response from the Redis server" try: - response = connection.read_response() + if NEVER_DECODE in options: + response = connection.read_response(disable_decoding=True) + else: + response = connection.read_response() except ResponseError: if EMPTY_RESPONSE in options: return options[EMPTY_RESPONSE] diff --git a/redis/commands/core.py b/redis/commands/core.py index 948655b..64e3b6d 100644 --- a/redis/commands/core.py +++ b/redis/commands/core.py @@ -1008,7 +1008,10 @@ class BasicKeyCommands: For more information check https://redis.io/commands/dump """ - return self.execute_command('DUMP', name) + from redis.client import NEVER_DECODE + options = {} + options[NEVER_DECODE] = [] + return self.execute_command('DUMP', name, **options) def exists(self, *names): """ diff --git a/redis/connection.py b/redis/connection.py index eac9db3..2f91fae 100755 --- a/redis/connection.py +++ b/redis/connection.py @@ -315,7 +315,7 @@ class PythonParser(BaseParser): def can_read(self, timeout): return self._buffer and self._buffer.can_read(timeout) - def read_response(self): + def read_response(self, disable_decoding=False): raw = self._buffer.readline() if not raw: raise ConnectionError(SERVER_CLOSED_CONNECTION_ERROR) @@ -355,8 +355,9 @@ class PythonParser(BaseParser): length = int(response) if length == -1: return None - response = [self.read_response() for i in range(length)] - if isinstance(response, bytes): + response = [self.read_response(disable_decoding=disable_decoding) + for i in range(length)] + if isinstance(response, bytes) and disable_decoding is False: response = self.encoder.decode(response) return response @@ -450,7 +451,7 @@ class HiredisParser(BaseParser): if custom_timeout: sock.settimeout(self._socket_timeout) - def read_response(self): + def read_response(self, disable_decoding=False): if not self._reader: raise ConnectionError(SERVER_CLOSED_CONNECTION_ERROR) @@ -758,10 +759,12 @@ class Connection: self.connect() return self._parser.can_read(timeout) - def read_response(self): + def read_response(self, disable_decoding=False): """Read the response from a previously sent command""" try: - response = self._parser.read_response() + response = self._parser.read_response( + disable_decoding=disable_decoding + ) except socket.timeout: self.disconnect() raise TimeoutError("Timeout reading from %s:%s" % diff --git a/redis/sentinel.py b/redis/sentinel.py index 17dd75b..3efd58f 100644 --- a/redis/sentinel.py +++ b/redis/sentinel.py @@ -51,9 +51,9 @@ class SentinelManagedConnection(Connection): continue raise SlaveNotFoundError # Never be here - def read_response(self): + def read_response(self, disable_decoding=False): try: - return super().read_response() + return super().read_response(disable_decoding=disable_decoding) except ReadOnlyError: if self.connection_pool.is_master: # When talking to a master, a ReadOnlyError when likely |