13

我有第三方 C++ 库,其中一些类方法使用原始字节缓冲区。我不太确定如何处理 Boost::Python 。

C++ 库头文件类似于:

class CSomeClass
{
  public:
      int load( unsigned char *& pInBufferData, int & iInBufferSize );
      int save( unsigned char *& pOutBufferData, int & iOutBufferSize );
}

坚持使用 Boost::Python 代码...

class_<CSomeClass>("CSomeClass", init<>())
    .def("load", &CSomeClass::load, (args(/* what do I put here??? */)))
    .def("save", &CSomeClass::save, (args(/* what do I put here??? */)))

如何包装这些原始缓冲区以将它们公开为 Python 中的原始字符串?

4

1 回答 1

12

您必须自己在绑定上编写函数,这些函数将从该数据返回Py_buffer对象,允许您从 Python 读取(使用PyBuffer_FromMemory)或读写(使用PyBuffer_FromReadWriteMemory)预分配的 C/C++ 内存。

这就是它的样子(欢迎反馈):

#include <boost/python.hpp>

using namespace boost::python;

//I'm assuming your buffer data is allocated from CSomeClass::load()
//it should return the allocated size in the second argument
static object csomeclass_load(CSomeClass& self) {
  unsigned char* buffer;
  int size;
  self.load(buffer, size);

  //now you wrap that as buffer
  PyObject* py_buf = PyBuffer_FromReadWriteMemory(buffer, size);
  object retval = object(handle<>(py_buf));
  return retval;
}

static int csomeclass_save(CSomeClass& self, object buffer) {
  PyObject* py_buffer = buffer.ptr();
  if (!PyBuffer_Check(py_buffer)) {
    //raise TypeError using standard boost::python mechanisms
  }

  //you can also write checks here for length, verify the 
  //buffer is memory-contiguous, etc.
  unsigned char* cxx_buf = (unsigned char*)py_buffer.buf;
  int size = (int)py_buffer.len;
  return self.save(cxx_buf, size);
}

稍后,当您绑定时CSomeClass,请使用上面的静态函数而不是方法loadsave

//I think that you should use boost::python::arg instead of boost::python::args
// -- it gives you better control on the documentation
class_<CSomeClass>("CSomeClass", init<>())
    .def("load", &csomeclass_load, (arg("self")), "doc for load - returns a buffer")
    .def("save", &csomeclass_save, (arg("self"), arg("buffer")), "doc for save - requires a buffer")
    ;

这对我来说看起来足够pythonic。

于 2013-07-20T06:57:14.680 回答