]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Expose the libpq build version as psycopg.pq.__build_version__
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Tue, 21 Sep 2021 18:54:24 +0000 (19:54 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Tue, 21 Sep 2021 18:54:24 +0000 (19:54 +0100)
docs/api/pq.rst
psycopg/psycopg/pq/__init__.py
psycopg_c/psycopg_c/pq.pyx
tests/fix_pq.py
tests/pq/test_pq.py

index b3c770eb758993b06b29e2e7ed039b0913093957..c55a414ab5727d58128279b216938937b657ae57 100644 (file)
@@ -76,6 +76,8 @@ Module content
     .. seealso:: the :pq:`PQlibVersion()` function
 
 
+.. autodata:: __build_version__
+
 .. autofunction:: error_message
 
 
index 1211d521bcdab9051f4d2c08611ff7c39724d14e..5b46c36dff5a376e410f3593c4566a0f2f91d95a 100644 (file)
@@ -11,7 +11,7 @@ implementation-dependant but all the implementations share the same interface.
 
 import os
 import logging
-from typing import Callable, List, Type
+from typing import Callable, List, Optional, Type
 
 from . import abc
 from .misc import ConninfoOption, PGnotify, PGresAttDesc
@@ -27,6 +27,16 @@ __impl__: str
 Possible values include ``python``, ``c``, ``binary``.
 """
 
+__build_version__: Optional[int]
+"""The libpq version the C package was built with.
+
+A number in the same format of `~psycopg.ConnectionInfo.server_version`
+representing the libpq used to build the speedup module (``c``, ``binary``) if
+available. `!None` if `__impl__` is ``python``.
+
+Certain features might not be available if the built version is too old.
+"""
+
 version: Callable[[], int]
 PGconn: Type[abc.PGconn]
 PGresult: Type[abc.PGresult]
@@ -43,7 +53,8 @@ def import_from_libpq() -> None:
     try to import the best implementation available.
     """
     # import these names into the module on success as side effect
-    global __impl__, version, PGconn, PGresult, Conninfo, Escaping, PGcancel
+    global __impl__, version, __build_version__
+    global PGconn, PGresult, Conninfo, Escaping, PGcancel
 
     impl = os.environ.get("PSYCOPG_IMPL", "").lower()
     module = None
@@ -87,6 +98,7 @@ def import_from_libpq() -> None:
         Conninfo = module.Conninfo
         Escaping = module.Escaping
         PGcancel = module.PGcancel
+        __build_version__ = getattr(module, "__build_version__", None)
     elif impl:
         raise ImportError(f"requested psycopg impementation '{impl}' unknown")
     else:
index c8009272f79914707e11a608a45c21ab2c82ee50..692755f5d7cc46c47605ea33d89465a9f8f4bdb4 100644 (file)
@@ -11,6 +11,7 @@ from psycopg.pq import Format
 from psycopg.pq.misc import error_message
 
 __impl__ = 'c'
+__build_version__ = libpq.PG_VERSION_NUM
 
 
 def version():
index bf1ca14f95fbab4473daa81bbae4b99aab4a7c1c..055178c564543f60855de6ec1d3c1dae5cbbe117 100644 (file)
@@ -12,8 +12,9 @@ def pytest_report_header(config):
         return []
 
     return [
-        f"libpq available: {pq.version()}",
         f"libpq wrapper implementation: {pq.__impl__}",
+        f"libpq used: {pq.version()}",
+        f"libpq compiled: {pq.__build_version__}",
     ]
 
 
index 98b7b626644c55fa802510a1a7b3120e94949d83..df660dc83701ec9bc7c9526b7df692f924fa46d8 100644 (file)
@@ -5,3 +5,12 @@ def test_version():
     rv = pq.version()
     assert rv > 90500
     assert rv < 200000  # you are good for a while
+
+
+def test_build_version():
+    if pq.__impl__ == "python":
+        assert pq.__build_version__ is None
+    elif pq.__impl__ in ["c", "binary"]:
+        assert pq.__build_version__ >= 70400
+    else:
+        assert False, f"unexpected libpq implementation: {pq.__impl__}"