From: Daniele Varrazzo Date: Sat, 11 Apr 2020 07:58:39 +0000 (+1200) Subject: reset and poll fail too if the connection is gone X-Git-Tag: 3.0.dev0~579 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9a49bfe54cd81c573f51251d1c96cc1cbbf23514;p=thirdparty%2Fpsycopg.git reset and poll fail too if the connection is gone --- diff --git a/psycopg3/pq/pq_ctypes.py b/psycopg3/pq/pq_ctypes.py index ce9f5e765..ae7b8d7b0 100644 --- a/psycopg3/pq/pq_ctypes.py +++ b/psycopg3/pq/pq_ctypes.py @@ -65,7 +65,7 @@ class PGconn: return cls(pgconn_ptr) def connect_poll(self) -> PollingStatus: - rv = impl.PQconnectPoll(self.pgconn_ptr) + rv = self._call_int(impl.PQconnectPoll) return PollingStatus(rv) def finish(self) -> None: @@ -86,6 +86,8 @@ class PGconn: impl.PQconninfoFree(opts) def reset(self) -> None: + if not self.pgconn_ptr: + raise PQerror("the connection is no more available") impl.PQreset(self.pgconn_ptr) def reset_start(self) -> None: @@ -93,7 +95,7 @@ class PGconn: raise PQerror("couldn't reset connection") def reset_poll(self) -> PollingStatus: - rv = impl.PQresetPoll(self.pgconn_ptr) + rv = self._call_int(impl.PQresetPoll) return PollingStatus(rv) @classmethod diff --git a/tests/pq/test_pgconn.py b/tests/pq/test_pgconn.py index 881a29cc7..65b16b4c4 100644 --- a/tests/pq/test_pgconn.py +++ b/tests/pq/test_pgconn.py @@ -38,6 +38,10 @@ def test_connect_async(pq, dsn): assert conn.status == pq.ConnStatus.OK + conn.finish() + with pytest.raises(psycopg3.OperationalError): + conn.connect_poll() + def test_connect_async_bad(pq, dsn): conn = pq.PGconn.connect_start(b"dbname=psycopg3_test_not_for_real") @@ -89,9 +93,10 @@ def test_reset(pq, pgconn): pgconn.reset() assert pgconn.status == pq.ConnStatus.OK - # doesn't work after finish, but doesn't die either pgconn.finish() - pgconn.reset() + with pytest.raises(psycopg3.OperationalError): + pgconn.reset() + assert pgconn.status == pq.ConnStatus.BAD @@ -116,7 +121,8 @@ def test_reset_async(pq, pgconn): with pytest.raises(psycopg3.OperationalError): pgconn.reset_start() - assert pgconn.reset_poll() == 0 + with pytest.raises(psycopg3.OperationalError): + pgconn.reset_poll() def test_ping(pq, dsn):