Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for non-decodable commands #1731

Merged
merged 1 commit into from
Nov 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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