]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
fix: raise a clean error on pipeline() on a closed connection
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sun, 8 May 2022 20:26:28 +0000 (22:26 +0200)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sun, 8 May 2022 20:26:28 +0000 (22:26 +0200)
psycopg/psycopg/connection.py
psycopg/psycopg/connection_async.py
tests/test_pipeline.py
tests/test_pipeline_async.py

index 8d01d86306f12de9cf3815c4c5981789b5444873..225f0c56ee15023e788bd30152b013a36c8850b4 100644 (file)
@@ -885,6 +885,8 @@ class Connection(BaseConnection[Row]):
     def pipeline(self) -> Iterator[Pipeline]:
         """Switch the connection into pipeline mode."""
         with self.lock:
+            self._check_connection_ok()
+
             pipeline = self._pipeline
             if pipeline is None:
                 # WARNING: reference loop, broken ahead.
index d0f0b181b6f6bf98741f5c236b3bc4704cbfe22f..af37e1618c4027066e7e9578a73b1366afc720be 100644 (file)
@@ -298,6 +298,8 @@ class AsyncConnection(BaseConnection[Row]):
     async def pipeline(self) -> AsyncIterator[AsyncPipeline]:
         """Context manager to switch the connection into pipeline mode."""
         async with self.lock:
+            self._check_connection_ok()
+
             pipeline = self._pipeline
             if pipeline is None:
                 # WARNING: reference loop, broken ahead.
index d5ef4185ecbafe9ff918dd78d952fd3dd3679242..e5c7d37ee5f20c821149b224dd431226c4b55a7e 100644 (file)
@@ -22,6 +22,13 @@ def test_repr(conn):
     assert "[BAD]" in repr(p)
 
 
+def test_connection_closed(conn):
+    conn.close()
+    with pytest.raises(e.OperationalError):
+        with conn.pipeline():
+            pass
+
+
 def test_pipeline_status(conn: psycopg.Connection[Any]) -> None:
     assert conn._pipeline is None
     with conn.pipeline() as p:
index 4207348248c0156da1a679da2641d91adbe690c7..af7f5bb082615efbe95f8c7afa4c80c44fa6a0a8 100644 (file)
@@ -25,6 +25,13 @@ async def test_repr(aconn):
     assert "[BAD]" in repr(p)
 
 
+async def test_connection_closed(aconn):
+    await aconn.close()
+    with pytest.raises(e.OperationalError):
+        async with aconn.pipeline():
+            pass
+
+
 async def test_pipeline_status(aconn: psycopg.AsyncConnection[Any]) -> None:
     assert aconn._pipeline is None
     async with aconn.pipeline() as p: