python - 如何使用 boost.python 将预填充的 "unsigned char*"缓冲区传递给 C++ 方法?

标签 python c++ boost ctypes boost-python

我有一个 C++ 类,它有一个成员函数,它接受一个 unsigned char* 缓冲区和一个 unsigned int 长度作为参数并对它们进行操作。我已经用 Boost::Python 包装了这个类,并希望将一个预填充的缓冲区从 Python 脚本传递给这个类。 Python 端缓冲区是使用 struct.pack 创建的。我不知道如何使参数类型匹配并不断收到 Boost.Python.ArgumentError。

include/Example.h

#ifndef EXAMPLECLASS_H_
#define EXAMPLECLASS_H_

#include <cstdio>

class ExampleClass
{
public:
    ExampleClass() {}
    virtual ~ExampleClass() {}

    void printBuffer(unsigned char* buffer, unsigned int length)
    {
        for (unsigned int i = 0; i < length; ++i)
        {
            printf("%c", buffer[i]);
        }

        printf("\n");
    }
};

#endif

src/example.cpp

#include "Example.h"

int main(int argc, char** argv)
{
    unsigned char buf[4];
    buf[0] = 0x41;
    buf[1] = 0x42;
    buf[2] = 0x43;
    buf[3] = 0x44;

    ExampleClass e;
    e.printBuffer(buf, 4);

    return 0;
}

src/Example_py.cpp

#include <boost/python.hpp>
#include "Example.h"

using namespace boost::python;

BOOST_PYTHON_MODULE(example_py)
{
    class_<ExampleClass>("ExampleClass")
    .def("printBuffer", &ExampleClass::printBuffer)
    ;
}

脚本/example.py
#!/usr/bin/env python

import example_py
import struct
import ctypes

buf = struct.pack('BBBB', 0x41, 0x42, 0x43, 0x44)

print 'python:'
print buf

e = example_py.ExampleClass()

print 'c++:'
print e.printBuffer(ctypes.cast(ctypes.c_char_p(buf), ctypes.POINTER(ctypes.c_ubyte)), len(buf))

CMakeLists.txt(不完整)

include_directories(
    include
    ${Boost_INCLUDE_DIRS}
    ${PYTHON_INCLUDE_DIRS}
)

add_library(example_py
    src/Example_py.cpp
)
target_link_libraries(example_py ${Boost_LIBRARIES} ${PYTHON_LIBRARIES})
set_target_properties(example_py PROPERTIES PREFIX "")

add_executable(example src/example.cpp)
target_link_libraries(example example_py)

输出

$ ./example
ABCD

$ ./scripts/example.py
python: ABCD
c++:
Traceback (most recent call last):
  File "/home/dustingooding/example/scripts/example.py", line 13, in <module>
    print 'c++:', e.printBuffer(ctypes.cast(ctypes.c_char_p(buf), ctypes.POINTER(ctypes.c_ubyte)), len(buf))
Boost.Python.ArgumentError: Python argument types in
    ExampleClass.printBuffer(ExampleClass, LP_c_ubyte, int)
did not match C++ signature:
    printBuffer(ExampleClass {lvalue}, unsigned char*, unsigned int)

我尝试了多种不同的方法(直接传递'buf',将'buf'作为ctypes.c_char_p传递,创建一个ctypes.ubyte数组并用'buf'的内容填充它并传递它),但似乎都不起作用。

我不明白为什么 'LP_c_ubyte' 和 'unsigned char*' 不匹配。

编辑

这是一个带有现成代码库的 Github 项目。随意使用它。我添加了@Tanner 的修复程序。 https://github.com/dustingooding/boost_python_ucharp_example

最佳答案

可能值得考虑将 Pythonic 辅助函数公开为 ExampleClass.printBuffer Python 的方法,委托(delegate)给 c-ish ExampleClass::printBuffer成员函数。例如,这将允许 Python 用户调用:

import example
import struct

buf = struct.pack('BBBB', 0x41, 0x42, 0x43, 0x44)
e.printBuffer(buf)

而不是要求用户执行正确的 ctypes类型转换和定型。


struct.pack() 方法返回 str Python2 中的对象和 bytes Python3 中的对象,因此辅助 C++ 函数需要使用 str 中的元素填充连续的内存块。或 bytes . boost::python::stl_input_iterator 可以提供一种方便的方式来构造C++容器,例如std::vector<char> ,来自 Python 对象,例如 strbytes .唯一奇怪的是 stl_input_iterator期望 Python 类型支持可迭代协议(protocol),即 str不做。但是,内置 iter() Python 方法可用于创建可迭代对象。

/// @brief Auxiliary function used to allow a Python iterable object with char
///        elements to be passed to ExampleClass.printBuffer().
void example_class_print_buffer_wrap(
  ExampleClass& self,
  boost::python::object py_buffer)
{
  namespace python = boost::python;
  // `str` objects do not implement the iterator protcol (__iter__),
  // but do implement the sequence protocol (__getitem__).  Use the
  // `iter()` builtin to create an iterator for the buffer.
  // >>> __builtins__.iter(py_buffer)
  python::object locals(python::borrowed(PyEval_GetLocals()));
  python::object py_iter = locals["__builtins__"].attr("iter");
  python::stl_input_iterator<char> begin(
     py_iter(py_buffer)), end;

  // Copy the py_buffer into a local buffer with known continguous memory.
  std::vector<char> buffer(begin, end);

  // Cast and delegate to the printBuffer member function.
  self.printBuffer(
    reinterpret_cast<unsigned char*>(&buffer[0]),
    buffer.size());
}

创建辅助函数后,只需要将其公开为ExampleClass.printBuffer即可。方法:

BOOST_PYTHON_MODULE(example)
{
  namespace python = boost::python;
  python::class_<ExampleClass>("ExampleClass")
    .def("printBuffer", &example_class_print_buffer_wrap)
    ;
}

这是一个完整的例子demonstrating这种方法:

#include <cstdio>
#include <vector>
#include <boost/python.hpp>
#include <boost/python/stl_iterator.hpp>

// Mocks...
/// @brief Legacy class that cannot be changed.
class ExampleClass
{
public:
  void printBuffer(unsigned char* buffer, unsigned int length)
  {
    for (unsigned int i = 0; i < length; ++i)
    {
      printf("%c", buffer[i]);
    }

    printf("\n");
  }
};

/// @brief Auxiliary function used to allow a Python iterable object with char
///        elements to be passed to ExampleClass.printBuffer().
void example_class_print_buffer_wrap(
  ExampleClass& self,
  boost::python::object py_buffer)
{
  namespace python = boost::python;
  // `str` objects do not implement the iterator protcol (__iter__),
  // but do implement the sequence protocol (__getitem__).  Use the
  // `iter()` builtin to create an iterator for the buffer.
  // >>> __builtins__.iter(py_buffer)
  python::object locals(python::borrowed(PyEval_GetLocals()));
  python::object py_iter = locals["__builtins__"].attr("iter");
  python::stl_input_iterator<char> begin(
     py_iter(py_buffer)), end;

  // Copy the py_buffer into a local buffer with known continguous memory.
  std::vector<char> buffer(begin, end);

  // Cast and delegate to the printBuffer member function.
  self.printBuffer(
    reinterpret_cast<unsigned char*>(&buffer[0]),
    buffer.size());
}

BOOST_PYTHON_MODULE(example)
{
  namespace python = boost::python;
  python::class_<ExampleClass>("ExampleClass")
    .def("printBuffer", &example_class_print_buffer_wrap)
    ;
}

交互使用:

>>> import example
>>> import struct
>>> buf = struct.pack('BBBB', 0x41, 0x42, 0x43, 0x44)
>>> print 'python:', buf
python: ABCD
>>> e = example.ExampleClass()
>>> e.printBuffer(buf)
ABCD

关于python - 如何使用 boost.python 将预填充的 "unsigned char*"缓冲区传递给 C++ 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32634765/

相关文章:

python - Pandas :计算所有仅在两列中不同的条目的平均值

python - python中如何从ROS实时获取数据?

c++ - 从重载函数中选择常量表达式?

c++ - Boost 堆元素句柄比较和 MSVC 迭代器调试工具

c++ - 仅当尝试在 Xcode 7.1 中使用 Instruments 时出现有关 Boost 文件的编译器错误

c++ - 链接多个使用 boost 的项目时出现 LNK2005 错误

python - 如何在pandas python中查找和匹配不同数据框中的特定值

Python 3.3 - 生成奇数

c++ - 如果运算符是常量,运算符 << 重载通常会失败?

c++ - 如何从 std::wstring _TCHAR [] 转换?