Easy extending guide¶
“… Boost.Python library is designed to wrap C++ interfaces non-intrusively, so that you should not have to change the C++ code at all in order to wrap it.”
The previous statement is almost true. There are few use cases that the library doesn’t support. This guide will list some of them and will offer few possible solutions.
Pointer to function¶
Boost.Python doesn’t handle “pointer to function” functionality. You cannot pass it as function argument or keep it, as a member variable.
The simple work-around is to use command design pattern
Problematic function arguments types¶
Boost.Python doesn’t handle
C arrays, the only exception are
Consider the following function:
int write( int* data, size_t size );
The technical reasons are not the only one that prevent Boost.Python to expose such
functions, there is a mental one: such interface is not intuitive for Python
developers. They expect to pass single argument. For example, built-in
method takes a single argument - sequence of characters.
- With small help from the developer, Py++ generates code which feets well into Python developer mental model. Pure virtual member functions are a special case, which Py++ doesn’t handle right now.
- Use STL containers,
Immutable by reference¶
Python defines few fundamental types as “immutable”. The value of an instance of the immutable type could not be changed after construction. Try to avoid passing the immutable types by reference.
short unsigned int
long unsigned int
long long int
long long unsigned int
complex long double
enumis mapped to Python
- smart pointers
- Just don’t pass them by reference :-)
- With small help from the developer, Py++ generates code which work-arounds this issue, but the resulting interface is ugly.
In most cases,
void* is used when a developer has to deal with a memory block.
Python provides support for this functionality, but I still didn’t find an easy and
intuitive way to expose it. There is no work-around for this issue.
If you use
void* to pass a reference to some object, than Boost.Python and Py++
support such use case.
std::auto_ptrto transfer ownership and responsibility for an object destruction.
- The only well supported smart pointer class is
boost::shared_ptr. I suggest you to use it all the time, especially in cases where you want to create object from Python and pass ownership to C++ code. You don’t want the headache associated with this task.