使用 SWIG 用指针参数包装 C 函数 [英] Wrapping C function with pointer arguments using SWIG

查看:55
本文介绍了使用 SWIG 用指针参数包装 C 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 SWIG 包装现有的 C 库以在 Python 中使用.我正在使用 Python 2.7.4 在 Windows XP 上运行 swig 2.0.10.我遇到的问题是我无法调用包装的 C 函数,该函数具有指向 int 的指针作为参数,该函数结果将存储在该参数中.我已将问题提炼为以下示例代码:

convert.c 中的 C 函数:

#include #include "convert.h"#include int convert(char *s, int *i){*i = atoi(s);返回0;}

convert.h 中的头文件

#ifndef _convert_h_#define _convert_h_int convert(char *, int *);#万一

convert.i 中的 swig 接口文件

/* 文件:convert.i */%模块转换%{#include "convert.h"%}%include "convert.h"

所有这些都使用 Visual C++ 2010 构建到 .pyd 文件中.构建完成后,我在构建目录中留下了两个文件:convert.py 和 _convert.pyd.我在这个目录中打开一个命令窗口并启动 python 会话并输入以下内容:

Python 2.7.4(默认,2013 年 4 月 6 日,19:54:46)[MSC v.1500 32 位(英特尔)] on win32输入帮助"、版权"、信用"或许可"以获取更多信息.>>>从 ctypes 导入 *>>>导入转换>>>目录(转换)['__builtins__', '__doc__', '__file__', '__name__', '__package__', '_convert', '_newclass', '_object', '_swig_getattr', '_swig_property', '_swig_repr', '_swig_setattr'_swig_setattr_nondynamic', '转换']>>>i = c_int()>>>一世c_long(0)>>>convert.convert('1234', byref(i))回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中类型错误:在方法convert"中,int *"类型的参数 2

为什么我的指针对象被拒绝了?我应该怎么做才能完成这项工作?

解决方案

SWIGctypes 是不同的库,所以你不能直接将 ctypes 对象传递给 SWIG-包装函数.

在 SWIG 中,%apply 命令可以将类型映射应用于常见参数类型,以将它们配置为 INPUTINOUTOUTPUT 参数.请尝试以下操作:

%module 转换%{#include "convert.h"%}%apply int *OUTPUT {int*};%include "convert.h"

Python 将不再需要输入参数,并将函数的输出更改为返回值和任何 INOUTOUTPUT 参数的元组:

<预><代码>>>>导入转换>>>convert.convert('123')[0, 123]

请注意,超出 POD(纯旧数据)类型的参数通常需要编写您自己的类型映射.有关详细信息,请参阅 SWIG 文档.

I'm trying to use SWIG to wrap an existing C library for use in Python. I'm running swig 2.0.10 on Windows XP with Python 2.7.4. The problem I'm encountering is that I'm unable to call a wrapped C function that has a pointer to an int as an argument which is where the function result is to be stored. I've distilled the problem into the follow example code:

The C function in convert.c:

#include <stdio.h>
#include "convert.h"
#include <stdlib.h>

int convert(char *s, int *i)
{
   *i = atoi(s);
   return 0;
} 

The header file in convert.h

#ifndef _convert_h_
#define _convert_h_

int convert(char *, int *);

#endif

The swig interface file in convert.i

/* File : convert.i */
%module convert
%{
#include "convert.h"
%}

%include "convert.h"

All of this is being built into a .pyd file using Visual C++ 2010. When the build is complete, I'm left with two files: convert.py and _convert.pyd in the build directory. I open a command window in this directory and start python session and enter the following:

Python 2.7.4 (default, Apr  6 2013, 19:54:46) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from ctypes import *
>>> import convert
>>> dir(convert)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', '_convert', '_newclass', '_object', '_swig_getattr', '_swig_property', '_swig_repr', '_swig_setattr', '_swig_setattr_nondynamic', 'convert']
>>> i = c_int()
>>> i
c_long(0)
>>> convert.convert('1234', byref(i))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: in method 'convert', argument 2 of type 'int *'

Why is my pointer object being rejected? What should I do to make this work?

解决方案

SWIG and ctypes are different libraries, so you can't pass ctypes objects directly to SWIG-wrapped functions.

In SWIG, the %apply command can apply typemaps to common parameter types to configure them as INPUT, INOUT, or OUTPUT parameters. Try the following:

%module convert
%{
#include "convert.h"
%}

%apply int *OUTPUT {int*};
%include "convert.h"

Python will no longer require the parameter on input, and will change the output of the function to be a tuple of the return value and any INOUT or OUTPUT parameters:

>>> import convert
>>> convert.convert('123')
[0, 123]

Note that parameters beyond the POD (plain old data) types usually require writing your own typemaps. Consult the SWIG Documentation for more details.

这篇关于使用 SWIG 用指针参数包装 C 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆