From: Jörg Breitbart Date: Fri, 12 Sep 2025 08:28:38 +0000 (+0200) Subject: cleanup code X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=b65a869550d0ec6cf47aac4c41c506596579819a;p=thirdparty%2Fpsycopg.git cleanup code --- diff --git a/psycopg_c/psycopg_c/_psycopg/transform.pyx b/psycopg_c/psycopg_c/_psycopg/transform.pyx index c5d7df731..0be56b7f2 100644 --- a/psycopg_c/psycopg_c/_psycopg/transform.pyx +++ b/psycopg_c/psycopg_c/_psycopg/transform.pyx @@ -25,19 +25,6 @@ from psycopg.pq import Format as PqFormat from psycopg.rows import Row, RowMaker from psycopg._encodings import conn_encoding - -cdef extern from *: - """ - #ifdef _WIN32 - #include - #else - #include - #endif - """ - void* alloca(size_t size) - -ctypedef object (*cload_ptr)(CLoader, const char *, size_t) - NoneType = type(None) # internal structure: you are not supposed to know this. But it's worth some @@ -456,8 +443,7 @@ cdef class Transformer: cdef PGresAttValue *attval cdef object record # not 'tuple' as it would check on assignment - cdef int rowcount = row1 - row0 - cdef object records = PyList_New(rowcount) + cdef object records = PyList_New(row1 - row0) cdef PyObject *loader # borrowed RowLoader cdef PyObject *brecord # borrowed @@ -503,7 +489,7 @@ cdef class Transformer: PyTuple_SET_ITEM(brecord, col, pyval) if make_row is not tuple: - for i in range(rowcount): + for i in range(row1 - row0): brecord = PyList_GET_ITEM(records, i) record = PyObject_CallFunctionObjArgs( make_row, brecord, NULL) @@ -512,76 +498,6 @@ cdef class Transformer: Py_DECREF(brecord) return records - def load_rows_(self, int row0, int row1, object make_row) -> list[Row]: - if self._pgresult is None: - raise e.InterfaceError("result not set") - - if not (0 <= row0 <= self._ntuples and 0 <= row1 <= self._ntuples): - raise e.InterfaceError( - f"rows must be included between 0 and {self._ntuples}" - ) - - cdef libpq.PGresult *res = self._pgresult._pgresult_ptr - # cheeky access to the internal PGresult structure - cdef pg_result_int *ires = res - - cdef int row - cdef int col - cdef PGresAttValue *attval - cdef object record # not 'tuple' as it would check on assignment - - cdef object records = PyList_New(row1 - row0) - - # hacky but fastest access to loaders - #cdef int32_t *has_cloader = alloca(self._nfields * sizeof(int32_t)) - cdef cload_ptr *cloads = alloca(self._nfields * sizeof(cload_ptr)) - cdef PyObject **loadfuncs = alloca(self._nfields * sizeof(PyObject *)) - cdef PyObject *loader - for col in range(self._nfields): - #loaders[col] = PyList_GET_ITEM(self._row_loaders, col) - loader = PyList_GET_ITEM(self._row_loaders, col) - if (loader).cloader is not None: - #has_cloader[col] = 1 - cloads[col] = (loader).cloader.cload - else: - #has_cloader[col] = 0 - cloads[col] = NULL - loadfuncs[col] = (loader).loadfunc - - for row in range(row0, row1): - record = PyTuple_New(self._nfields) - for col in range(self._nfields): - attval = &(ires.tuples[row][col]) - if attval.len == -1: # NULL_LEN - pyval = None - else: - if cloads[col]: - #if has_cloader[col]: - #if (loaders[col]).cloader is not None: - #pyval = (loaders[col]).cloader.cload( - # attval.value, attval.len) - pyval = cloads[col](None, attval.value, attval.len) - else: - #loader = PyList_GET_ITEM(self._row_loaders, col) - b = PyMemoryView_FromObject( - ViewBuffer._from_buffer( - self._pgresult, - attval.value, - attval.len - )) - #pyval = PyObject_CallFunctionObjArgs( - # (loader).loadfunc, b, NULL) - pyval = PyObject_CallFunctionObjArgs( - loadfuncs[col], b, NULL) - Py_INCREF(pyval) - PyTuple_SET_ITEM(record, col, pyval) - if make_row is not tuple: - record = PyObject_CallFunctionObjArgs( - make_row, record, NULL) - Py_INCREF(record) - PyList_SET_ITEM(records, row - row0, record) - return records - def load_row(self, int row, object make_row) -> Row: if self._pgresult is None: raise e.InterfaceError("result not set")