]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Added PQlibVersion() wrapper
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Tue, 24 Mar 2020 07:44:08 +0000 (20:44 +1300)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Tue, 24 Mar 2020 08:05:54 +0000 (21:05 +1300)
Skip importing PQhostaddr from libpq version where it's not available.

psycopg3/pq/__init__.py
psycopg3/pq/_pq_ctypes.py
psycopg3/pq/pq_ctypes.py
tests/pq/test_pq.py [new file with mode: 0644]

index 05c7a9225e2860bb95ce2fd73d3ab5f345ba830e..b9387cca059547a8ae629ebb784db6106f3905af 100644 (file)
@@ -22,6 +22,7 @@ from .misc import error_message
 
 from . import pq_ctypes as pq_module
 
+version = pq_module.version
 PGconn = pq_module.PGconn
 PGresult = pq_module.PGresult
 PQerror = pq_module.PQerror
@@ -39,4 +40,5 @@ __all__ = (
     "PQerror",
     "error_message",
     "py_codecs",
+    "version",
 )
index 1f8eaff9daaa0217ec0cbd8fa651d751c34f05c0..16177776c1e0ec94682ebcc8a60a2807c9198ac5 100644 (file)
@@ -9,8 +9,18 @@ import ctypes.util
 from ctypes import Structure, POINTER
 from ctypes import c_char, c_char_p, c_int, c_uint, c_void_p
 
+from psycopg3.exceptions import NotSupportedError
+
 pq = ctypes.pydll.LoadLibrary(ctypes.util.find_library("pq"))
 
+# Get the libpq version to define what functions are available.
+
+PQlibVersion = pq.PQlibVersion
+PQlibVersion.argtypes = []
+PQlibVersion.restype = c_int
+
+libpq_version = PQlibVersion()
+
 
 # libpq data types
 
@@ -121,9 +131,18 @@ PQhost = pq.PQhost
 PQhost.argtypes = [PGconn_ptr]
 PQhost.restype = c_char_p
 
-PQhostaddr = pq.PQhostaddr
-PQhostaddr.argtypes = [PGconn_ptr]
-PQhostaddr.restype = c_char_p
+if libpq_version >= 120000:
+    PQhostaddr = pq.PQhostaddr
+    PQhostaddr.argtypes = [PGconn_ptr]
+    PQhostaddr.restype = c_char_p
+else:
+
+    def PQhostaddr(pgconn):
+        raise NotSupportedError(
+            f"PQhostaddr requires libpq from PostgreSQL 12,"
+            f" {libpq_version} available instead"
+        )
+
 
 PQport = pq.PQport
 PQport.argtypes = [PGconn_ptr]
index 38a5a47866326405e42d26826928c14955746285..a79bb4298f0ba0f13917ad82ee8242c50f43206e 100644 (file)
@@ -24,6 +24,10 @@ from . import _pq_ctypes as impl
 from ..exceptions import OperationalError
 
 
+def version():
+    return impl.PQlibVersion()
+
+
 class PQerror(OperationalError):
     pass
 
diff --git a/tests/pq/test_pq.py b/tests/pq/test_pq.py
new file mode 100644 (file)
index 0000000..0978496
--- /dev/null
@@ -0,0 +1,4 @@
+def test_version(pq):
+    rv = pq.version()
+    assert rv > 90500
+    assert rv < 200000  # you are good for a while