From: Daniele Varrazzo Date: Mon, 4 May 2020 16:44:45 +0000 (+1200) Subject: Keep pq_cython as an extension separate from the rest X-Git-Tag: 3.0.dev0~540 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=44877ca03658779949e1f48a624aacae1f578473;p=thirdparty%2Fpsycopg.git Keep pq_cython as an extension separate from the rest --- diff --git a/psycopg3/_psycopg3.pyx b/psycopg3/_psycopg3.pyx index f35a0b7de..007af0a48 100644 --- a/psycopg3/_psycopg3.pyx +++ b/psycopg3/_psycopg3.pyx @@ -1,4 +1,3 @@ -include "pq/pq_cython.pyx" include "types/numeric.pyx" include "types/text.pyx" include "transform.pyx" diff --git a/psycopg3/pq/__init__.py b/psycopg3/pq/__init__.py index 59eddcdab..cbb90557a 100644 --- a/psycopg3/pq/__init__.py +++ b/psycopg3/pq/__init__.py @@ -47,7 +47,7 @@ def import_from_libpq() -> None: if not impl or impl == "c": try: # TODO: extension module not recognised by mypy? - from psycopg3 import _psycopg3 # type: ignore + from . import pq_cython # type: ignore except Exception as e: if not impl: logger.debug(f"C pq wrapper not available: %s", e) @@ -56,12 +56,12 @@ def import_from_libpq() -> None: f"requested pq implementation '{impl}' not available" ) from e else: - __impl__ = _psycopg3.__impl__ - version = _psycopg3.version - PGconn = _psycopg3.PGconn - PGresult = _psycopg3.PGresult - Conninfo = _psycopg3.Conninfo - Escaping = _psycopg3.Escaping + __impl__ = pq_cython.__impl__ + version = pq_cython.version + PGconn = pq_cython.PGconn + PGresult = pq_cython.PGresult + Conninfo = pq_cython.Conninfo + Escaping = pq_cython.Escaping return if not impl or impl == "ctypes": diff --git a/psycopg3/pq/pq_cython.pxd b/psycopg3/pq/pq_cython.pxd new file mode 100644 index 000000000..b48d88908 --- /dev/null +++ b/psycopg3/pq/pq_cython.pxd @@ -0,0 +1,22 @@ +from psycopg3.pq cimport libpq as impl + +ctypedef char *(*conn_bytes_f) (const impl.PGconn *) +ctypedef int(*conn_int_f) (const impl.PGconn *) + + +cdef class PGconn: + cdef impl.PGconn* pgconn_ptr + + @staticmethod + cdef PGconn _from_ptr(impl.PGconn *ptr) + + cdef int _ensure_pgconn(self) except 0 + cdef char *_call_bytes(self, conn_bytes_f func) except NULL + cdef int _call_int(self, conn_int_f func) except -1 + + +cdef class PGresult: + cdef impl.PGresult* pgresult_ptr + + @staticmethod + cdef PGresult _from_ptr(impl.PGresult *ptr) diff --git a/psycopg3/pq/pq_cython.pyx b/psycopg3/pq/pq_cython.pyx index ce8d420ad..937a3cc64 100644 --- a/psycopg3/pq/pq_cython.pyx +++ b/psycopg3/pq/pq_cython.pyx @@ -7,6 +7,7 @@ libpq Python wrapper using cython bindings. from cpython.mem cimport PyMem_Malloc, PyMem_Free from psycopg3.pq cimport libpq as impl +from psycopg3.pq.libpq cimport Oid from psycopg3.errors import OperationalError from psycopg3.pq.misc import error_message, ConninfoOption, PQerror @@ -27,13 +28,8 @@ __impl__ = 'c' def version(): return impl.PQlibVersion() -ctypedef unsigned int Oid; -ctypedef char *(*conn_bytes_f) (const impl.PGconn *) -ctypedef int(*conn_int_f) (const impl.PGconn *) cdef class PGconn: - cdef impl.PGconn* pgconn_ptr - @staticmethod cdef PGconn _from_ptr(impl.PGconn *ptr): cdef PGconn rv = PGconn.__new__(PGconn) @@ -526,8 +522,6 @@ cdef _options_from_array(impl.PQconninfoOption *opts): cdef class PGresult: - cdef impl.PGresult* pgresult_ptr - def __cinit__(self): self.pgresult_ptr = NULL diff --git a/psycopg3/transform.pyx b/psycopg3/transform.pyx index f59153b80..52eb9d4e8 100644 --- a/psycopg3/transform.pyx +++ b/psycopg3/transform.pyx @@ -8,9 +8,10 @@ import codecs from typing import Any, Dict, Iterable, List, Optional, Tuple from psycopg3.pq cimport libpq +from psycopg3.pq.pq_cython cimport PGresult from psycopg3 import errors as e -# from psycopg3.pq.enum import Format +from psycopg3.pq.enums import Format # from psycopg3.types.oids import builtins, INVALID_OID TEXT_OID = 25 @@ -159,7 +160,7 @@ cdef class Transformer: for i, (oid, fmt) in enumerate(types): loader = self._set_loader(i, oid, fmt) - cdef void _set_loader(self, int col, Oid oid, int fmt): + cdef void _set_loader(self, int col, libpq.Oid oid, int fmt): cdef RowLoader *loader = self._row_loaders + col pyloader = self.get_load_function(oid, fmt) diff --git a/setup.py b/setup.py index 7fa1549a5..084e9ba81 100644 --- a/setup.py +++ b/setup.py @@ -71,14 +71,20 @@ class our_build_ext(build_ext): # type: ignore includedir = out.stdout.strip().decode("utf8") - ext = Extension( + pgext = Extension( "psycopg3._psycopg3", ["psycopg3/_psycopg3.pyx"], libraries=["pq"], include_dirs=[includedir], ) + pqext = Extension( + "psycopg3.pq.pq_cython", + ["psycopg3/pq/pq_cython.pyx"], + libraries=["pq"], + include_dirs=[includedir], + ) self.distribution.ext_modules = cythonize( - [ext], + [pgext, pqext], language_level=3, # annotate=True, # enable to get an html view of the C module )