user1023380
user1023380

Reputation: 51

PyArrayObject has no members

I am trying to write a simple c++ extension to my python/numpy code. However, I am not able to compile the extension script because PyArrayObject from the function input has no members. It seems to me that I am doing the same as for example this post, but I suppose I have missed something. Here is an example that fails to compile because I try to retrieve the dimensions member:

#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];
}

This is compiled on a linux system and a windows 7 computer, using the MVS 14.0 c++ compiler, so the problem seems to be platform independent.

Python version: 3.5

Exception output from the windows system:

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

Upvotes: 2

Views: 1898

Answers (1)

Paul Otto
Paul Otto

Reputation: 11

I also had this problem when following posted examples. I believe using the dimensions member is deprecated. Instead PyArray_DIM or PyArray_DIMS should be used (see NumPy docs)

For example:

long n=PyArray_DIM(array,1);

Upvotes: 1

Related Questions