[Numpy-discussion] newbie: PyArray_Check difficulties

Tom Adelman tla at research.nj.nec.com
Thu Feb 3 15:57:41 CST 2000


I'm having a problem with PyArray_Check.  If I just call
PyArray_Check(args) I don't have a problem, but if I try to assign the
result to anything, etc., it crashes (due to acces violation).  So, for
example the code at the end of this note doesn't work, yet I know an array
is being passed and I can, for example, calculate its trace correctly if I
type cast it as a PyArrayObject*.

Also, a more general question: is this the recommended way to input numpy
arrays when using swig, or do most people find it easier to use more
elaborate typemaps, or something else?

Finally, I apologize if this is the wrong forum to post this question.
Please let me know.

Thanks, Tom 



Method from C++ class:

PyObject * Test01::trace(PyObject * args)
{
	if (!(PyArray_Check(args))) {  // <- crashes here
		PyErr_SetString(PyExc_ValueError, "must use NumPy array");
		return NULL;
	}
	return NULL;
}


Swig file: (where typemaps are the ones included with most recent swig)

/* TMatrix.i */
%module Ptest
%include "typemaps.i"
%{
#include "Test01.h"
%}

class Test01  
{
public:
	PyObject * trace(PyObject *INPUT);
	Test01();
	virtual ~Test01();
};


Python code:

import Ptest
t = Ptest.Test01()

import Numeric
a = Numeric.arange(1.1, 2.7, .1)
b = Numeric.reshape(a, (4,4))

x = t.trace(b)






More information about the Numpy-discussion mailing list