]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Keep pq_cython as an extension separate from the rest
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Mon, 4 May 2020 16:44:45 +0000 (04:44 +1200)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Mon, 4 May 2020 18:28:05 +0000 (06:28 +1200)
psycopg3/_psycopg3.pyx
psycopg3/pq/__init__.py
psycopg3/pq/pq_cython.pxd [new file with mode: 0644]
psycopg3/pq/pq_cython.pyx
psycopg3/transform.pyx
setup.py

index f35a0b7de8b24437177a5babc84b61843a3d3fc1..007af0a480f8faaf148c2952054c07d23d6a9cb9 100644 (file)
@@ -1,4 +1,3 @@
-include "pq/pq_cython.pyx"
 include "types/numeric.pyx"
 include "types/text.pyx"
 include "transform.pyx"
index 59eddcdab427d4ef6f647c8d49bc3de3ec929cc0..cbb90557a0411b1102e27d2968ed94bc3f4ad11b 100644 (file)
@@ -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 (file)
index 0000000..b48d889
--- /dev/null
@@ -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)
index ce8d420ad1f15dae9a83ef83d1efdfdc2bc76009..937a3cc64f706f6cf2a0f2bd961c1bfcf9d1561d 100644 (file)
@@ -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
 
index f59153b808cc57602ee168e3400140fc2fe9bc6e..52eb9d4e883e29cb8a1743498298b29faa350a49 100644 (file)
@@ -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)
index 7fa1549a5ac4c71d101590600553d18bf1e87221..084e9ba81754dea1176edd655432bccd4753ed37 100644 (file)
--- 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
         )