From 92034d3d2f409da4b704fa004137b63e0741821c Mon Sep 17 00:00:00 2001 From: "J.A. de Jong - ASCEE" Date: Tue, 7 Apr 2020 21:51:30 +0200 Subject: [PATCH] Running on Windows. WOOHOO! --- lasp/c/lasp_python.h | 22 ++++++++++++++++++++++ lasp/wrappers.pyx | 12 +++++++++--- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/lasp/c/lasp_python.h b/lasp/c/lasp_python.h index 5b51cac..4ce31d6 100644 --- a/lasp/c/lasp_python.h +++ b/lasp/c/lasp_python.h @@ -17,6 +17,17 @@ #define LASP_NUMPY_FLOAT_TYPE NPY_FLOAT32 #endif +#ifdef MS_WIN64 +/** + * Function passed to Python to use for cleanup of + * foreignly obtained data. + **/ +static inline void capsule_cleanup(void* capsule) { + void *memory = PyCapsule_GetPointer(capsule, NULL); + free(memory); + } + +#endif /** * Create a numpy array from an existing dmat. * @@ -47,7 +58,18 @@ static inline PyObject* dmat_to_ndarray(dmat* mat,bool transfer_ownership) { if(transfer_ownership) { mat->_foreign_data = true; +#ifdef MS_WIN64 + // The default destructor of Python cannot free the data, as it is allocated + // with malloc. Therefore, with this code, we tell Numpy/Python to use + // the capsule_cleanup constructor. See: + // https://stackoverflow.com/questions/54269956/crash-of-jupyter-due-to-the-use-of-pyarray-enableflags/54278170#54278170 + // Note that in general it was disadvised to build all C code with MinGW on Windows. + // We do it anyway, see if we find any problems on the way. + void* capsule = PyCapsule_New(mat->_data, NULL, capsule_cleanup); + PyArray_SetBaseObject( arr_t, capsule); +#else PyArray_ENABLEFLAGS(arr_t, NPY_OWNDATA); +#endif } // Transpose the array diff --git a/lasp/wrappers.pyx b/lasp/wrappers.pyx index 4e0acd2..6d89325 100644 --- a/lasp/wrappers.pyx +++ b/lasp/wrappers.pyx @@ -2,6 +2,12 @@ This file contains the Cython wrapper functions to C implementations. """ include "config.pxi" +from libc.stdio cimport printf +from numpy cimport import_array + +import_array() + + IF LASP_FLOAT == "double": ctypedef double d ctypedef double complex c @@ -456,13 +462,13 @@ cdef class SosFilterBank: cdef dmat output with nogil: output = Sosfilterbank_filter(self.fb,&input_vd) - + #printf('Came back from filter\n') # Steal the pointer from output result = dmat_to_ndarray(&output,True) - + #printf('Converted to array\n') dmat_free(&output) vd_free(&input_vd) - + #printf('Ready to return\n') return result cdef extern from "lasp_decimation.h":