gpt4 book ai didi

python - SWIG:如何使用 %apply 返回结构? 'No typemaps defined' 警告

转载 作者:太空宇宙 更新时间:2023-11-04 02:34:57 25 4
gpt4 key购买 nike

我目前有一个函数使用结构作为缓冲区来返回一些信息,如下所示:

int example_reader(int code, void* return_struct);

我的目标是,当我使用 SWIG 包装此函数以便它可以在 Python 中使用时,我将返回结构以及函数的常规返回值。到目前为止,我一直在使用 %apply 命令这样做:

%apply struct ret_struct *OUTPUT {void* return_struct};

但是,当我将以上行添加到我的 .i 文件并尝试运行 SWIG 时,我收到以下警告:

“警告 453:无法应用(struct ret_struct *OUTPUT。未定义类型映射”

我相信我包含了定义我试图返回的结构的 .h 文件,所以我无法查明问题所在。如果问题似乎涉及结构的不当包含,请纠正我。我已经尝试通读 SWIG 文档以及其他 Stack Overflow 帖子,以了解问题可能出在哪里,但到目前为止我还没有弄清楚。这个问题变得有点棘手,因为我试图返回一个指向结构的空指针,而我试图包装的代码可能有多种结构供我返回。处理此结构返回的明智方法是什么?谢谢!

最佳答案

我在这里给出了一个完整的 C 示例,其中使用接口(interface)将结构连同返回值一起返回到目标语言。通过这种方式,您可以创建一个适当的接口(interface),标题中没有给出实现。那不是虚拟析构函数的默认实现。如果不想使用接口(interface),可以让 SWIG 和 Python 知道数据的表示方式。

接口(interface)头文件:foo.h

typedef struct _Foo Foo;

int foo_new(Foo **obj);
int foo_free(Foo *obj);

int foo_get_value_a(Foo *obj, int *result);
int foo_set_value_a(Foo *obj, int value);

int foo_get_value_b(Foo *obj, char **result);
int foo_set_value_b(Foo *obj, char *value);

SWIG 接口(interface):foo.i

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

%include "typemaps.i"

%typemap(in, numinputs=0) Foo ** (Foo *temp) {
$1 = &temp;
}

%typemap(argout) Foo ** {
PyObject* temp = NULL;
if (!PyList_Check($result)) {
temp = $result;
$result = PyList_New(1);
PyList_SetItem($result, 0, temp);
}
temp = SWIG_NewPointerObj(*$1, SWIGTYPE_p__Foo, SWIG_POINTER_NEW);
PyList_Append($result, temp);
Py_DECREF(temp);
}

%delobject foo_free; // Protect for double deletion

struct _Foo {};
%extend _Foo {
~_Foo() {
foo_free($self);
}
};
%ignore _Foo;

接口(interface)的一些实现:foo.c

%include "foo.h"

#include "foo.h"
#include "stdlib.h"
#include "string.h"

struct FooImpl {
char* c;
int i;
};

int foo_new(Foo **obj)
{
struct FooImpl* f = (struct FooImpl*) malloc(sizeof(struct FooImpl));
f->c = NULL;
*obj = (Foo*) f;
return 0;
}
int foo_free(Foo *obj)
{
struct FooImpl* impl = (struct FooImpl*) obj;
if (impl) {
if (impl->c) {
free(impl->c);
impl->c = NULL;
}
}
return 0;
}

int foo_get_value_a(Foo *obj, int *result)
{
struct FooImpl* impl = (struct FooImpl*) obj;
*result = impl->i;
return 0;
}
int foo_set_value_a(Foo *obj, int value)
{
struct FooImpl* impl = (struct FooImpl*) obj;
impl->i = value;
return 0;
}

int foo_get_value_b(Foo *obj, char **result)
{
struct FooImpl* impl = (struct FooImpl*) obj;
*result = impl->c;
return 0;
}
int foo_set_value_b(Foo *obj, char *value)
{
struct FooImpl* impl = (struct FooImpl*) obj;
int len = strlen(value);
if (impl->c) {
free(impl->c);
}
impl->c = (char*)malloc(len+1);
strcpy(impl->c,value);
return 0;
}

构建脚本

#!/usr/bin/env python
from distutils.core import setup, Extension
import os

os.environ['CC'] = 'gcc';
setup(name='foo',
version='1.0',
ext_modules =[Extension('_foo',
['foo.i','foo.c'])])

用法:

import foo

OK, f = foo.foo_new()
OK = foo.foo_set_value_b(f, 'Hello world!')
OK = foo.foo_free(f)

OK, f = foo.foo_new()
# Test safe to double delete
del f

关于python - SWIG:如何使用 %apply 返回结构? 'No typemaps defined' 警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38839828/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com