using stdint with swig and numpy.i

谁都会走 提交于 2019-12-22 10:57:17

问题


I'm developing a module for using c inline in Python code based on swig. For that I would like to make numpy arrays accessible in C. Until now I used C types like unsigned short but I would like to use types like uint16_t from stdint.h to be save whatever compiler my module encounters.

Unfortunately the c++-functions do not get wrapped correctly when using stdint.htypes. The Error given is: _setc() takes exactly 2 arguments (1 given). That means, the function is not wrapped to accept numpy arrays. The error does not occur, when I use e.g. unsigned short.

Do you have any ideas, how I can make swig map numpy arrays into stdint-types?

interface.i NOT working:

/* interface.i */
extern int __g();
%}
%include "stdint.i"
%include "numpy.i"

%init %{
import_array();
%}
%apply (uint16_t* INPLACE_ARRAY3, int DIM1) {(uint16_t* seq, int n1)};
extern int __g();

c++ function NOT working:

#include "Python.h"
#include <stdio.h>
#include <stdint.h>
extern uint16_t* c;
extern int Dc;
extern int Nc[4];
void _setc(uint16_t *seq, int n1, int n2, int n3)
{
    c = seq;
    Nc[0] = n1;
    Nc[1] = n2;
    Nc[2] = n3;
}

interface.i working:

/* interface.i */
extern int __g();
%}
%include "stdint.i"
%include "numpy.i"

%init %{
import_array();
%}
%apply (unsigned short* INPLACE_ARRAY3, int DIM1) {(unsigned short* seq, int n1)};
extern int __g();

c++ function working:

#include "Python.h"
#include <stdio.h>
#include <stdint.h>
extern unsigned short* c;
extern int Dc;
extern int Nc[4];
void _setc(unsigned short *seq, int n1, int n2, int n3)
{
    c = seq;
    Nc[0] = n1;
    Nc[1] = n2;
    Nc[2] = n3;
}

回答1:


Haha, I found some "solution" just a few minutes after I gave up and posted this question.

I edited the numpy.ito fit my cause: I substituted the old C types with stdint.h types in lines 3044 ff:

[..]
/* Concrete instances of the %numpy_typemaps() macro: Each invocation
 * below applies all of the typemaps above to the specified data type.
 */
%numpy_typemaps(int8_t       , NPY_BYTE     , int)
%numpy_typemaps(uint8_t     , NPY_UBYTE    , int)
%numpy_typemaps(int16_t             , NPY_SHORT    , int)
%numpy_typemaps(uint16_t    , NPY_USHORT   , int)
%numpy_typemaps(int32_t               , NPY_INT      , int)
%numpy_typemaps(uint32_t      , NPY_UINT     , int)
%numpy_typemaps(long              , NPY_LONG     , int)
%numpy_typemaps(unsigned long     , NPY_ULONG    , int)
%numpy_typemaps(int64_t         , NPY_LONGLONG , int)
%numpy_typemaps(uint64_t, NPY_ULONGLONG, int)
%numpy_typemaps(float             , NPY_FLOAT    , int)
%numpy_typemaps(double            , NPY_DOUBLE   , int)
[..]

I wonder if anyone has a better idea than editing the numpy.i

Cheers Jochen



来源:https://stackoverflow.com/questions/34315791/using-stdint-with-swig-and-numpy-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!