From 40dfa0b2e2b2df41baf82896fe9e50966437856f Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Wed, 7 May 2025 23:40:23 +0200 Subject: [PATCH] fix: remove assert calling informative libpq function At least calling PQport on a connection with multiple attempts ends up returning a NULL instead of an empty string (libpq from Postgres 17.4). --- psycopg/psycopg/pq/pq_ctypes.py | 3 +-- psycopg_c/psycopg_c/pq/pgconn.pyx | 6 ++++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/psycopg/psycopg/pq/pq_ctypes.py b/psycopg/psycopg/pq/pq_ctypes.py index d58396014..7c16451e3 100644 --- a/psycopg/psycopg/pq/pq_ctypes.py +++ b/psycopg/psycopg/pq/pq_ctypes.py @@ -790,8 +790,7 @@ class PGconn: if not self._pgconn_ptr: raise e.OperationalError("the connection is closed") rv = func(self._pgconn_ptr) - assert rv is not None - return rv + return rv if rv is not None else b"" def _call_int(self, func: Callable[[impl.PGconn_struct], int]) -> int: """ diff --git a/psycopg_c/psycopg_c/pq/pgconn.pyx b/psycopg_c/psycopg_c/pq/pgconn.pyx index 7c4eccb83..f8e23d4f3 100644 --- a/psycopg_c/psycopg_c/pq/pgconn.pyx +++ b/psycopg_c/psycopg_c/pq/pgconn.pyx @@ -700,8 +700,10 @@ cdef char *_call_bytes(PGconn pgconn, conn_bytes_f func) except NULL: if not _ensure_pgconn(pgconn): return NULL cdef char *rv = func(pgconn._pgconn_ptr) - assert rv is not NULL - return rv + if rv is not NULL: + return rv + else: + return b"" cdef int _call_int(PGconn pgconn, conn_int_f func) except -2: -- 2.39.5