From a7559fee257ad85aaaefad84d3566ef9bdda8a02 Mon Sep 17 00:00:00 2001 From: AleksMat Date: Sat, 9 May 2020 20:37:24 +0200 Subject: Fix for HSET argument validation to allow any non-None key Fixes #1337 Fixes #1341 --- CHANGES | 3 +++ redis/client.py | 8 ++++---- tests/test_commands.py | 4 ++++ 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/CHANGES b/CHANGES index b926270..8acb467 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,6 @@ +* 3.5.1 (May 9, 2020) + * Fix for HSET argument validation to allow any non-None key. Thanks + @AleksMat, #1337, #1341 * 3.5.0 (April 29, 2020) * Removed exception trapping from __del__ methods. redis-py objects that hold various resources implement __del__ cleanup methods to release diff --git a/redis/client.py b/redis/client.py index cc77515..6777959 100755 --- a/redis/client.py +++ b/redis/client.py @@ -3034,14 +3034,14 @@ class Redis(object): def hset(self, name, key=None, value=None, mapping=None): """ Set ``key`` to ``value`` within hash ``name``, - Use ``mappings`` keyword args to set multiple key/value pairs - for a hash ``name``. + ``mapping`` accepts a dict of key/value pairs that that will be + added to hash ``name``. Returns the number of fields that were added. """ - if not key and not mapping: + if key is None and not mapping: raise DataError("'hset' with no key value pairs") items = [] - if key: + if key is not None: items.extend((key, value)) if mapping: for pair in mapping.items(): diff --git a/tests/test_commands.py b/tests/test_commands.py index 5c29dd5..65e877c 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -1612,6 +1612,10 @@ class TestRedisCommands(object): # key inside of hash that doesn't exist returns null value assert r.hget('a', 'b') is None + # keys with bool(key) == False + assert r.hset('a', 0, 10) == 1 + assert r.hset('a', '', 10) == 1 + def test_hset_with_multi_key_values(self, r): r.hset('a', mapping={'1': 1, '2': 2, '3': 3}) assert r.hget('a', '1') == b'1' -- cgit v1.2.1