Long int * to np_intp * platform dependent conversion

On my 64-bit office desktop, this compiles fine:

#include <Python.h>
#include <numpy/arrayobject.h>

...

Py_Initialize();
import_array();

// Build array object
long int NUMEL=3;
PyObject *out_array = PyArray_SimpleNew(1, &NUMEL, NPY_DOUBLE);

Conversely, on my 32-bit laptop this does not result in an error:

 error: invalid conversion from ‘long int*’ to ‘npy_intp* {aka int*}’ [-fpermissive]
     PyArray_New(&PyArray_Type, nd, dims, typenum, NULL, NULL, 0, 0, NULL)

Alternatively, if I declare int NUMEL=3instead, the code will compile on a 32-bit machine, but not on a 64-bit one. I suspect it npy_intpis platform dependent. Since I cannot determine the type of the NUMELtype npy_intp(since in practice it is passed by other C / C ++ routines), is there a way to conditionally determine NUMEL, depending on the platform, inside C ++ code?

+4
source share
1 answer

npy_intp typedef 'ed :

typedef Py_intptr_t npy_intp;

Py_intptr_t Python C pyport.h. , . , , Microsoft Visual C, C89, stdint.h, intptr_t . ++ std::ptrdiff_t, , std::size_t.

, Python - :

#if sizeof(void *) <= sizeof(int)
    typedef int my_type;
#elif sizeof(void *) <= sizeof(long)
    typedef long my_type;
#else
    typedef long long my_type;
#endif

, , .

+2

All Articles