]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
fix(c): allow set_loader_types to be any sequence
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Mon, 27 Oct 2025 15:16:29 +0000 (15:16 +0000)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Thu, 30 Oct 2025 12:28:54 +0000 (12:28 +0000)
psycopg_c/psycopg_c/_psycopg/transform.pyx

index 21a41cde7c9df908ab64de420d5b353ae4767ee5..b1f530d4f765ce590e888ff5afe7f8e593b462f7 100644 (file)
@@ -15,6 +15,8 @@ from cpython.list cimport PyList_GET_ITEM, PyList_GET_SIZE, PyList_New, PyList_S
 from cpython.bytes cimport PyBytes_AS_STRING
 from cpython.tuple cimport PyTuple_New, PyTuple_SET_ITEM
 from cpython.object cimport PyObject, PyObject_CallFunctionObjArgs
+from cpython.sequence cimport PySequence_Fast, PySequence_Fast_GET_ITEM
+from cpython.sequence cimport PySequence_Fast_GET_SIZE
 
 from typing import Sequence
 
@@ -185,16 +187,15 @@ cdef class Transformer:
         self.formats = [format] * ntypes
 
     def set_loader_types(self, types: Sequence[int], format: PqFormat) -> None:
-        self._c_loader_types(len(types), types, format)
-
-    cdef void _c_loader_types(self, Py_ssize_t ntypes, list types, object format):
+        cdef types_fast = PySequence_Fast(types, "'types' is not a valid sequence")
+        cdef Py_ssize_t ntypes = PySequence_Fast_GET_SIZE(types_fast)
         cdef list loaders = PyList_New(ntypes)
 
         # these are used more as Python object than C
         cdef PyObject *oid
         cdef PyObject *row_loader
         for i in range(ntypes):
-            oid = PyList_GET_ITEM(types, i)
+            oid = PySequence_Fast_GET_ITEM(types_fast, i)
             row_loader = self._c_get_loader(oid, <PyObject *>format)
             Py_INCREF(<object>row_loader)
             PyList_SET_ITEM(loaders, i, <object>row_loader)