如何在SWIG/Python中将结构列表传递给C 我有一个C++类,我通过SWIG导出,并且使用一个函数,该数组使用了 FooS: typedef class Foo { int i; } Foo; void func(Foo *all_foos);
现在,我希望能够将包含这些内容的python列表传递到所有\u foos:如何在SWIG/Python中将结构列表传递给C 我有一个C++类,我通过SWIG导出,并且使用一个函数,该数组使用了 FooS: typedef class Foo { int i; } Foo; void func(Foo *all_foos);,python,swig,Python,Swig,现在,我希望能够将包含这些内容的python列表传递到所有\u foos: afoo = mymod.Foo() bfoo = mymod.Foo() mymod.func([afoo, bfoo]) 我有一张不起作用的打字图。请参阅FIXME热线,了解我需要帮助的地方 %typemap(in) Foo ** { /* Check if it's a list */ if (PyList_Check($input)) { int size = PyList_Size($inpu
afoo = mymod.Foo()
bfoo = mymod.Foo()
mymod.func([afoo, bfoo])
我有一张不起作用的打字图。请参阅FIXME热线,了解我需要帮助的地方
%typemap(in) Foo ** {
/* Check if it's a list */
if (PyList_Check($input)) {
int size = PyList_Size($input);
int i = 0;
$1 = (Foo **) malloc((size+1)*sizeof(Foo *));
for (i = 0; i < size; i++) {
PyObject *o = PyList_GetItem($input,i);
// here o->ob_type->tp_name is "Foo"; could check that
// FIXME: HOW DO I GO FROM o -> SwigPyObject -> Foo *? THIS IS WRONG
$1[i] = (Foo *)(reinterpret_cast<SwigPyObject *>(o))->ptr;
}
} else {
PyErr_SetString(PyExc_TypeError,"not a list");
return NULL;
}
}
%typemap(in)Foo**{
/*检查它是否是一个列表*/
if(PyList_检查($input)){
int size=PyList_size($input);
int i=0;
$1=(Foo**)malloc((尺寸+1)*sizeof(Foo*);
对于(i=0;iob\u type->tp\u名称是“Foo”;可以检查一下
//修正我:如何从o->SwigPyObject->Foo*?这是错误的
$1[i]=(Foo*)(重新解释演员阵容(o))->ptr;
}
}否则{
PyErr_SetString(PyExc_TypeError,“非列表”);
返回NULL;
}
}
基本上,我有一个PyObject
o;我需要从它那里获取SwigPyObject
(我是要强制转换它吗?还是它是一个成员?),然后以某种方式从SwigPyObject
中获取我的Foo
指针。我会尝试,比如
%include carrays.i
%array_class(Foo, FooArray)
void func(Foo *all_foos);
然后在python中:
fooArray = FooArray(10)
func(fooArray)
只有在没有其他SWIG工具实现您想要的API时,才应该使用Typemaps。我认为它甚至可能做得比上面更好,使用%inline(单独的答案,因为与此非常不同) > P>你的例子有点困惑,因为你的C++函数采用了<代码> Foo*<代码>,但是你的类型映射采用了<代码> Fo**<代码>(即FoOS数组和指针到FOS数组)。我假设你指的是后者,因为这是唯一合理的方法来判断数组距离你给出的函数声明有多长
关于“如何将Python对象转换为给定类型的C++指针”的直接问题?“我通常通过让SWIG为我生成一些代码然后检查它来解决这个问题。例如,如果你有一个函数
voidbar(Foo*)代码>然后SWIG将在包装器中生成一些代码:
SWIGINTERN PyObject *_wrap_bar(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {
PyObject *resultobj = 0;
Foo *arg1 = (Foo *) 0 ;
void *argp1 = 0 ;
int res1 = 0 ;
PyObject * obj0 = 0 ;
if (!PyArg_ParseTuple(args,(char *)"O:bar",&obj0)) SWIG_fail;
res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_Foo, 0 | 0 );
if (!SWIG_IsOK(res1)) {
SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "bar" "', argument " "1"" of type '" "Foo *""'");
}
arg1 = reinterpret_cast< Foo * >(argp1);
bar(arg1);
resultobj = SWIG_Py_Void();
return resultobj;
fail:
return NULL;
}
请注意,为了使typemap具有通用性和可重用性,我将其部分替换为-生成的代码与我们看到的单个示例相同,但您可以稍微重用它
这就足够编译和运行您给出的示例代码了(注意到有一个更改),但仍然存在内存泄漏。您调用了malloc()
,但从未调用过free()
,因此我们需要添加相应的:
这在成功和出错时都会被调用,但这很好,因为$1
被初始化为NULL,因此无论我们是否成功malloc
,行为都是正确的
作为C++的一般点,我认为你的接口设计错误了,没有充分的理由不使用<代码> STD::矢量< /代码>,<代码> STD::列表< /C> >或类似,这也使得包裹更简单。像在头文件中一样使用typedef也是一种奇怪的风格
如果是我写的,我会在包装器中使用RAII,即使我不能将接口更改为使用容器。这就意味着我要把我的“输入”类型图写成:
%typemap(in) Foo ** (std::vector<Foo*> temp) {
if (PyList_Check($input)) {
const size_t size = PyList_Size($input);
temp.resize(size+1);
for (int i = 0; i < size; ++i) {
void *argp = 0 ;
const int res = SWIG_ConvertPtr(PyList_GetItem($input, i), &argp, $*1_descriptor, 0);
if (!SWIG_IsOK(res)) {
SWIG_exception_fail(SWIG_ArgError(res), "in method '" "$symname" "', argument " "$argnum"" of type '" "$1_type""'");
}
temp[i] = reinterpret_cast<Foo*>(argp);
}
temp[size] = NULL;
$1 = &temp[0]; // Always valid since we +1
}
else {
// Raise exception
SWIG_exception_fail(SWIG_TypeError, "Expected list in $symname");
}
}
%typemap(in)Foo**(std::vector temp){
if(PyList_检查($input)){
常量大小=PyList大小($input);
临时调整大小(大小+1);
对于(int i=0;i
这样就不需要“freearg”类型映射,而且永远不会泄漏
如果出于某种原因您不想编写自定义类型映射,或者将接口更改为使用在SWIG库中已经具有良好类型映射的类型。您仍然可以通过使用%rename
来“隐藏”默认实现,并使用%pythoncode
来注入与“按摩”同名的其他Python,为Python用户提供直观的Python接口Python用户可以透明地将Python输入到carrays界面。您应该能够使用标准的“前端”SWIG工具完成所有操作:
%include <std_list.i>
%ignore func
%rename(func) funcWrap
namespace std {
%template(FooList) std::list<Foo*>;
}
%include "Foo.h"
%inline %{
// wrap with const list of non-const Foo*
void funcWrap(const std::list<Foo *>& all_foos)
{
// create Foo* array:
Foo* fooArray = new Foo(all_foos.size());
int count = 0;
for (std::list<Foo *>::const_iterator ii=all_foos.begin(); ...)
fooArray[count] = *ii;
func(fooArray);
delete fooArray;
}
%}
%include
%忽略函数
%重命名(func)funcWrap
名称空间标准{
%模板(傻瓜)标准::列表;
}
%包括“Foo.h”
%内联%{
//用非常量Foo的常量列表换行*
void funcWrap(const std::list&all_foos)
{
//创建Foo*数组:
Foo*fooArray=newfoo(all_foos.size());
整数计数=0;
for(std::list::const_iterator ii=all_foos.begin();…)
fooArray[count]=*ii;
func(fooArray);
删除数组;
}
%}
这里应该不需要typemap。我想我找到了一个解决方案,只需浏览一下我生成的一些包装代码:(Foo*)SWIG\u Python\u GetSwigThis(o)->ptr
似乎从o变成了一个正确有效的Foo*
。这可以吗,还是我滥用了一些内部SWIG API?我不同意你关于类型映射的说法-它们应该在需要时编写,以给目标语言用户最直观的体验。@flexo我的措辞有点过于简洁,因为我同意,希望现在更好。我同意@flexo,我宁愿给我的用户一个更具Python风格的界面。对于python程序员来说,carrays有点难看。这是完美的——超出了我的需要,但我肯定会使用它。使用温度向量的想法很好,谢谢!实际上,它对我的帮助更大,因为在我的实际代码中,每个元素都需要一个aux ptr(一个py_缓冲区视图),这使我的freearg变得复杂——我不再需要这种方式。
%typemap(in) Foo ** (std::vector<Foo*> temp) {
if (PyList_Check($input)) {
const size_t size = PyList_Size($input);
temp.resize(size+1);
for (int i = 0; i < size; ++i) {
void *argp = 0 ;
const int res = SWIG_ConvertPtr(PyList_GetItem($input, i), &argp, $*1_descriptor, 0);
if (!SWIG_IsOK(res)) {
SWIG_exception_fail(SWIG_ArgError(res), "in method '" "$symname" "', argument " "$argnum"" of type '" "$1_type""'");
}
temp[i] = reinterpret_cast<Foo*>(argp);
}
temp[size] = NULL;
$1 = &temp[0]; // Always valid since we +1
}
else {
// Raise exception
SWIG_exception_fail(SWIG_TypeError, "Expected list in $symname");
}
}
%include <std_list.i>
%ignore func
%rename(func) funcWrap
namespace std {
%template(FooList) std::list<Foo*>;
}
%include "Foo.h"
%inline %{
// wrap with const list of non-const Foo*
void funcWrap(const std::list<Foo *>& all_foos)
{
// create Foo* array:
Foo* fooArray = new Foo(all_foos.size());
int count = 0;
for (std::list<Foo *>::const_iterator ii=all_foos.begin(); ...)
fooArray[count] = *ii;
func(fooArray);
delete fooArray;
}
%}