Swig [C-> Python]:如何处理原始类型的输入或输出函数参数?

时间:2013-03-01 01:24:51

标签: python c swig

例如,我有

my_types.h

typedef uint16_t my_type_1;
typedef uint8_t my_type_2;

my_types.c

int my_func(my_type_1 * arg1, my_type_2 * arg2, my_type_3 *arg3)
{
    *args3 = *arg1 + *arg2;
    return 0;
}

如何定义接口文件以将其包装为python?

1 个答案:

答案 0 :(得分:0)

假设你在my_types.h中声明了my_func,那么你的界面就可以变成:

%module test

%{
#include "my_types.h"
%}

%include <stdint.i>

%include "my_types.h"

%apply uint16_t *INPUT { my_type_1 * arg1 };
%apply uint8_t *INPUT { my_type_2 * arg2 };
%apply uint16_t *OUTPUT { my_type_1 *arg3 };

int my_func(my_type_1 * arg1, my_type_2 * arg2, my_type_1 *arg3);

这足以让您按如下方式使用它:

import test

print test.my_func(100, 50)[1]

你的“真实”回归是元组中的位置0,第一个OUTPUT位于第1位。