Skip to content

Commit

Permalink
Adding support for non-decodable commands
Browse files Browse the repository at this point in the history
Some commands (i.e DUMP) should never have their response decoded, as they return binaries, not encoded blobs

fixes #1254
  • Loading branch information
chayim committed Nov 21, 2021
1 parent d2b2333 commit 1448a65
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
8 changes: 7 additions & 1 deletion redis/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
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"
Expand Down Expand Up @@ -1081,7 +1084,10 @@ def execute_command(self, *args, **options):
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]
Expand Down
5 changes: 4 additions & 1 deletion redis/commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,10 @@ def dump(self, name):
Return a serialized version of the value stored at the specified key.
If key does not exist a nil bulk reply is returned.
"""
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):
"Returns the number of ``names`` that exist"
Expand Down
15 changes: 9 additions & 6 deletions redis/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ def on_disconnect(self):
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)
Expand Down Expand Up @@ -354,8 +354,9 @@ def read_response(self):
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

Expand Down Expand Up @@ -449,7 +450,7 @@ def read_from_socket(self, timeout=SENTINEL, raise_on_timeout=True):
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)

Expand Down Expand Up @@ -742,10 +743,12 @@ def can_read(self, timeout=0):
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" %
Expand Down
4 changes: 2 additions & 2 deletions redis/sentinel.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ def connect(self):
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
Expand Down

0 comments on commit 1448a65

Please sign in to comment.