Skip to content

Commit 8b76019

Browse files
committed
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
1 parent 8df8cd5 commit 8b76019

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

CHANGES

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
* Removed support for end of life Python 3.4.
77
* Added support for all ACL commands in Redis 6. Thanks @IAmATeaPot418
88
for helping.
9+
* Pipeline instances now always evaluate to True. Prior to this change,
10+
pipeline instances relied on __len__ for boolean evaluation which
11+
meant that pipelines with no commands on the stack would be considered
12+
False. #994
913
* 3.3.11
1014
* Further fix for the SSLError -> TimeoutError mapping to work
1115
on obscure releases of Python 2.7.

redis/client.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3703,6 +3703,14 @@ def __del__(self):
37033703
def __len__(self):
37043704
return len(self.command_stack)
37053705

3706+
def __nonzero__(self):
3707+
"Pipeline instances should always evaluate to True on Python 2.7"
3708+
return True
3709+
3710+
def __bool__(self):
3711+
"Pipeline instances should always evaluate to True on Python 3+"
3712+
return True
3713+
37063714
def reset(self):
37073715
self.command_stack = []
37083716
self.scripts = set()

tests/test_pipeline.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77

88
class TestPipeline(object):
9+
def test_pipeline_is_true(self, r):
10+
"Ensure pipeline instances are not false-y"
11+
with r.pipeline() as pipe:
12+
assert pipe
13+
914
def test_pipeline(self, r):
1015
with r.pipeline() as pipe:
1116
(pipe.set('a', 'a1')
@@ -28,17 +33,14 @@ def test_pipeline_length(self, r):
2833
with r.pipeline() as pipe:
2934
# Initially empty.
3035
assert len(pipe) == 0
31-
assert not pipe
3236

3337
# Fill 'er up!
3438
pipe.set('a', 'a1').set('b', 'b1').set('c', 'c1')
3539
assert len(pipe) == 3
36-
assert pipe
3740

3841
# Execute calls reset(), so empty once again.
3942
pipe.execute()
4043
assert len(pipe) == 0
41-
assert not pipe
4244

4345
def test_pipeline_no_transaction(self, r):
4446
with r.pipeline(transaction=False) as pipe:

0 commit comments

Comments
 (0)