Skip to content

Commit

Permalink
Return Redis sets as Python lists (#189)
Browse files Browse the repository at this point in the history
In some (rare) cases, the sets from a Redis response contain nested
types that are not hashable in Python, for example maps. To handle these
cases uniformly, always return Python lists for Redis sets. The elements
will still be unique, relying on the correctness of data arriving from
the server.

This is a breaking change, although in reality it might not have a big 
impact. Therefore the major version gets bumped to 3.
  • Loading branch information
gerzse authored Jul 19, 2024
1 parent a94bb44 commit c1eefbd
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 9 deletions.
2 changes: 1 addition & 1 deletion hiredis/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "2.4.0"
__version__ = "3.0.0"
7 changes: 0 additions & 7 deletions src/reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,6 @@ static void *tryParentize(const redisReadTask *task, PyObject *obj) {
PyDict_SetItem(parent, last_key, obj);
}
break;
case REDIS_REPLY_SET:
assert(PyAnySet_CheckExact(parent));
PySet_Add(parent, obj);
break;
default:
assert(PyList_CheckExact(parent));
PyList_SET_ITEM(parent, task->idx, obj);
Expand Down Expand Up @@ -162,9 +158,6 @@ static void *createArrayObject(const redisReadTask *task, size_t elements) {
case REDIS_REPLY_MAP:
obj = PyDict_New();
break;
case REDIS_REPLY_SET:
obj = PySet_New(NULL);
break;
default:
obj = PyList_New(elements);
}
Expand Down
6 changes: 5 additions & 1 deletion tests/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,11 @@ def test_none(reader):

def test_set(reader):
reader.feed(b"~3\r\n+tangerine\r\n_\r\n,10.5\r\n")
assert {b"tangerine", None, 10.5} == reader.gets()
assert [b"tangerine", None, 10.5] == reader.gets()

def test_set_with_nested_dict(reader):
reader.feed(b"~2\r\n+tangerine\r\n%1\r\n+a\r\n:1\r\n")
assert [b"tangerine", {b"a": 1}] == reader.gets()

def test_dict(reader):
reader.feed(b"%2\r\n+radius\r\n,4.5\r\n+diameter\r\n:9\r\n")
Expand Down

0 comments on commit c1eefbd

Please sign in to comment.