From: Daniele Varrazzo Date: Tue, 24 Mar 2020 07:44:08 +0000 (+1300) Subject: Added PQlibVersion() wrapper X-Git-Tag: 3.0.dev0~679 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=5b44a1ac2a0243db2ad4b97c1a3ea3690444e553;p=thirdparty%2Fpsycopg.git Added PQlibVersion() wrapper Skip importing PQhostaddr from libpq version where it's not available. --- diff --git a/psycopg3/pq/__init__.py b/psycopg3/pq/__init__.py index 05c7a9225..b9387cca0 100644 --- a/psycopg3/pq/__init__.py +++ b/psycopg3/pq/__init__.py @@ -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", ) diff --git a/psycopg3/pq/_pq_ctypes.py b/psycopg3/pq/_pq_ctypes.py index 1f8eaff9d..16177776c 100644 --- a/psycopg3/pq/_pq_ctypes.py +++ b/psycopg3/pq/_pq_ctypes.py @@ -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] diff --git a/psycopg3/pq/pq_ctypes.py b/psycopg3/pq/pq_ctypes.py index 38a5a4786..a79bb4298 100644 --- a/psycopg3/pq/pq_ctypes.py +++ b/psycopg3/pq/pq_ctypes.py @@ -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 index 000000000..0978496b6 --- /dev/null +++ b/tests/pq/test_pq.py @@ -0,0 +1,4 @@ +def test_version(pq): + rv = pq.version() + assert rv > 90500 + assert rv < 200000 # you are good for a while