我有一个封闭源码的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,反之亦然?
发布于 2012-10-17 00:22:42
Boost.Python抱怨缺少一个lvalue参数,这个概念在Python中是不存在的:
def f(x):
x = 1
y = 2
f(y)
print(y) # Prints 2f函数的x参数不是类似C++的引用。在C++中,输出是不同的:
void f(int &x) {
x = 1;
}
void main() {
int y = 2;
f(y);
cout << y << endl; // Prints 1.
}这里有几个选择:
a)包装CSomething.getParams函数以返回新参数值的元组:
one, two = 0, 0
one, two = CSomething.getParams(one, two)
print(one, two)b)包装CSomething.getParams函数以接受一个类实例作为参数:
class GPParameter:
def __init__(self, one, two):
self.one = one
self.two = two
p = GPParameter(0, 0)
CSomething.getParams(p)
print(p.one, p.two)https://stackoverflow.com/questions/12916877
复制相似问题