diff options
author | Andy McCurdy <andy@andymccurdy.com> | 2019-12-29 12:16:50 -0800 |
---|---|---|
committer | Andy McCurdy <andy@andymccurdy.com> | 2019-12-29 12:16:50 -0800 |
commit | 8b76019c7d6b2eaa543e5dbf16c050cc6155efb1 (patch) | |
tree | 1373a358d103ebeae97774a3577b61b0eca0c2f2 | |
parent | 8df8cd54d135380ad8b3b8807a67a3e6915b0b49 (diff) | |
download | redis-py-8b76019c7d6b2eaa543e5dbf16c050cc6155efb1.tar.gz |
Testing the boolean nature of Pipeline instance should always return True.
Prior to this, pipeline instances used __len__() which returns the number
of queued commands on the pipeline. When there were no queued commands,
the pipeline instance would evaluate to 0 or False.
Fixes #994
-rw-r--r-- | CHANGES | 4 | ||||
-rwxr-xr-x | redis/client.py | 8 | ||||
-rw-r--r-- | tests/test_pipeline.py | 8 |
3 files changed, 17 insertions, 3 deletions
@@ -6,6 +6,10 @@ * Removed support for end of life Python 3.4. * Added support for all ACL commands in Redis 6. Thanks @IAmATeaPot418 for helping. + * Pipeline instances now always evaluate to True. Prior to this change, + pipeline instances relied on __len__ for boolean evaluation which + meant that pipelines with no commands on the stack would be considered + False. #994 * 3.3.11 * Further fix for the SSLError -> TimeoutError mapping to work on obscure releases of Python 2.7. diff --git a/redis/client.py b/redis/client.py index c0ac25e..93da07c 100755 --- a/redis/client.py +++ b/redis/client.py @@ -3703,6 +3703,14 @@ class Pipeline(Redis): def __len__(self): return len(self.command_stack) + def __nonzero__(self): + "Pipeline instances should always evaluate to True on Python 2.7" + return True + + def __bool__(self): + "Pipeline instances should always evaluate to True on Python 3+" + return True + def reset(self): self.command_stack = [] self.scripts = set() diff --git a/tests/test_pipeline.py b/tests/test_pipeline.py index 264a64f..08b7fba 100644 --- a/tests/test_pipeline.py +++ b/tests/test_pipeline.py @@ -6,6 +6,11 @@ from redis._compat import unichr, unicode class TestPipeline(object): + def test_pipeline_is_true(self, r): + "Ensure pipeline instances are not false-y" + with r.pipeline() as pipe: + assert pipe + def test_pipeline(self, r): with r.pipeline() as pipe: (pipe.set('a', 'a1') @@ -28,17 +33,14 @@ class TestPipeline(object): with r.pipeline() as pipe: # Initially empty. assert len(pipe) == 0 - assert not pipe # Fill 'er up! pipe.set('a', 'a1').set('b', 'b1').set('c', 'c1') assert len(pipe) == 3 - assert pipe # Execute calls reset(), so empty once again. pipe.execute() assert len(pipe) == 0 - assert not pipe def test_pipeline_no_transaction(self, r): with r.pipeline(transaction=False) as pipe: |