Skip to content

Fix special response parsing options handling #2302

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

Merged
merged 4 commits into from
Nov 9, 2022
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
5 changes: 5 additions & 0 deletions redis/asyncio/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,12 +501,17 @@ async def parse_response(
try:
if NEVER_DECODE in options:
response = await connection.read_response(disable_decoding=True)
options.pop(NEVER_DECODE)
else:
response = await connection.read_response()
except ResponseError:
if EMPTY_RESPONSE in options:
return options[EMPTY_RESPONSE]
raise

if EMPTY_RESPONSE in options:
options.pop(EMPTY_RESPONSE)

if command_name in self.response_callbacks:
# Mypy bug: https://github.com/python/mypy/issues/10977
command_name = cast(str, command_name)
Expand Down
4 changes: 4 additions & 0 deletions redis/asyncio/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -867,13 +867,17 @@ async def parse_response(
try:
if NEVER_DECODE in kwargs:
response = await connection.read_response(disable_decoding=True)
kwargs.pop(NEVER_DECODE)
else:
response = await connection.read_response()
except ResponseError:
if EMPTY_RESPONSE in kwargs:
return kwargs[EMPTY_RESPONSE]
raise

if EMPTY_RESPONSE in kwargs:
kwargs.pop(EMPTY_RESPONSE)

# Return response
if command in self.response_callbacks:
return self.response_callbacks[command](response, **kwargs)
Expand Down
5 changes: 5 additions & 0 deletions redis/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1258,12 +1258,17 @@ def parse_response(self, connection, command_name, **options):
try:
if NEVER_DECODE in options:
response = connection.read_response(disable_decoding=True)
options.pop(NEVER_DECODE)
else:
response = connection.read_response()
except ResponseError:
if EMPTY_RESPONSE in options:
return options[EMPTY_RESPONSE]
raise

if EMPTY_RESPONSE in options:
options.pop(EMPTY_RESPONSE)

if command_name in self.response_callbacks:
return self.response_callbacks[command_name](response, **options)
return response
Expand Down
12 changes: 11 additions & 1 deletion tests/test_asyncio/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import redis
from redis import exceptions
from redis.client import parse_info
from redis.client import EMPTY_RESPONSE, NEVER_DECODE, parse_info
from tests.conftest import (
skip_if_server_version_gte,
skip_if_server_version_lt,
Expand Down Expand Up @@ -542,6 +542,16 @@ async def test_time(self, r: redis.Redis):
assert isinstance(t[0], int)
assert isinstance(t[1], int)

async def test_never_decode_option(self, r: redis.Redis):
opts = {NEVER_DECODE: []}
await r.delete("a")
assert await r.execute_command("EXISTS", "a", **opts) == 0

async def test_empty_response_option(self, r: redis.Redis):
opts = {EMPTY_RESPONSE: []}
await r.delete("a")
assert await r.execute_command("EXISTS", "a", **opts) == 0

# BASIC KEY COMMANDS
async def test_append(self, r: redis.Redis):
assert await r.append("a", "a1") == 2
Expand Down
12 changes: 11 additions & 1 deletion tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import redis
from redis import exceptions
from redis.client import parse_info
from redis.client import EMPTY_RESPONSE, NEVER_DECODE, parse_info

from .conftest import (
_get_client,
Expand Down Expand Up @@ -917,6 +917,16 @@ def test_bgsave(self, r):
time.sleep(0.3)
assert r.bgsave(True)

def test_never_decode_option(self, r: redis.Redis):
opts = {NEVER_DECODE: []}
r.delete("a")
assert r.execute_command("EXISTS", "a", **opts) == 0

def test_empty_response_option(self, r: redis.Redis):
opts = {EMPTY_RESPONSE: []}
r.delete("a")
assert r.execute_command("EXISTS", "a", **opts) == 0

# BASIC KEY COMMANDS
def test_append(self, r):
assert r.append("a", "a1") == 2
Expand Down