【发布时间】:2012-10-16 14:24:03
【问题描述】:
我有一个闭源 C++ 库,它提供的头文件代码相当于:
class CSomething
{
public:
void getParams( unsigned char & u8OutParamOne,
unsigned char & u8OutParamTwo ) const;
private:
unsigned char u8OutParamOne_,
unsigned char u8OutParamTwo_,
};
我正在尝试将它暴露给 Python,我的包装代码是这样的:
BOOST_PYTHON_MODULE(MySomething)
{
class_<CSomething>("CSomething", init<>())
.def("getParams", &CSomething::getParams,(args("one", "two")))
}
现在我正在尝试在 Python 中使用它,但失败了:
one, two = 0, 0
CSomething.getParams(one, two)
结果:
ArgumentError: Python argument types in
CSomething.getParams(CSomething, int, int)
did not match C++ signature:
getParams(CSomething {lvalue}, unsigned char {lvalue} one, unsigned char {lvalue} two)
我需要在 Boost.Python 包装器代码或 Python 代码中进行哪些更改才能使其正常工作?如何添加一些 Boost.Python 魔法以自动将 PyInt 转换为 unsigned char 或反之亦然?
【问题讨论】:
-
这两个参数是 Python 没有的引用,请参见 question。
-
@martineau:但我无法改变这一点,所以必须有办法解决这个问题
-
好的,this 答案有一个解决方法。
-
你看过call policies吗?如果您必须使用引用,请尝试使用 return_internal_reference 或 return_value_policy
-
@martineau, @georgesl:你们都专注于参考问题,而错误是关于
int和unsigned char之间的不匹配。
标签: c++ python boost-python