SWIG / Python - 传递typedef值的数组

时间:2017-04-11 14:30:14

标签: python swig

我试图用这个签名包装一个C函数:

typedef unsigned short UINT16;
void print_uint16_array(UINT16 x[3]);

http://www.swig.org/Doc2.0/SWIGDocumentation.html#Library_carrays

的示例开始

我让它适用于示例(双)和非typedef' d unsigned short的情况,但是当我尝试使用UINT16时,Python抱怨说我通过了错误的参数类型(TypeError: in method 'print_uint16_array', argument 1 of type 'UINT16 [3]')。

这就是我现在所拥有的,知道我做错了什么?

print_array.c

#include <stdio.h>

void print_ushort_array(unsigned short x[3]) {
   int i;
   for (i = 0; i < 3; i++) { printf("[%d] = %d\n", i, x[i]); }
}

typedef unsigned short UINT16;
void print_uint16_array(UINT16 x[3]) {
   int i;
   for (i = 0; i < 3; i++) { printf("[%d] = %d\n", i, x[i]); }
}

print_array.i

%module print_array
%include "carrays.i"

%{
typedef unsigned short UINT16;

void print_ushort_array(unsigned short x[3]);
void print_uint16_array(UINT16 x[3]);
%}

typedef unsigned short UINT16;

%array_class(unsigned short, ushortArray);
%array_class(UINT16        , uint16Array);


void print_ushort_array(unsigned short x[3]);
void print_uint16_array(UINT16 x[3]);

test.py

from print_array import *
ushort_array = ushortArray(3)
uint16_array = uint16Array(3)

print_ushort_array(ushort_array)
print_uint16_array(uint16_array)

构建/测试:

$ swig -python print_array.i
$ gcc -shared print_array*.c -o _print_array.pyd -I /c/Miniconda/include -L /c/Miniconda -lpython27

    $ /c/Miniconda/python test.py
[0] = 0
[1] = 0
[2] = 0
Traceback (most recent call last):
  File "test.py", line 8, in <module>
    print_uint16_array(uint16_array)
TypeError: in method 'print_uint16_array', argument 1 of type 'UINT16 [3]'

0 个答案:

没有答案