Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass a pre-populated "unsigned char*" buffer to a C++ method using boost.python?

I have a C++ class with a member function that takes an unsigned char* buffer and an unsigned int length as arguments and operates on them. I've wrapped this class with Boost::Python and would like to pass a pre-populated buffer to the class from a Python script. The Python-side buffer is created with struct.pack. I can't figure out how to make the argument type match and keep getting 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)
    ;
}

scripts/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 (incomplete)

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)

Output

$ ./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)

I've tried a number of different approaches (passing 'buf' directly, passing 'buf' as a ctypes.c_char_p, creating a ctypes.ubyte array and populating it with the contents of 'buf' and passing it), but none seem to work.

I don't understand why 'LP_c_ubyte' and 'unsigned char*' don't match.

EDIT

Here's a Github project with a ready-to-go codebase. Feel free to use this. I've added @Tanner's fix. https://github.com/dustingooding/boost_python_ucharp_example

like image 670
Dustin Avatar asked Mar 14 '23 18:03

Dustin


1 Answers

It may be worth considering exposing a Pythonic auxiliary function as the ExampleClass.printBuffer method to Python, that delegates to the c-ish ExampleClass::printBuffer member function. For instance, this would allow the Python users to invoke:

import example
import struct

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

Rather than requiring the user to perform the correct ctypes cast and sizing.


The struct.pack() method returns a str object in Python2 and a bytes object in Python3, so the auxiliary C++ function would need to populate a continuous block of memory with the elements of from either str or bytes. The boost::python::stl_input_iterator can provide a convenient way to construct C++ containers, such as std::vector<char>, from a Python object, such as str or bytes. The only oddity is that stl_input_iterator expects the Python type to support the iterable protocol, which str does not do. However, the builtin iter() Python method can be used to create an iterable object.

/// @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());
}

With the auxiliary function created, one just needs to expose it as the ExampleClass.printBuffer method:

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

Here is a complete example demonstrating this approach:

#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)
    ;
}

Interactive usage:

>>> import example
>>> import struct
>>> buf = struct.pack('BBBB', 0x41, 0x42, 0x43, 0x44)
>>> print 'python:', buf
python: ABCD
>>> e = example.ExampleClass()
>>> e.printBuffer(buf)
ABCD
like image 60
Tanner Sansbury Avatar answered Apr 09 '23 05:04

Tanner Sansbury