[C++-sig] (Philip Austin) (David Abrahams)numpy.ndarray did not match PyArrayObject
Qinfeng(Javen) Shi
shiqinfeng at gmail.com
Wed Sep 27 02:11:33 CEST 2006
Hi Philip and David
What I really want to do is to access numpy.ndarray in c extension. I tried
2 ways and Philip told me another possible way but I failed.
----------------------------------------------------- hello.c
1st way:
void testArray(PyArrayObject * args)
{
}
>>> hello.testArray(b)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
Boost.Python.ArgumentError: Python argument types in
hello.testArray(numpy.ndarray)
did not match C++ signature:
testArray(PyArrayObject*)
>>>
------------------------------------------------------hello.c
2nd way:
void testArray(PyObject * args)
{
PyObject *input;
PyArrayObject *array;
if (!PyArg_ParseTuple(args, "O", &input))
printf("getting input failed!\n");
else
printf("getting input sucess!\n");
}
>>> import hello
>>> import numpy
>>> c = numpy.arange(25).reshape((5,5))
>>> d = hello.testArray(c);
getting input failed!
>>> d
SystemError: new style getargs format but argument is not a tuple
>>>
then I tried adding following lines asPhilip told me to hello.cpp file.
------------------------------------------------hello.cpp
namespace ar=boost::python::numeric;
namespace bp=boost::python;
import_array();
ar::array::set_module_and_type ("numpy", "ndarray");
It didn't crash but still had error. The interesting thing is that now
running it
twice would get two different error informations.
---------------------------------------------------error report in
python
>>> import hello
>>> import numpy
>>> a = numpy.arange(25).reshape((5,5));
>>> hello.testArray(a) // first time
SystemError: new style getargs format but argument is not a tuple
>>> hello.testArray(a) // second time
args is array type! //it means PyArray_Check(args)
successed this time.
getting input failed! //PyArg_ParseTuple() failed but not crashed
I am upset why I can't access numpy.ndarray.
Any suggestion?
Thanks!
--
Qinfeng(Javen) Shi
Research School of Information Sciences and Engineering
Australian National University
Locked Bag 8001
Canberra ACT 2601
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20060927/08f51f5b/attachment.htm>
More information about the Cplusplus-sig
mailing list