公开带有无符号字符的方法&参数使用Boost.Python

2024-04-27 04:26:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经关闭了源C++库,它提供了与E/P>等价的代码头文件

class CSomething
{
  public:
      void getParams( unsigned char & u8OutParamOne, 
                      unsigned char & u8OutParamTwo ) const;
  private:
      unsigned char u8OutParamOne_,
      unsigned char u8OutParamTwo_,
};

我正试图将其公开给Python,我的包装代码如下:

^{pr2}$

现在我尝试在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,反之亦然?在


Tags: 代码头文件oneclassint等价boosttwo
1条回答
网友
1楼 · 发布于 2024-04-27 04:26:11

Boost.Python正在抱怨丢失的lvalue参数,这个概念在Python中不存在:

def f(x):
  x = 1

y = 2
f(y)
print(y) # Prints 2
<^ > ^ {< CD3>}的^ {< CD4>}函数的参数不是C++类引用。在C++中,输出不同:

^{pr2}$

你有几个选择:

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)

相关问题 更多 >