快速修复(继续阅读以了解更多详细信息和更复杂的方法):
您需要通过调用import_array(),在您使用numpy-stuff的每个cpp文件中初始化变量PyArray_API:
//it is only a trick to ensure import_array() is called, when *.so is loaded
//just called only once
int init_numpy(){
import_array(); // PyError if not successful
return 0;
}
const static int numpy_initialized = init_numpy();
void parse_ndarraray(PyObject *obj) { // would be called every time
if (PyArray_Check(obj)) {
cout << "PyArray_Check Passed" << endl;
} else {
cout << "PyArray_Check Failed" << endl;
}
}
也可以使用_import_array(如果不成功则返回负数)来使用自定义错误处理。 See here 用于定义import_array。
警告:正如@isra60 所指出的,_import_array()/import_array() 只能在 Python 初始化后调用,即在调用 Py_Initialize() 之后。扩展总是如此,但如果嵌入了 python 解释器,则并非总是如此,因为numpy_initialized 是在main-starts 之前初始化的。在这种情况下,不应使用“初始化技巧”,而是在 Py_Initialize() 之后调用 init_numpy()。
复杂的解决方案:
NB:有关信息,为什么需要设置PyArray_API,请参阅此SO-answer:为了能够将符号解析推迟到运行时,因此在链接时不需要numpy的共享对象,并且不能在动态库路径上(那么python的系统路径就足够了)。
建议的解决方案很快,但如果有多个 cpp 使用 numpy,则需要初始化很多 PyArray_API 实例。
如果PyArray_API 没有被定义为静态,而是在除一个翻译单元之外的所有翻译单元中定义为extern,则可以避免这种情况。对于那些翻译单元 NO_IMPORT_ARRAY 宏必须在 numpy/arrayobject.h 包含之前定义。
然而,我们需要一个翻译单元来定义这个符号。对于此翻译单元,不得定义宏 NO_IMPORT_ARRAY。
但是,如果不定义宏 PY_ARRAY_UNIQUE_SYMBOL,我们将只得到一个静态符号,即对其他翻译单元不可见,因此链接器将失败。原因:如果有两个库并且每个人都定义了一个PyArray_API,那么我们将有一个符号的多个定义并且链接器将失败,即我们不能同时使用这两个库。
因此,通过在 numpy/arrayobject.h 的每个包含之前将 PY_ARRAY_UNIQUE_SYMBOL 定义为 MY_FANCY_LIB_PyArray_API,我们将拥有自己的 PyArray_API-name,这不会与其他库发生冲突。
把它们放在一起:
答: use_numpy.h - 包含 numpy 功能的标头,即 numpy/arrayobject.h
//use_numpy.h
//your fancy name for the dedicated PyArray_API-symbol
#define PY_ARRAY_UNIQUE_SYMBOL MY_PyArray_API
//this macro must be defined for the translation unit
#ifndef INIT_NUMPY_ARRAY_CPP
#define NO_IMPORT_ARRAY //for usual translation units
#endif
//now, everything is setup, just include the numpy-arrays:
#include <numpy/arrayobject.h>
B: init_numpy_api.cpp - 用于初始化全局MY_PyArray_API的翻译单元:
//init_numpy_api.cpp
//first make clear, here we initialize the MY_PyArray_API
#define INIT_NUMPY_ARRAY_CPP
//now include the arrayobject.h, which defines
//void **MyPyArray_API
#inlcude "use_numpy.h"
//now the old trick with initialization:
int init_numpy(){
import_array();// PyError if not successful
return 0;
}
const static int numpy_initialized = init_numpy();
C:只要你需要 numpy 就包含use_numpy.h,它会定义extern void **MyPyArray_API:
//example
#include "use_numpy.h"
...
PyArray_Check(obj); // works, no segmentation error
警告:不要忘记,要使初始化技巧起作用,必须已经调用了Py_Initialize()。
为什么需要它(出于历史原因保留):
当我使用调试符号构建您的扩展时:
extra_compile_args=['-fPIC', '-O0', '-g'],
extra_link_args=['-O0', '-g'],
并使用 gdb 运行它:
gdb --args python run_test.py
(gdb) run
--- Segmentation fault
(gdb) disass
我可以看到以下内容:
0x00007ffff1d2a6d9 <+20>: mov 0x203260(%rip),%rax
# 0x7ffff1f2d940 <_ZL11PyArray_API>
0x00007ffff1d2a6e0 <+27>: add $0x10,%rax
=> 0x00007ffff1d2a6e4 <+31>: mov (%rax),%rax
...
(gdb) print $rax
$1 = 16
我们应该记住,PyArray_Check 只是一个define for:
#define PyArray_Check(op) PyObject_TypeCheck(op, &PyArray_Type)
看来&PyArray_Type 以某种方式使用了PyArray_API 的一部分,该部分未初始化(具有值0)。
我们看一下预处理器后的cpp_parser.cpp(编译时带有标志-E:
static void **PyArray_API= __null
...
static int
_import_array(void)
{
PyArray_API = (void **)PyCapsule_GetPointer(c_api,...
所以PyArray_API 是静态的并通过_import_array(void) 初始化,这实际上可以解释我在构建期间收到的警告,即_import_array() 已定义但未使用-我们没有初始化PyArray_API。
因为PyArray_API 是一个静态变量,它必须在每个编译单元(即 cpp - 文件)中初始化。
所以我们只需要这样做 - import_array() 似乎是官方的方式。