Boost.Python: Wrap functions to release the GIL

Exposing functors as methods is not officially supported. The supported approach would be to expose a non-member function that delegates to the member-function. However, this can result in a large amount of boilerplate code. As best as I can tell, Boost.Python’s implementation does not explicitly preclude functors, as it allows for instances of python::object to … Read more

Feeding a Python list into a function taking in a vector with Boost Python

There are a few solutions to accomplish this without having to modify the original functions. To accomplish this with a small amount of boilerplate code and transparency to python, consider registering a custom converter. Boost.Python uses registered converters when going between C++ and Python types. Some converters are implicitly created when creating bindings, such as … Read more

how to return numpy.array from boost::python?

UPDATE: the library described in my original answer (https://github.com/ndarray/Boost.NumPy) has been integrated directly into Boost.Python as of Boost 1.63, and hence the standalone version is now deprecated. The text below now corresponds to the new, integrated version (only the namespace has changed). Boost.Python now includes a moderately complete wrapper of the NumPy C-API into a … Read more

std::vector to boost::python::list

boost::python already includes functionality for wrapping vectors and maps. Here’s sample code for vectors, as you can see both passing and returning lists is quite simple: // C++ code typedef std::vector<std::string> MyList; class MyClass { MyList myFuncGet(); void myFuncSet(const Mylist& list); // stuff }; // Wrapper code #include <boost/python/suite/indexing/vector_indexing_suite.hpp> using namespace boost::python; BOOST_PYTHON_MODULE(mymodule) { class_<MyList>(“MyList”) … Read more