Boost logo

Boost :

From: John Barnard (barnard_at_[hidden])
Date: 2001-01-04 23:07:11


I'm trying to add a method to a wrapped valarray<double> class
(DVArray) that creates a Python NumPy array that shares the valarray
instance's data (allowing me to easily perform numerical operations on
the valarray data in Python). I've written a function, given below,
that does most of the job, but it doesn't increment the reference
count of the wrapped DVArray instance.

  PyObject* to_array(DVArray& self) {
    PyArrayObject *arr;
    int n;
  
    n = self.size();
    arr = (PyArrayObject *) \
      PyArray_FromDimsAndData(1, &n, PyArray_DOUBLE, (char
      *)(&self[0]));
    if (arr == NULL) return NULL;
    arr->flags |= OWN_DATA;
    PyArray_INCREF(arr);
    return (PyObject *) arr;
  }

to_array gets added as a method of the wrapped class as follows (in
the module init function):

      boost::python::class_builder<grid_python::DVArray>
      DVArrayPC(grid, "DVArray");
      DVArrayPC.def(boost::python::constructor<>());
      DVArrayPC.def(&grid_python::DVArray::size, "__len__");
      DVArrayPC.def(grid_python::to_array, "toarray");

How do I access the reference count of the wrapped DVArray instance? I
would also like to gain access to the PyObject* of the wrapped
instance so I can make it the base for the NumPy array instance (and
subsequently have the reference count of the wrapped DVArray decrease
when the NumPy array is destroyed).

Thanks,

John

-- 
John Barnard
Assistant Professor
Department of Statistics
Harvard University
Phone: (617) 495-1603
Fax:   (617) 496-8057
Email: barnard_at_[hidden]

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk