[C++-sig] Exposing boost::python::numeric::array to python

Robert Lupton rhl at astro.princeton.edu
Thu Jan 11 16:41:31 CET 2007


I must be missing something.  I have a class derived from numeric::array that I 
want to expose in python, inheriting all numeric::array's methods:
    class_<image, bases<numeric::array> >("image", init<int, int>())        ;

This fails to import:
   RuntimeError: extension class wrapper for base class
        boost::python::numeric::array has not been created yet

This is confusing, as a function 
   numeric::array makeArray(...)
can be defined without any problem, and exposes the numpy:ndarray interface
as expected.

I can add:
    class_<numeric::array>("array", init<object &>())  ;
and then import my class, but it fails to inherit any of numpy:ndarray's
interface.

So how am I supposed to inherit from numeric::array?

                       R


Here's a complete test case:


#define PY_ARRAY_UNIQUE_SYMBOL PyArrayHandle
#include <boost/python.hpp>
#include <numpy/arrayobject.h>

using namespace boost::python;

class image : public numeric::array {
public:
    image(const int width, const int height) :
        numeric::array(0) {
    }
};

numeric::array makeArray(int width, int height) {
    int dimens[2] = {width, height};
    object obj(handle<>(PyArray_FromDims(2, dimens, PyArray_DOUBLE)));

    return extract<numeric::array>(obj);
}
    
BOOST_PYTHON_MODULE(numpy_b) { 
    import_array();
    numeric::array::set_module_and_type("numpy", "ndarray");

#if 1
    class_<numeric::array>("array", init<object &>())
        ;
#endif
    class_<image, bases<numeric::array> >("image", init<int, int>())
        ;

    def("makeArray", makeArray);
}





More information about the Cplusplus-sig mailing list