[C++-sig] numpy.ndarray did not match PyArrayObject *?

David Abrahams dave at boost-consulting.com
Sat Sep 23 15:56:56 CEST 2006


"Qinfeng(Javen) Shi " <shiqinfeng at gmail.com> writes:

> Dear All,
>
> I wrapped a c++ code which need accept numpy.ndarray as argument.
> Here numpy.version='1.0rc1'.
> And to the best of my knowledge, numpy.ndarray's c type is PyArrayObject.
>
> But why when I run it, it reports type did not match?
>
> running result as following:
>>>> b = numpy.array([1])
>>>> b
> array([1])
>>>> 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*)
>>>>
>
> Any suggestion?

You need to register an lvalue converter from Python numpy.ndarray to
PyArrayObject.

  struct extract_array_object
  {
      static PyArrayObject& execute(PyArrayObject& x)
      {
          return x;
      }
  };

  ...
  // in your module definition function
  boost::python::lvalue_from_pytype<extract_array_object,&PyArrayObject_Type>();
                                                          ^^^^^^^^^^^^^^^^^^

replace the indicated with the name of NumPy's python type object for ndarray.


-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com




More information about the Cplusplus-sig mailing list