]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Allow reading needs_password, used_password on broken connections
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Fri, 23 Apr 2021 00:30:19 +0000 (01:30 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Fri, 23 Apr 2021 00:30:19 +0000 (01:30 +0100)
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.

psycopg3/psycopg3/pq/pq_ctypes.py
psycopg3_c/psycopg3_c/pq/pgconn.pyx
tests/pq/test_pgconn.py

index fccf4ac91ec64eadd86aa6a73b51d3b887d524d1..194ba20ed12e88b519c0d403278c386c88e25c66 100644 (file)
@@ -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:
index 6e16fb33f33d4a9c1db608f5db5b92bf96528883..0cb2095cd53aec1fb04930109f542f9ef8f77406 100644 (file)
@@ -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:
index 77bcc2b3edb0030b95eea563a77bd4d8f28ec3ac..fef38eca912a76e5ea9c04862d054d8d2bfc3a32 100644 (file)
@@ -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):