]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
refactor: use consistently the version_pretty() function
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Tue, 9 Apr 2024 20:15:39 +0000 (22:15 +0200)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Fri, 12 Apr 2024 22:09:08 +0000 (00:09 +0200)
psycopg/psycopg/pq/_pq_ctypes.py
psycopg_c/psycopg_c/pq/pgconn.pyx
tools/update_oids.py

index 1f7f482a0f0d9b58c15f5eacbcc513013a008870..c423083fba25f2b76ad9cc209ca50d26c0b0e0f7 100644 (file)
@@ -11,7 +11,7 @@ from ctypes import Structure, CFUNCTYPE, POINTER
 from ctypes import c_char, c_char_p, c_int, c_size_t, c_ubyte, c_uint, c_void_p
 from typing import Any, List, NoReturn, Tuple
 
-from .misc import find_libpq_full_path
+from .misc import find_libpq_full_path, version_pretty
 from ..errors import NotSupportedError
 
 libname = find_libpq_full_path()
@@ -194,8 +194,8 @@ PQhost.restype = c_char_p
 def not_supported_before(fname: str, pgversion: int) -> Any:
     def not_supported(*args: Any, **kwargs: Any) -> NoReturn:
         raise NotSupportedError(
-            f"{fname} requires libpq from PostgreSQL {pgversion} on the client;"
-            f" version {libpq_version // 10000} available instead"
+            f"{fname} requires libpq from PostgreSQL {version_pretty(pgversion)} on"
+            f" the client; version {version_pretty(libpq_version)} available instead"
         )
 
     return not_supported
@@ -206,7 +206,7 @@ if libpq_version >= 120000:
     PQhostaddr.argtypes = [PGconn_ptr]
     PQhostaddr.restype = c_char_p
 else:
-    PQhostaddr = not_supported_before("PQhostaddr", 12)
+    PQhostaddr = not_supported_before("PQhostaddr", 120000)
 
 PQport = pq.PQport
 PQport.argtypes = [PGconn_ptr]
@@ -320,8 +320,8 @@ if libpq_version >= 170000:
     PQclosePortal.restype = PGresult_ptr
 
 else:
-    PQclosePrepared = not_supported_before("PQclosePrepared", 17)
-    PQclosePortal = not_supported_before("PQclosePrepared", 17)
+    PQclosePrepared = not_supported_before("PQclosePrepared", 170000)
+    PQclosePortal = not_supported_before("PQclosePrepared", 170000)
 
 PQresultStatus = pq.PQresultStatus
 PQresultStatus.argtypes = [PGresult_ptr]
@@ -526,8 +526,8 @@ if libpq_version >= 170000:
     PQsendClosePortal.restype = c_int
 
 else:
-    PQsendClosePrepared = not_supported_before("PQsendClosePrepared", 17)
-    PQsendClosePortal = not_supported_before("PQsendClosePortal", 17)
+    PQsendClosePrepared = not_supported_before("PQsendClosePrepared", 170000)
+    PQsendClosePortal = not_supported_before("PQsendClosePortal", 170000)
 
 PQgetResult = pq.PQgetResult
 PQgetResult.argtypes = [PGconn_ptr]
@@ -600,15 +600,15 @@ if libpq_version >= 170000:
     PQcancelFinish.restype = None
 
 else:
-    PQcancelCreate = not_supported_before("PQcancelCreate", 17)
-    PQcancelStart = not_supported_before("PQcancelStart", 17)
-    PQcancelBlocking = not_supported_before("PQcancelBlocking", 17)
-    PQcancelPoll = not_supported_before("PQcancelPoll", 17)
-    PQcancelStatus = not_supported_before("PQcancelStatus", 17)
-    PQcancelSocket = not_supported_before("PQcancelSocket", 17)
-    PQcancelErrorMessage = not_supported_before("PQcancelErrorMessage", 17)
-    PQcancelReset = not_supported_before("PQcancelReset", 17)
-    PQcancelFinish = not_supported_before("PQcancelFinish", 17)
+    PQcancelCreate = not_supported_before("PQcancelCreate", 170000)
+    PQcancelStart = not_supported_before("PQcancelStart", 170000)
+    PQcancelBlocking = not_supported_before("PQcancelBlocking", 170000)
+    PQcancelPoll = not_supported_before("PQcancelPoll", 170000)
+    PQcancelStatus = not_supported_before("PQcancelStatus", 170000)
+    PQcancelSocket = not_supported_before("PQcancelSocket", 170000)
+    PQcancelErrorMessage = not_supported_before("PQcancelErrorMessage", 170000)
+    PQcancelReset = not_supported_before("PQcancelReset", 170000)
+    PQcancelFinish = not_supported_before("PQcancelFinish", 170000)
 
 
 PQgetCancel = pq.PQgetCancel
@@ -658,7 +658,7 @@ if libpq_version >= 140000:
     PQsetTraceFlags.argtypes = [PGconn_ptr, c_int]
     PQsetTraceFlags.restype = None
 else:
-    PQsetTraceFlags = not_supported_before("PQsetTraceFlags", 14)
+    PQsetTraceFlags = not_supported_before("PQsetTraceFlags", 140000)
 
 PQuntrace = pq.PQuntrace
 PQuntrace.argtypes = [PGconn_ptr]
@@ -680,7 +680,7 @@ if libpq_version >= 100000:
     ]
     PQencryptPasswordConn.restype = POINTER(c_char)
 else:
-    PQencryptPasswordConn = not_supported_before("PQencryptPasswordConn", 10)
+    PQencryptPasswordConn = not_supported_before("PQencryptPasswordConn", 100000)
 
 PQmakeEmptyPGresult = pq.PQmakeEmptyPGresult
 PQmakeEmptyPGresult.argtypes = [PGconn_ptr, c_int]
@@ -723,11 +723,11 @@ if libpq_version >= 140000:
     PQsendFlushRequest.restype = c_int
 
 else:
-    PQpipelineStatus = not_supported_before("PQpipelineStatus", 14)
-    PQenterPipelineMode = not_supported_before("PQenterPipelineMode", 14)
-    PQexitPipelineMode = not_supported_before("PQexitPipelineMode", 14)
-    PQpipelineSync = not_supported_before("PQpipelineSync", 14)
-    PQsendFlushRequest = not_supported_before("PQsendFlushRequest", 14)
+    PQpipelineStatus = not_supported_before("PQpipelineStatus", 140000)
+    PQenterPipelineMode = not_supported_before("PQenterPipelineMode", 140000)
+    PQexitPipelineMode = not_supported_before("PQexitPipelineMode", 140000)
+    PQpipelineSync = not_supported_before("PQpipelineSync", 140000)
+    PQsendFlushRequest = not_supported_before("PQsendFlushRequest", 140000)
 
 
 # 33.18. SSL Support
index 798c68d0d7c9e719bf325d36e33d965e58b609d0..2f9510328769a3a500a685b1eb85110bf1292404 100644 (file)
@@ -24,15 +24,16 @@ from cpython.memoryview cimport PyMemoryView_FromObject
 
 import sys
 
-from psycopg.pq import Format as PqFormat, Trace
+from psycopg.pq import Format as PqFormat, Trace, version_pretty
 from psycopg.pq.misc import PGnotify, connection_summary
 from psycopg_c.pq cimport PQBuffer
 
 cdef object _check_supported(fname, int pgversion):
     if libpq.PG_VERSION_NUM < pgversion:
         raise e.NotSupportedError(
-            f"{fname} requires libpq from PostgreSQL {pgversion // 10000} on the"
-            f" client; version {libpq.PG_VERSION_NUM // 10000} available instead"
+            f"{fname} requires libpq from PostgreSQL {version_pretty(pgversion)}"
+            f" on the client; version {version_pretty(libpq.PG_VERSION_NUM)}"
+            " available instead"
         )
 
 cdef class PGconn:
index 7e303bb5120d2eeee388eb73722f0b929ec63ce3..80980cc38fffe6e2067a92a37dbf2b286412c8dd 100755 (executable)
@@ -21,6 +21,7 @@ from typing import List
 from pathlib import Path
 
 import psycopg
+from psycopg.pq import version_pretty
 from psycopg.rows import TupleRow
 from psycopg.crdb import CrdbConnection
 from psycopg._compat import TypeAlias
@@ -90,13 +91,10 @@ def update_crdb_python_oids(conn: Connection) -> None:
 
 def get_version_comment(conn: Connection) -> List[str]:
     if conn.info.vendor == "PostgreSQL":
-        # Assume PG > 10
-        num = conn.info.server_version
-        version = f"{num // 10000}.{num % 100}"
+        version = version_pretty(conn.info.server_version)
     elif conn.info.vendor == "CockroachDB":
         assert isinstance(conn, CrdbConnection)
-        num = conn.info.server_version
-        version = f"{num // 10000}.{num % 10000 // 100}.{num % 100}"
+        version = version_pretty(conn.info.server_version)
     else:
         raise NotImplementedError(f"unexpected vendor: {conn.info.vendor}")
     return ["", f"    # Generated from {conn.info.vendor} {version}", ""]