为了回答您为什么收到有关“ISO C 禁止将对象指针转换为函数指针类型”的警告的问题,我检查了 numpy 的源代码。
PyArray_SimpleNew
是一个定义在的宏numpy/ndarrayobject.h
第 125 行:
#define PyArray_SimpleNew(nd, dims, typenum) \
PyArray_New(&PyArray_Type, nd, dims, typenum, NULL, NULL, 0, 0, NULL)
将 A 行展开为:
PyObject * A = PyArray_New(&PyArray_Type, 1, dims, typenum, NULL, NULL, 0, 0, NULL); // Line A
PyArray_New
本身是一个定义在的宏numpy/__multiarray_api.h
1017行:
#define PyArray_New \
(*(PyObject * (*)(PyTypeObject *, int, npy_intp *, int, npy_intp *, void *, int, int, PyObject *)) \
PyArray_API[93])
将 A 行展开为:
PyObject * A = (*(PyObject * (*)(PyTypeObject *, int, npy_intp *, int, npy_intp *, void *, int, int, PyObject *))
PyArray_API[93])(&PyArray_Type, 1, dims, typenum, NULL, NULL, 0, 0, NULL); // Line A
这个复杂的表达式可以简化为:
// PyObject * function93(PyTypeObject *, int, npy_intp *, int, npy_intp *, void *, int, int, PyObject *)
typedef PyObject * (*function93)(PyTypeObject *, int, npy_intp *, int, npy_intp *, void *, int, int, PyObject *);
// Get the PyArray API function #93, cast the function pointer to its
// signature, and call it with the arguments to `PyArray_New`.
PyObject * A = (*(function93) PyArray_API[93])(&PyArray_Type, 1, dims, typenum, NULL, NULL, 0, 0, NULL); // Line A
导致禁止转换的部分是:
*(function93) PyArray_API[93]
In numpy/__multiarray_api.h
第 807、810 和 812 行PyArray_API
是
声明为void **
. So PyArray_API[93]
is a void *
(即一个对象
指针),它被转换为函数指针。
我不太熟悉 NumPy 或其 C-api,但看起来你很熟悉
正确使用它。 NumPy 只是碰巧使用了一些非标准的、未定义的
GCC 支持但 ISO 标准不支持的内部行为(即 NumPy 不能通过 ISO 标准移植)。
也可以看看[SciPy-User] NumPy C API:将对象指针转换为函数指针从何而来? http://mail.scipy.org/pipermail/scipy-user/2012-June/032371.html