【发布时间】:2015-09-09 11:43:35
【问题描述】:
我在使用 SWIG(版本 3.0.6)围绕 C++ 库生成 Python 包装器时遇到了一些问题。
我的问题与应用 OUTPUT 类型映射有关,特别是在指针/引用类类型的情况下。
为了说明,这就是我想要的标准类型,并且它有效:
// .h
int add(const long arg1,const long arg2,long& resultLong);
// interface.i
%apply long& OUTPUT { long& resultLong };
int add(const long arg1,const long arg2,long& resultLong);
// projectWrapper.py
def add(arg1, arg2):
return _projectWrapper.add(arg1, arg2)
addTerm = _projectWrapper.add
// usage
>>> result = projectWrapper.add(2, 4)
>>> print result
[0, 6L]
您不必传入“resultLong”,但它会自动附加到结果中。太好了!
但是,当输出类型是指向类类型的指针时,这似乎不像我预期的那样工作:
// .h
int GetClassType(const char* name, exportedClassType*& resultPointer);
class exportedClassType
{...}
// interface.i
%apply exportedClassType*& OUTPUT { exportedClassType*& resultPointer };
int GetClassType(const char* name, exportedClassType*& resultPointer);
// projectWrapper.py
def GetClassType(name, resultPointer):
return _projectWrapper.GetClassType(name, resultPointer)
GetClassType = _projectWrapper.GetClassType
问题似乎是 SWIG 没有以与简单类型相同的方式处理它。它仍然作为包装函数签名中的“输入”参数出现。
// attempted usage
>>> classType = projectWrapper.GetClassType("name")
TypeError: GetClassType() takes exactly 2 arguments (1 given)
>>> result = 0
>>> projectWrapper.GetClassType("name", result)
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: in method 'GetClassType', argument 2 of type 'exportedClassType *&'
有人可以告诉我我做错了什么或指出我正确的方向吗?任何帮助都感激不尽!谢谢
【问题讨论】:
-
你试过使用双指针吗?我在使用 SWIG 2.0.7 时遇到了双指针和生成代码的问题,但这在 3.X.X 中得到了解决
-
感谢您的回复。抱歉,interface.i 文件中的内容是什么样的?
-
我应该提到我不拥有 C++ 并且不能在那里更改方法签名。我肯定需要处理采用
exportedClassType*&参数的方法。 -
好的,所以没有纯C接口。您可以做一个简单的包装器,它改为公开一个双指针。最好,你可以在你的接口文件中制作这个包装器。
-
这里给出了这个问题的解决方案stackoverflow.com/questions/12739331/…。