使用SWIG和NumPy.i时应如何使用stdint。

3
我正在开发一个基于swig的Python模块,用于使用c inline。为此,我想使C中的numpy数组可访问。到目前为止,我使用了C类型,如unsigned short,但我想使用stdint.h中的uint16_t等类型,以便无论我的模块遇到什么编译器时都能保持一致。

不幸的是,在使用stdint.h类型时,c++函数没有正确包装。给出的错误是:_setc() takes exactly 2 arguments (1 given)。这意味着,这个函数没有被包装成能够接受numpy数组的形式。当我使用例如unsigned short时,就不会出现上述错误。

你有任何想法,如何让swig将numpy数组映射到stdint-types中吗?

interface.i 不起作用:

/* 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++函数无法正常工作:

#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 的工作原理:

/* 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++函数工作原理:

#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个回答

1

哈哈,我在放弃并发布这个问题后几分钟内找到了一些“解决方案”。

我编辑了numpy.i以适应我的需求: 我在3044行及以下用stdint.h类型替换了旧的C类型:

[..]
/* 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)
[..]

我想知道是否有比编辑 numpy.i 更好的方法。

谢谢 Jochen


哇+1。我认为正确的解决方案是提交一个错误报告,但我不知道它是否会被注意到。最终我使用了以下代码:%apply (unsigned char *IN_ARRAY1, int DIM1) {(uint8_t * buff_ptr, int buff_size)} - hmaarrfk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接