Skip to content

Commit 1670e50

Browse files
author
Shay Fadida
committed
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.
1 parent a8c8227 commit 1670e50

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-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/client.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,12 +1250,17 @@ def parse_response(self, connection, command_name, **options):
12501250
try:
12511251
if NEVER_DECODE in options:
12521252
response = connection.read_response(disable_decoding=True)
1253+
options.pop(NEVER_DECODE)
12531254
else:
12541255
response = connection.read_response()
12551256
except ResponseError:
12561257
if EMPTY_RESPONSE in options:
12571258
return options[EMPTY_RESPONSE]
12581259
raise
1260+
1261+
if EMPTY_RESPONSE in options:
1262+
options.pop(EMPTY_RESPONSE)
1263+
12591264
if command_name in self.response_callbacks:
12601265
return self.response_callbacks[command_name](response, **options)
12611266
return response

tests/test_asyncio/test_commands.py

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

1818
import redis
1919
from redis import exceptions
20-
from redis.client import parse_info
20+
from redis.client import EMPTY_RESPONSE, NEVER_DECODE, parse_info
2121
from tests.conftest import (
2222
skip_if_server_version_gte,
2323
skip_if_server_version_lt,
@@ -598,6 +598,16 @@ async def test_time(self, r: redis.Redis):
598598
assert isinstance(t[0], int)
599599
assert isinstance(t[1], int)
600600

601+
async def test_never_decode_option(self, r: redis.Redis):
602+
opts = {NEVER_DECODE: []}
603+
await r.delete("a")
604+
assert await r.execute_command("EXISTS", "a", **opts) == 0
605+
606+
async def test_empty_response_option(self, r: redis.Redis):
607+
opts = {EMPTY_RESPONSE: []}
608+
await r.delete("a")
609+
assert await r.execute_command("EXISTS", "a", **opts) == 0
610+
601611
# BASIC KEY COMMANDS
602612
async def test_append(self, r: redis.Redis):
603613
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,
@@ -899,6 +899,16 @@ def test_bgsave(self, r):
899899
time.sleep(0.3)
900900
assert r.bgsave(True)
901901

902+
def test_never_decode_option(self, r: redis.Redis):
903+
opts = {NEVER_DECODE: []}
904+
r.delete("a")
905+
assert r.execute_command("EXISTS", "a", **opts) == 0
906+
907+
def test_empty_response_option(self, r: redis.Redis):
908+
opts = {EMPTY_RESPONSE: []}
909+
r.delete("a")
910+
assert r.execute_command("EXISTS", "a", **opts) == 0
911+
902912
# BASIC KEY COMMANDS
903913
def test_append(self, r):
904914
assert r.append("a", "a1") == 2

0 commit comments

Comments
 (0)