From: Daniele Varrazzo Date: Fri, 23 Apr 2021 00:30:19 +0000 (+0100) Subject: Allow reading needs_password, used_password on broken connections X-Git-Tag: 3.0.dev0~66^2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=98f4910355dc23ad2a6b045cb9e3395a834a2f1f;p=thirdparty%2Fpsycopg.git Allow reading needs_password, used_password on broken connections It makes sense (especially the former: it is used to detect if a password was needed). However they are not that easy to use because a failed connection attempt will throw an exception. For this reason they are not added to ConnectionInfo. --- diff --git a/psycopg3/psycopg3/pq/pq_ctypes.py b/psycopg3/psycopg3/pq/pq_ctypes.py index fccf4ac91..194ba20ed 100644 --- a/psycopg3/psycopg3/pq/pq_ctypes.py +++ b/psycopg3/psycopg3/pq/pq_ctypes.py @@ -234,11 +234,11 @@ class PGconn: @property def needs_password(self) -> bool: - return self._call_bool(impl.PQconnectionNeedsPassword) + return bool(impl.PQconnectionNeedsPassword(self._pgconn_ptr)) @property def used_password(self) -> bool: - return self._call_bool(impl.PQconnectionUsedPassword) + return bool(impl.PQconnectionUsedPassword(self._pgconn_ptr)) @property def ssl_in_use(self) -> bool: diff --git a/psycopg3_c/psycopg3_c/pq/pgconn.pyx b/psycopg3_c/psycopg3_c/pq/pgconn.pyx index 6e16fb33f..0cb2095cd 100644 --- a/psycopg3_c/psycopg3_c/pq/pgconn.pyx +++ b/psycopg3_c/psycopg3_c/pq/pgconn.pyx @@ -178,11 +178,11 @@ cdef class PGconn: @property def needs_password(self) -> bool: - return bool(_call_int(self, libpq.PQconnectionNeedsPassword)) + return bool(libpq.PQconnectionNeedsPassword(self.pgconn_ptr)) @property def used_password(self) -> bool: - return bool(_call_int(self, libpq.PQconnectionUsedPassword)) + return bool(libpq.PQconnectionUsedPassword(self.pgconn_ptr)) @property def ssl_in_use(self) -> bool: diff --git a/tests/pq/test_pgconn.py b/tests/pq/test_pgconn.py index 77bcc2b3e..fef38eca9 100644 --- a/tests/pq/test_pgconn.py +++ b/tests/pq/test_pgconn.py @@ -323,8 +323,7 @@ def test_needs_password(pgconn): # assume connection worked so an eventually needed password wasn't missing assert pgconn.needs_password is False pgconn.finish() - with pytest.raises(psycopg3.OperationalError): - pgconn.needs_password + pgconn.needs_password def test_used_password(pgconn, dsn, monkeypatch): @@ -347,8 +346,7 @@ def test_used_password(pgconn, dsn, monkeypatch): assert pgconn.used_password pgconn.finish() - with pytest.raises(psycopg3.OperationalError): - pgconn.used_password + pgconn.used_password def test_ssl_in_use(pgconn):