diff --git a/redis/commands/core.py b/redis/commands/core.py index c367827359..d1f3c7fc50 100644 --- a/redis/commands/core.py +++ b/redis/commands/core.py @@ -1363,6 +1363,7 @@ def bitcount( key: KeyT, start: Union[int, None] = None, end: Union[int, None] = None, + mode: Optional[str] = None, ) -> ResponseT: """ Returns the count of set bits in the value of ``key``. Optional @@ -1376,6 +1377,8 @@ def bitcount( params.append(end) elif (start is not None and end is None) or (end is not None and start is None): raise DataError("Both start and end must be specified") + if mode is not None: + params.append(mode) return self.execute_command("BITCOUNT", *params) def bitfield( @@ -1411,6 +1414,7 @@ def bitpos( bit: int, start: Union[int, None] = None, end: Union[int, None] = None, + mode: Optional[str] = None, ) -> ResponseT: """ Return the position of the first bit set to 1 or 0 in a string. @@ -1430,6 +1434,9 @@ def bitpos( params.append(end) elif start is None and end is not None: raise DataError("start argument is not set, " "when end is specified") + + if mode is not None: + params.append(mode) return self.execute_command("BITPOS", *params) def copy( diff --git a/tests/test_commands.py b/tests/test_commands.py index de6bcead1c..8fdc69259b 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -854,6 +854,15 @@ def test_bitcount(self, r): assert r.bitcount("a", -2, -1) == 2 assert r.bitcount("a", 1, 1) == 1 + @skip_if_server_version_lt("7.0.0") + def test_bitcount_mode(self, r): + r.set("mykey", "foobar") + assert r.bitcount("mykey") == 26 + assert r.bitcount("mykey", 1, 1, "byte") == 6 + assert r.bitcount("mykey", 5, 30, "bit") == 17 + with pytest.raises(redis.ResponseError): + assert r.bitcount("mykey", 5, 30, "but") + @pytest.mark.onlynoncluster @skip_if_server_version_lt("2.6.0") def test_bitop_not_empty_string(self, r): @@ -926,6 +935,15 @@ def test_bitpos_wrong_arguments(self, r): with pytest.raises(exceptions.RedisError): r.bitpos(key, 7) == 12 + @skip_if_server_version_lt("7.0.0") + def test_bitpos_mode(self, r): + r.set("mykey", b"\x00\xff\xf0") + assert r.bitpos("mykey", 1, 0) == 8 + assert r.bitpos("mykey", 1, 2, -1, "byte") == 16 + assert r.bitpos("mykey", 0, 7, 15, "bit") == 7 + with pytest.raises(redis.ResponseError): + r.bitpos("mykey", 1, 7, 15, "bite") + @pytest.mark.onlynoncluster @skip_if_server_version_lt("6.2.0") def test_copy(self, r):