diff options
-rw-r--r-- | redis/commands/core.py | 7 | ||||
-rw-r--r-- | tests/test_commands.py | 18 |
2 files changed, 25 insertions, 0 deletions
diff --git a/redis/commands/core.py b/redis/commands/core.py index 55ec5ec..e2d45fe 100644 --- a/redis/commands/core.py +++ b/redis/commands/core.py @@ -1363,6 +1363,7 @@ class BasicKeyCommands(CommandsProtocol): 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 @@ class BasicKeyCommands(CommandsProtocol): 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 @@ class BasicKeyCommands(CommandsProtocol): 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 @@ class BasicKeyCommands(CommandsProtocol): 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 a0d57f0..de94539 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -863,6 +863,15 @@ class TestRedisCommands: 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): @@ -935,6 +944,15 @@ class TestRedisCommands: 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): |