Skip to content

Commit 883fca7

Browse files
authored
Get command keys for subcommands (#2170)
* parse subcommands * fix tests
1 parent faf55b6 commit 883fca7

File tree

5 files changed

+32
-3
lines changed

5 files changed

+32
-3
lines changed

redis/client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,10 @@ def parse_command(response, **options):
553553
cmd_dict["first_key_pos"] = command[3]
554554
cmd_dict["last_key_pos"] = command[4]
555555
cmd_dict["step_count"] = command[5]
556+
if len(command) > 7:
557+
cmd_dict["tips"] = command[7]
558+
cmd_dict["key_specifications"] = command[8]
559+
cmd_dict["subcommands"] = command[9]
556560
commands[cmd_name] = cmd_dict
557561
return commands
558562

redis/cluster.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ class AbstractRedisCluster:
240240
[
241241
"ACL CAT",
242242
"ACL DELUSER",
243+
"ACL DRYRUN",
243244
"ACL GENPASS",
244245
"ACL GETUSER",
245246
"ACL HELP",

redis/commands/parser.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,21 @@ def initialize(self, r):
2525
commands[cmd.lower()] = commands.pop(cmd)
2626
self.commands = commands
2727

28+
def parse_subcommand(self, command, **options):
29+
cmd_dict = {}
30+
cmd_name = str_if_bytes(command[0])
31+
cmd_dict["name"] = cmd_name
32+
cmd_dict["arity"] = int(command[1])
33+
cmd_dict["flags"] = [str_if_bytes(flag) for flag in command[2]]
34+
cmd_dict["first_key_pos"] = command[3]
35+
cmd_dict["last_key_pos"] = command[4]
36+
cmd_dict["step_count"] = command[5]
37+
if len(command) > 7:
38+
cmd_dict["tips"] = command[7]
39+
cmd_dict["key_specifications"] = command[8]
40+
cmd_dict["subcommands"] = command[9]
41+
return cmd_dict
42+
2843
# As soon as this PR is merged into Redis, we should reimplement
2944
# our logic to use COMMAND INFO changes to determine the key positions
3045
# https://github.com/redis/redis/pull/8324
@@ -72,8 +87,17 @@ def get_keys(self, redis_conn, *args):
7287
and command["first_key_pos"] == 0
7388
and command["last_key_pos"] == 0
7489
):
90+
is_subcmd = False
91+
if "subcommands" in command:
92+
subcmd_name = f"{cmd_name}|{args[1].lower()}"
93+
for subcmd in command["subcommands"]:
94+
if str_if_bytes(subcmd[0]) == subcmd_name:
95+
command = self.parse_subcommand(subcmd)
96+
is_subcmd = True
97+
7598
# The command doesn't have keys in it
76-
return None
99+
if not is_subcmd:
100+
return None
77101
last_key_pos = command["last_key_pos"]
78102
if last_key_pos < 0:
79103
last_key_pos = len(args) - abs(last_key_pos)

tests/test_cluster.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1121,7 +1121,6 @@ def test_cluster_links(self, r):
11211121
links_to = sum(x.count("to") for x in res)
11221122
links_for = sum(x.count("from") for x in res)
11231123
assert links_to == links_for
1124-
print(res)
11251124
for i in range(0, len(res) - 1, 2):
11261125
assert res[i][3] == res[i + 1][3]
11271126

tests/test_commands.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1164,7 +1164,7 @@ def test_expire_option_xx(self, r):
11641164
r.set("key", "val")
11651165
assert r.expire("key", 100, xx=True) == 0
11661166
assert r.expire("key", 100)
1167-
assert r.expire("key", 500, nx=True) == 1
1167+
assert r.expire("key", 500, xx=True) == 1
11681168

11691169
@skip_if_server_version_lt("7.0.0")
11701170
def test_expire_option_gt(self, r):
@@ -3012,6 +3012,7 @@ def test_sort_all_options(self, r):
30123012
assert r.lrange("sorted", 0, 10) == [b"vodka", b"milk", b"gin", b"apple juice"]
30133013

30143014
@skip_if_server_version_lt("7.0.0")
3015+
@pytest.mark.onlynoncluster
30153016
def test_sort_ro(self, r):
30163017
r["score:1"] = 8
30173018
r["score:2"] = 3

0 commit comments

Comments
 (0)