From 0093a6aa0febf777fe679c0dc8b55cf295b175a5 Mon Sep 17 00:00:00 2001 From: Guilhem Lavaux Date: Wed, 6 Dec 2023 09:28:36 +0100 Subject: [PATCH] Fix cython syntax and port to more recent numpy.pxd --- python/_cosmomath.pyx | 2 +- python/_cosmotool.pyx | 12 ++++++++++-- python/_project.pyx | 2 +- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/python/_cosmomath.pyx b/python/_cosmomath.pyx index e5ab880..fc4b3b6 100644 --- a/python/_cosmomath.pyx +++ b/python/_cosmomath.pyx @@ -12,7 +12,7 @@ cdef extern from "numpy/npy_common.h": ctypedef npy_intp cdef extern from "special_math.hpp" namespace "CosmoTool": - T log_modified_bessel_first_kind[T](T v, T z) nogil except + + T log_modified_bessel_first_kind[T](T v, T z) except + nogil cdef extern from "numpy_adaptors.hpp" namespace "CosmoTool": void parallel_ufunc_dd_d[T,IT](char **args, IT* dimensions, IT* steps, void *func) diff --git a/python/_cosmotool.pyx b/python/_cosmotool.pyx index b2494bd..7f2bc76 100644 --- a/python/_cosmotool.pyx +++ b/python/_cosmotool.pyx @@ -39,7 +39,7 @@ cdef extern from "loadSimu.hpp" namespace "CosmoTool": cdef extern from "loadGadget.hpp" namespace "CosmoTool": - SimuData *loadGadgetMulti(const char *fname, int id, int flags, int gformat) nogil except + + SimuData *loadGadgetMulti(const char *fname, int id, int flags, int gformat) except + nogil void cxx_writeGadget "CosmoTool::writeGadget" (const char * s, SimuData *data) except + cdef extern from "safe_gadget.hpp": @@ -313,6 +313,13 @@ tries to get an array from the object.""" references to the object are gone. """ pass + +cdef extern from "numpy/arrayobject.h": + # a little bit awkward: the reference to obj will be stolen + # using PyObject* to signal that Cython cannot handle it automatically + int PyArray_SetBaseObject(np.ndarray arr, PyObject *obj) except -1 # -1 means there was an error + + cdef object wrap_array(void *p, np.uint64_t s, int typ): cdef np.ndarray ndarray cdef ArrayWrapper wrapper @@ -320,7 +327,8 @@ cdef object wrap_array(void *p, np.uint64_t s, int typ): wrapper = ArrayWrapper() wrapper.set_data(s, typ, p) ndarray = np.array(wrapper, copy=False) - ndarray.base = wrapper + #ndarray.base = wrapper + PyArray_SetBaseObject(ndarray, wrapper) Py_INCREF(wrapper) return ndarray diff --git a/python/_project.pyx b/python/_project.pyx index bd64667..726ed0c 100644 --- a/python/_project.pyx +++ b/python/_project.pyx @@ -727,7 +727,7 @@ cdef DTYPE_t C_line_of_sight_projection(DTYPE_t[:,:,:] density, cdef int iu0[3] cdef int i cdef int N = density.shape[0] - cdef int half_N = density.shape[0]/2 + cdef int half_N = density.shape[0]//2 cdef int completed cdef DTYPE_t I0, d, dist2, delta, s, max_distance2 cdef int jumper[1]