Skip to content

Commit fb64743

Browse files
fadidachayimdvora-h
authored
Fix special response parsing options handling (#2302)
* Fix special response parsing options handling When using special response parsing options like `NEVER_DECODE` and `EMPTY_RESPONSE`, don't pass them to the response callbacks because some of them are not prepared for receiving named arguments. Instead, redis-py should use them before calling the callbacks and then discard them. * Use kwargs instead of options * change options to kwargs in asyncio/cluster.py/L878 Co-authored-by: Chayim <chayim@users.noreply.github.com> Co-authored-by: dvora-h <67596500+dvora-h@users.noreply.github.com>
1 parent 772079f commit fb64743

File tree

5 files changed

+36
-2
lines changed

5 files changed

+36
-2
lines changed

redis/asyncio/client.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,12 +501,17 @@ async def parse_response(
501501
try:
502502
if NEVER_DECODE in options:
503503
response = await connection.read_response(disable_decoding=True)
504+
options.pop(NEVER_DECODE)
504505
else:
505506
response = await connection.read_response()
506507
except ResponseError:
507508
if EMPTY_RESPONSE in options:
508509
return options[EMPTY_RESPONSE]
509510
raise
511+
512+
if EMPTY_RESPONSE in options:
513+
options.pop(EMPTY_RESPONSE)
514+
510515
if command_name in self.response_callbacks:
511516
# Mypy bug: https://github.com/python/mypy/issues/10977
512517
command_name = cast(str, command_name)

redis/asyncio/cluster.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -934,13 +934,17 @@ async def parse_response(
934934
try:
935935
if NEVER_DECODE in kwargs:
936936
response = await connection.read_response(disable_decoding=True)
937+
kwargs.pop(NEVER_DECODE)
937938
else:
938939
response = await connection.read_response()
939940
except ResponseError:
940941
if EMPTY_RESPONSE in kwargs:
941942
return kwargs[EMPTY_RESPONSE]
942943
raise
943944

945+
if EMPTY_RESPONSE in kwargs:
946+
kwargs.pop(EMPTY_RESPONSE)
947+
944948
# Return response
945949
if command in self.response_callbacks:
946950
return self.response_callbacks[command](response, **kwargs)

redis/client.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,12 +1258,17 @@ def parse_response(self, connection, command_name, **options):
12581258
try:
12591259
if NEVER_DECODE in options:
12601260
response = connection.read_response(disable_decoding=True)
1261+
options.pop(NEVER_DECODE)
12611262
else:
12621263
response = connection.read_response()
12631264
except ResponseError:
12641265
if EMPTY_RESPONSE in options:
12651266
return options[EMPTY_RESPONSE]
12661267
raise
1268+
1269+
if EMPTY_RESPONSE in options:
1270+
options.pop(EMPTY_RESPONSE)
1271+
12671272
if command_name in self.response_callbacks:
12681273
return self.response_callbacks[command_name](response, **options)
12691274
return response

tests/test_asyncio/test_commands.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
import redis
1313
from redis import exceptions
14-
from redis.client import parse_info
14+
from redis.client import EMPTY_RESPONSE, NEVER_DECODE, parse_info
1515
from tests.conftest import (
1616
skip_if_server_version_gte,
1717
skip_if_server_version_lt,
@@ -542,6 +542,16 @@ async def test_time(self, r: redis.Redis):
542542
assert isinstance(t[0], int)
543543
assert isinstance(t[1], int)
544544

545+
async def test_never_decode_option(self, r: redis.Redis):
546+
opts = {NEVER_DECODE: []}
547+
await r.delete("a")
548+
assert await r.execute_command("EXISTS", "a", **opts) == 0
549+
550+
async def test_empty_response_option(self, r: redis.Redis):
551+
opts = {EMPTY_RESPONSE: []}
552+
await r.delete("a")
553+
assert await r.execute_command("EXISTS", "a", **opts) == 0
554+
545555
# BASIC KEY COMMANDS
546556
async def test_append(self, r: redis.Redis):
547557
assert await r.append("a", "a1") == 2

tests/test_commands.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import redis
1111
from redis import exceptions
12-
from redis.client import parse_info
12+
from redis.client import EMPTY_RESPONSE, NEVER_DECODE, parse_info
1313

1414
from .conftest import (
1515
_get_client,
@@ -917,6 +917,16 @@ def test_bgsave(self, r):
917917
time.sleep(0.3)
918918
assert r.bgsave(True)
919919

920+
def test_never_decode_option(self, r: redis.Redis):
921+
opts = {NEVER_DECODE: []}
922+
r.delete("a")
923+
assert r.execute_command("EXISTS", "a", **opts) == 0
924+
925+
def test_empty_response_option(self, r: redis.Redis):
926+
opts = {EMPTY_RESPONSE: []}
927+
r.delete("a")
928+
assert r.execute_command("EXISTS", "a", **opts) == 0
929+
920930
# BASIC KEY COMMANDS
921931
def test_append(self, r):
922932
assert r.append("a", "a1") == 2

0 commit comments

Comments
 (0)