From f7dc953b85beaae5cef0e8156de53963cfb2db0a Mon Sep 17 00:00:00 2001 From: Petar Kostov Date: Mon, 13 Oct 2025 16:54:43 +0300 Subject: [PATCH] Add handling of empty spaces during CLIENT LIST response parsing --- redis/_parsers/helpers.py | 18 ++++++++++++++++-- tests/test_parsers/test_helpers.py | 22 +++++++++++++++++++++- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/redis/_parsers/helpers.py b/redis/_parsers/helpers.py index 154dc66dfb..a698cfc2da 100644 --- a/redis/_parsers/helpers.py +++ b/redis/_parsers/helpers.py @@ -349,8 +349,22 @@ def parse_zadd(response, **options): def parse_client_list(response, **options): clients = [] for c in str_if_bytes(response).splitlines(): - # Values might contain '=' - clients.append(dict(pair.split("=", 1) for pair in c.split(" "))) + client_dict = {} + tokens = c.split(" ") + last_key = None + for token in tokens: + if "=" in token: + # Values might contain '=' + key, value = token.split("=", 1) + client_dict[key] = value + last_key = key + else: + # Values may include spaces. For instance, when running Redis via a Unix socket — such as + # "/tmp/redis sock/redis.sock" — the addr or laddr field will include a space. + client_dict[last_key] += " " + token + + if client_dict: + clients.append(client_dict) return clients diff --git a/tests/test_parsers/test_helpers.py b/tests/test_parsers/test_helpers.py index 383b9de794..0ab5a49083 100644 --- a/tests/test_parsers/test_helpers.py +++ b/tests/test_parsers/test_helpers.py @@ -1,4 +1,4 @@ -from redis._parsers.helpers import parse_info +from redis._parsers.helpers import parse_info, parse_client_list def test_parse_info(): @@ -61,3 +61,23 @@ def test_parse_info_list_dict_mixed(): assert isinstance(info["list_two"], dict) assert info["list_two"] == {"a b": "foo", "c": True, "d": "bar", "e": True} + + +def test_parse_client_list(): + 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=" + expected = [ + { + "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": "", + } + ] + clients = parse_client_list(response) + assert clients == expected