From: Daniele Varrazzo Date: Tue, 21 Sep 2021 18:54:24 +0000 (+0100) Subject: Expose the libpq build version as psycopg.pq.__build_version__ X-Git-Tag: 3.0~70 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d1b618bf85671476f2200931f829921062df699c;p=thirdparty%2Fpsycopg.git Expose the libpq build version as psycopg.pq.__build_version__ --- diff --git a/docs/api/pq.rst b/docs/api/pq.rst index b3c770eb7..c55a414ab 100644 --- a/docs/api/pq.rst +++ b/docs/api/pq.rst @@ -76,6 +76,8 @@ Module content .. seealso:: the :pq:`PQlibVersion()` function +.. autodata:: __build_version__ + .. autofunction:: error_message diff --git a/psycopg/psycopg/pq/__init__.py b/psycopg/psycopg/pq/__init__.py index 1211d521b..5b46c36df 100644 --- a/psycopg/psycopg/pq/__init__.py +++ b/psycopg/psycopg/pq/__init__.py @@ -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: diff --git a/psycopg_c/psycopg_c/pq.pyx b/psycopg_c/psycopg_c/pq.pyx index c8009272f..692755f5d 100644 --- a/psycopg_c/psycopg_c/pq.pyx +++ b/psycopg_c/psycopg_c/pq.pyx @@ -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(): diff --git a/tests/fix_pq.py b/tests/fix_pq.py index bf1ca14f9..055178c56 100644 --- a/tests/fix_pq.py +++ b/tests/fix_pq.py @@ -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__}", ] diff --git a/tests/pq/test_pq.py b/tests/pq/test_pq.py index 98b7b6266..df660dc83 100644 --- a/tests/pq/test_pq.py +++ b/tests/pq/test_pq.py @@ -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__}"