From 6e86ff4a87fd223275d9047e24dd36e5134ff483 Mon Sep 17 00:00:00 2001 From: Eoghan Murray Date: Sat, 22 Aug 2020 11:55:52 +0100 Subject: [PATCH] Change pipeline behaviour to autoexecute upon exit of the `with` block - a behaviour I very much expected --- redis/client.py | 2 +- tests/test_pipeline.py | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/redis/client.py b/redis/client.py index 1560c96afd..163968b673 100755 --- a/redis/client.py +++ b/redis/client.py @@ -3874,7 +3874,7 @@ def __enter__(self): return self def __exit__(self, exc_type, exc_value, traceback): - self.reset() + self.execute() # also resets def __del__(self): try: diff --git a/tests/test_pipeline.py b/tests/test_pipeline.py index 9bc4a9f4d9..727068b51d 100644 --- a/tests/test_pipeline.py +++ b/tests/test_pipeline.py @@ -51,6 +51,15 @@ def test_pipeline_length(self, r): pipe.execute() assert len(pipe) == 0 + def test_pipeline_autoexecute(self, r): + with r.pipeline() as pipe: + # Fill 'er up! + pipe.set('d', 'd1').set('e', 'e1').set('f', 'f1') + assert len(pipe) == 3 + + # exiting with block calls execute() and reset(), so empty once again + assert len(pipe) == 0 + def test_pipeline_no_transaction(self, r): with r.pipeline(transaction=False) as pipe: pipe.set('a', 'a1').set('b', 'b1').set('c', 'c1')