我正在使用 Boost::Python 将 Python 代码提供给 C++ 库。我有一个将 C++ 类型转换为 Python 类型的模板函数:
template <typename T> bp::object convert(T v);
它专门用于各种原始类型以及一些模板化类。其中一个类是 N 维数组,我有一个函数可以将其转换为 NumPy 数组。我想在相应的功能中使用这个功能convert
专业化,例如:
template <typename Y> bp::object convert(NDArray<Y> v);
我的主要问题是这个转换函数需要存在于标头中,因为它是模板化的,但它使用 NumPy 的PyArray_
函数,这需要import_array()
在使用之前调用。import_array()
当前在单例对象的构造函数中调用,其目的是提供对 Python 函数的访问。看来这行不通,因为默认情况下,#include <numpy/arrayobject.h>
只使得PyArray_
当前编译单元可用的函数。我尝试过定义一个PY_ARRAY_UNIQUE_SYMBOL
并定义NO_IMPORT_ARRAY
对于标题,但这并不能阻止PyArray_
防止段错误的功能。
这是我的代码的简化表示,它在使用时出现段错误PyArray_
“conversions.h”标头中的函数:
“转换.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!
}
“桥.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_;
}
“桥.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
}
“主.cpp”:
#include "Bridge.h"
int main(void)
{
NDArray my_array(...);
Bridge::instance()->exec(42, "hello", my_array);
return 0;
}