Skip to content

Commit c1e566f

Browse files
peperonpetyaslavova
authored andcommitted
Add handling of empty spaces during CLIENT LIST response parsing (#3797)
1 parent 295bec6 commit c1e566f

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

redis/_parsers/helpers.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,8 +382,22 @@ def parse_zadd(response, **options):
382382
def parse_client_list(response, **options):
383383
clients = []
384384
for c in str_if_bytes(response).splitlines():
385-
# Values might contain '='
386-
clients.append(dict(pair.split("=", 1) for pair in c.split(" ")))
385+
client_dict = {}
386+
tokens = c.split(" ")
387+
last_key = None
388+
for token in tokens:
389+
if "=" in token:
390+
# Values might contain '='
391+
key, value = token.split("=", 1)
392+
client_dict[key] = value
393+
last_key = key
394+
else:
395+
# Values may include spaces. For instance, when running Redis via a Unix socket — such as
396+
# "/tmp/redis sock/redis.sock" — the addr or laddr field will include a space.
397+
client_dict[last_key] += " " + token
398+
399+
if client_dict:
400+
clients.append(client_dict)
387401
return clients
388402

389403

tests/test_parsers/test_helpers.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from redis._parsers.helpers import parse_info
1+
from redis._parsers.helpers import parse_info, parse_client_list
22

33

44
def test_parse_info():
@@ -61,3 +61,23 @@ def test_parse_info_list_dict_mixed():
6161

6262
assert isinstance(info["list_two"], dict)
6363
assert info["list_two"] == {"a b": "foo", "c": True, "d": "bar", "e": True}
64+
65+
66+
def test_parse_client_list():
67+
response = "id=7 addr=/tmp/redis sock/redis.sock:0 fd=9 name=test=_complex_[name] age=-1 idle=0 cmd=client|list user=default lib-name=go-redis(,go1.24.4) lib-ver="
68+
expected = [
69+
{
70+
"id": "7",
71+
"addr": "/tmp/redis sock/redis.sock:0",
72+
"fd": "9",
73+
"name": "test=_complex_[name]",
74+
"age": "-1",
75+
"idle": "0",
76+
"cmd": "client|list",
77+
"user": "default",
78+
"lib-name": "go-redis(,go1.24.4)",
79+
"lib-ver": "",
80+
}
81+
]
82+
clients = parse_client_list(response)
83+
assert clients == expected

0 commit comments

Comments
 (0)