[C++-sig] boost::python: marshalling buffers

Christoph Rupp chris at crupp.de
Wed Apr 18 19:57:14 CEST 2012


Hi,

i have a C++ class that i'm wrapping with boost::python:

class Foo {
  // ...
  const char *get_buffer();
  size_t get_size() const;
  // ...
};

I want to send this class through a thrift interface (which uses the
write() method to send the data to a file or a socket). For a few days now
i'm trying to implement this. By now i understand that i have to implement
the buffer protocol, and i know that i can create a buffer with
boost::python (read-only access is sufficient):

inline object as_buffer(const Foo &f) {
  return object (handle<> (PyBuffer_FromMemory ((void*)f.get_buffer(),
f.get_size())));
}

(This is based on an email from 2009 that i found in this mailing list's
archive.)

But i do not know how to glue it together to form a full module. This is
how the module looks right now:

BOOST_PYTHON_MODULE(Foo)
{
  class_<Foo, boost::noncopyable>("Foo",
          init<int32_t, bool>())
    .def("add", afn)
    .def("finalize", &Foo::finalize)
    .def("empty", &Foo::empty)
    .def("clear", &Foo::clear)
  ;
}

How can i include that buffer conversion?

Thanks a lot,
Christoph
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20120418/b984a403/attachment.html>


More information about the Cplusplus-sig mailing list