将 stdint 与 swig 和 numpy.i 一起使用
using stdint with swig and numpy.i
我正在基于 swig
开发一个在 Python 代码中使用 c inline
的模块。
为此,我想让 numpy
数组在 C
中可访问。到目前为止,我使用的是 unsigned short
这样的 C 类型,但我想使用 stdint.h
中的 uint16_t
这样的类型来保存我的模块遇到的任何编译器。
不幸的是,c++
-函数在使用 stdint.h
类型时没有正确包装。给出的错误是:_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;
}
哈哈,在我放弃发布这个问题的几分钟后,我发现了一些"solution"
我编辑了 numpy.i
以适合我的事业:
我在第 3044 行 ff:
中用 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
更好的主意
干杯
约臣
我正在基于 swig
开发一个在 Python 代码中使用 c inline
的模块。
为此,我想让 numpy
数组在 C
中可访问。到目前为止,我使用的是 unsigned short
这样的 C 类型,但我想使用 stdint.h
中的 uint16_t
这样的类型来保存我的模块遇到的任何编译器。
不幸的是,c++
-函数在使用 stdint.h
类型时没有正确包装。给出的错误是:_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;
}
哈哈,在我放弃发布这个问题的几分钟后,我发现了一些"solution"
我编辑了 numpy.i
以适合我的事业:
我在第 3044 行 ff:
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
干杯 约臣