From: Daniele Varrazzo Date: Sat, 11 Apr 2020 09:01:46 +0000 (+1200) Subject: Added connection status check on send_query X-Git-Tag: 3.0.dev0~574 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=750b23a47573de4a7f04923d3a671577f93bfe70;p=thirdparty%2Fpsycopg.git Added connection status check on send_query --- diff --git a/psycopg3/pq/pq_ctypes.py b/psycopg3/pq/pq_ctypes.py index b40e8f31c..af0df71b9 100644 --- a/psycopg3/pq/pq_ctypes.py +++ b/psycopg3/pq/pq_ctypes.py @@ -194,6 +194,7 @@ class PGconn: def send_query(self, command: bytes) -> None: if not isinstance(command, bytes): raise TypeError(f"bytes expected, got {type(command)} instead") + self._ensure_pgconn() if not impl.PQsendQuery(self.pgconn_ptr, command): raise PQerror(f"sending query failed: {error_message(self)}") diff --git a/tests/pq/test_async.py b/tests/pq/test_async.py index a04b7d678..d5a0e185d 100644 --- a/tests/pq/test_async.py +++ b/tests/pq/test_async.py @@ -1,4 +1,6 @@ +import pytest from select import select +import psycopg3 def test_send_query(pq, pgconn): @@ -69,9 +71,17 @@ def test_send_query_compact_test(pq, conn): assert results[1].fname(0) == b"foo" assert results[1].get_value(0, 0) == b"1" + conn.pgconn.finish() + with pytest.raises(psycopg3.OperationalError): + conn.pgconn.send_query(b"select 1") + def test_send_query_params(pq, conn): res = conn.pgconn.send_query_params(b"select $1::int + $2", [b"5", b"3"]) (res,) = conn.wait(conn._exec_gen(conn.pgconn)) assert res.status == pq.ExecStatus.TUPLES_OK assert res.get_value(0, 0) == b"8" + + conn.pgconn.finish() + with pytest.raises(psycopg3.OperationalError): + conn.pgconn.send_query_params(b"select $1", [b"1"])