From: Daniele Varrazzo Date: Sun, 8 May 2022 20:26:28 +0000 (+0200) Subject: fix: raise a clean error on pipeline() on a closed connection X-Git-Tag: 3.1~115 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d89005421d4c9d238984e365b269f663bdffcd92;p=thirdparty%2Fpsycopg.git fix: raise a clean error on pipeline() on a closed connection --- diff --git a/psycopg/psycopg/connection.py b/psycopg/psycopg/connection.py index 8d01d8630..225f0c56e 100644 --- a/psycopg/psycopg/connection.py +++ b/psycopg/psycopg/connection.py @@ -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. diff --git a/psycopg/psycopg/connection_async.py b/psycopg/psycopg/connection_async.py index d0f0b181b..af37e1618 100644 --- a/psycopg/psycopg/connection_async.py +++ b/psycopg/psycopg/connection_async.py @@ -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. diff --git a/tests/test_pipeline.py b/tests/test_pipeline.py index d5ef4185e..e5c7d37ee 100644 --- a/tests/test_pipeline.py +++ b/tests/test_pipeline.py @@ -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: diff --git a/tests/test_pipeline_async.py b/tests/test_pipeline_async.py index 420734824..af7f5bb08 100644 --- a/tests/test_pipeline_async.py +++ b/tests/test_pipeline_async.py @@ -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: