[英]exposing method with unsigned char & argument using Boost.Python
I've closed-source C++ library, which provides header files with code equivalent to: 我已经关闭了C ++库,该库提供的头文件的代码等效于:
class CSomething
{
public:
void getParams( unsigned char & u8OutParamOne,
unsigned char & u8OutParamTwo ) const;
private:
unsigned char u8OutParamOne_,
unsigned char u8OutParamTwo_,
};
I'm trying to expose that to Python, my wrapper code is something like this: 我试图将其公开给Python,我的包装器代码如下所示:
BOOST_PYTHON_MODULE(MySomething)
{
class_<CSomething>("CSomething", init<>())
.def("getParams", &CSomething::getParams,(args("one", "two")))
}
Now I'm trying to use that in Python, which fails horribly: 现在,我正在尝试在Python中使用它,但失败的非常严重:
one, two = 0, 0
CSomething.getParams(one, two)
Which results in: 结果是:
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)
What do I need to change either in the Boost.Python wrapper code or Python code to make it work? 我需要在Boost.Python包装器代码或Python代码中进行哪些更改才能使其正常工作? How do I add some Boost.Python magic to automatically cast
PyInt
to unsigned char
and vice-versa? 如何添加一些Boost.Python魔术来自动将
PyInt
为unsigned char
,反之亦然?
Boost.Python
is complaining about a missing lvalue
parameter, a concept which does not exist in Python: Boost.Python
抱怨缺少lvalue
参数,这个概念在Python中不存在:
def f(x):
x = 1
y = 2
f(y)
print(y) # Prints 2
The x
paramter of the f
function is not a C++-like reference. f
函数的x
参数不是类似C ++的引用。 In C++ the output is different: 在C ++中,输出是不同的:
void f(int &x) {
x = 1;
}
void main() {
int y = 2;
f(y);
cout << y << endl; // Prints 1.
}
You have a few choices here: 您可以在这里选择:
a) Wrap the CSomething.getParams
function to return a tuple of the new parameters values: a)包装
CSomething.getParams
函数以返回新参数值的元组:
one, two = 0, 0
one, two = CSomething.getParams(one, two)
print(one, two)
b) Wrap the CSomething.getParams
function to accept a class instance as parameter: 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.