【问题标题】:PyArrayObject has no membersPyArrayObject 没有成员
【发布时间】:2017-06-16 13:08:49
【问题描述】:

我正在尝试为我的 python/numpy 代码编写一个简单的 c++ 扩展。但是,我无法编译扩展脚本,因为函数输入中的 PyArrayObject 没有成员。在我看来,我正在做与例如this post 相同的事情,但我想我错过了一些东西。这是一个无法编译的示例,因为我尝试检索维度成员:

#include <Python.h>
#include <stdio.h>
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include "numpy/arrayobject.h"

static PyObject *function(PyObject *self, PyObject *args) { 

    PyObject *input;
    PyArrayObject *array;

    if (!PyArg_ParseTuple(args, "O", &input))
        return NULL;

    array= (PyArrayObject *)
        PyArray_ContiguousFromObject(input, NPY_DOUBLE, 2, 2);
    long n=array->dimensions[1];
}

这是在 linux 系统和 windows 7 计算机上编译的,使用 MVS 14.0 c++ 编译器,所以问题似乎与平台无关。

Python 版本:3.5

windows系统的异常输出:

paneltime/cfunctions.cpp(20): error C2039: 'dimensions': is not a member of 'tagPyArrayObject'c:\anaconda3\lib\site-packages\numpy\core\include\numpy\ndarraytypes.h(692): note: see declaration of 'tagPyArrayObject'
error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\BIN\\x86_amd64\\cl.exe' failed with exit status 2

【问题讨论】:

标签: python c++ numpy python-c-extension


【解决方案1】:

在关注发布的示例时,我也遇到了这个问题。我相信不推荐使用维度成员。相反,应该使用 PyArray_DIM 或 PyArray_DIMS(参见NumPy docs

例如:

long n=PyArray_DIM(array,1);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-29
    • 2018-09-29
    • 2017-06-15
    • 2017-02-25
    相关资源
    最近更新 更多