From: Daniele Varrazzo Date: Sun, 27 Mar 2022 17:18:17 +0000 (+0200) Subject: feat: add 'Pipeline.is_supported()' X-Git-Tag: 3.1~146^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=86ec0eabcee0bde299435888c5976dbe9dde7151;p=thirdparty%2Fpsycopg.git feat: add 'Pipeline.is_supported()' This makes easier to write conditional code depending on whether we support pipeline mode or not. --- diff --git a/psycopg/psycopg/_pipeline.py b/psycopg/psycopg/_pipeline.py index 429b2c75d..8a7456910 100644 --- a/psycopg/psycopg/_pipeline.py +++ b/psycopg/psycopg/_pipeline.py @@ -43,6 +43,7 @@ class BasePipeline: command_queue: Deque[PipelineCommand] result_queue: Deque[PendingResult] + _is_supported: Optional[bool] = None def __init__(self, conn: "BaseConnection[Any]") -> None: self._conn = conn @@ -59,6 +60,16 @@ class BasePipeline: self.command_queue.append(self.pgconn.pipeline_sync) self.result_queue.append(None) + @staticmethod + def is_supported() -> bool: + """Return `True` if the psycopg libpq wrapper suports pipeline mode.""" + if BasePipeline._is_supported is None: + # Support only depends on the libpq functions available in the pq + # wrapper, not on the database version. + pq_version = pq.__build_version__ or pq.version() + BasePipeline._is_supported = pq_version >= 140000 + return BasePipeline._is_supported + def _enter(self) -> None: self.pgconn.enter_pipeline_mode() diff --git a/tests/pq/test_pq.py b/tests/pq/test_pq.py index e859a122a..41e4b72a1 100644 --- a/tests/pq/test_pq.py +++ b/tests/pq/test_pq.py @@ -1,3 +1,4 @@ +import psycopg from psycopg import pq @@ -14,3 +15,12 @@ def test_build_version(): assert pq.__build_version__ and pq.__build_version__ >= 70400 else: assert False, f"unexpected libpq implementation: {pq.__impl__}" + + +def test_pipeline_supported(): + # Note: This test is here because pipeline tests are skipped on libpq < 14 + if pq.__impl__ == "python": + assert psycopg.Pipeline.is_supported() == (pq.version() >= 140000) + else: + assert pq.__build_version__ is not None + assert psycopg.Pipeline.is_supported() == (pq.__build_version__ >= 140000)