I am making Python code available to a C++ library using Boost::Python. I have a template function that converts C++ types to Python types:
template <typename T> bp::object convert(T v);
that is specialized for various primitive types as well as some templated classes. One such class is an N-dimensional Array, for which I have a function to convert to a NumPy Array. I'd like to use this function in a corresponding convert specialization, e.g.:
template <typename Y> bp::object convert(NDArray<Y> v);
My primary issue is that this conversion function then needs to live in a header since it's templated, but it uses NumPy's PyArray_ functions, which require import_array() to be called prior to usage. import_array() is currently called in the constructor for a singleton object whose purpose is to provide access to Python functions. It appears this won't work because, by default, #include <numpy/arrayobject.h> only makes the PyArray_ functions available to the current compilation unit. I've tried defining a PY_ARRAY_UNIQUE_SYMBOL and defining NO_IMPORT_ARRAY for the header, but this doesn't prevent PyArray_ functions from segfaulting.
Here's a simplified representation of my code, which segfaults upon using a PyArray_ function in the "conversions.h" header:
"conversions.h":
#include <boost/python.hpp>
#include <numpy/numpyconfig.h>
#include <numpy/arrayobject.h>
namespace bp = boost::python;
template <typename T> bp::object convert(T v);
template <> bp::object convert<int>(int v) { return bp::long_(v); }
...
template <typename Y> bp::object convert(NDArray<Y> v)
{
... use PyArray_ functions to create and return a NumPy array
... segfaults here!
}
"Bridge.h":
#include "conversions.h"
class Bridge {
public:
static Bridge* instance();
// c++11 variadic template (parameter pack)
template <typename... Args> void exec(Args... args)
{
...
fn(convert(args)...); // fn is a Python function
...
}
void foo();
private:
Bridge();
Bridge(const Bridge&);
void operator=(const Bridge&);
static Bridge* instance_;
}
"Bridge.cpp":
#include "Bridge.h"
#include <numpy/numpyconfig.h>
#include <numpy/arrayobject.h>
Bridge* Bridge::instance_ = nullptr;
Bridge* Bridge::instance() {
if (!instance_) { instance_ = new Bridge(); }
return instance_;
}
Bridge::Bridge() {
Py_Initialize();
_import_array();
...
}
void Bridge::foo() {
... // other stuff using PyArray functions
}
"main.cpp":
#include "Bridge.h"
int main(void)
{
NDArray my_array(...);
Bridge::instance()->exec(42, "hello", my_array);
return 0;
}