[C++-sig] from_python

Peter Bienstman pbienst at MIT.EDU
Wed Jul 17 17:03:29 CEST 2002


Hi,

I'm trying to upgrade a piece of code from a prehistoric version of BPL
V2 to the current CVS version:


namespace boost { namespace python { namespace converter { namespace {

struct cVector_from_python
{
  static unaryfunc* get_slot(PyObject* o)
  {
    return &py_object_identity;
  }
    
  static cVector extract(PyObject* o)
  {
    PyArrayObject* a 
      = (PyArrayObject*) PyArray_Cast((PyArrayObject*)o,
PyArray_CDOUBLE);

    if ( (a->nd != 1) || (a->dimensions[0] != int(global.N)) )
    {
      PyErr_SetString(PyExc_ValueError, "array has wrong dimensions.");
      throw boost::python::argument_error();
    }
  
    return cVector((Complex*) a->data,global.N,
                   blitz::neverDeleteData,fortranArray);
  }
};

}}}} // namespace boost::python::converter



After reading through the mailing lists and the docs
(boost/libs/python/doc/v2/from_python.html) I tried something like this:

#include <boost/python/from_python.hpp>

struct boost::python::from_python<cVector>
{

  from_python(PyObject*) {}

  bool convertible() const {return true;}
    
  cVector operator()(PyObject* o) const
  {
    PyArrayObject* a 
      = (PyArrayObject*) PyArray_Cast((PyArrayObject*)o,
PyArray_CDOUBLE);

    if ( (a->nd != 1) || (a->dimensions[0] != int(global.N)) )
    {
      PyErr_SetString(PyExc_ValueError, "array has wrong dimensions.");
      throw boost::python::argument_error();
    }
  
    return cVector((Complex*) a->data,global.N,
                   blitz::neverDeleteData,fortranArray);
  }

};


This doesn't even want to compile, because from_python.hpp doesn't seem
to exist.

Or should I wait a couple of days until extractors are implemented and
the dust has settled?

Cheers,

Peter








More information about the Cplusplus-sig mailing list