[Numpy-discussion] Creating PyArrayObject in C++

Kenny Abernathy kenny.abernathy@gmail....
Tue Jul 7 12:18:14 CDT 2009


I've built an analysis framework for studying electrophysiogical data, and
I'd like to add NumPy (and SciPy, although that's not germane to my current
question) as an engine so users can more easily add their own functions to
the program.  I'm at the point now of trying to share the data between the
C++ program and the python interpreter.  Keep in mind, these can be large
datasets (some of our data is sampled at 40,000 Hz), so I'd like to minimize
copying data as much as possible.

My first solution is to use PyArray_FromDimsAndData().  I know that it's not
recommended, but the data is owned by an object that takes care of freeing
it, so it would look like the following:

class Unit()
{
public:
  Unit(int size, int *data) : data_(data), pyArray_(0)
  {
    pyArray_ = PyArrayFromDimsAndData(1, &size, PyArray_INT, (char*)data_);
  }
}

  ~Unit()
  {
    Py_DECREF(pyArray_);
    delete[] data_;
  }

private:
  int *data_;
  PyArrayObject* pyArray_;
};

I can guarantee that all analysis will be finished before the Unit object is
destroyed and delete[] is called, so I don't think that's a problem.
 However, what if there are modifications to the array from within a Python
script (specifically changes to the number of elements in the array).  I
should be able to get the new size from PyArrayObject, but will it have any
effect on the delete[] operation?  Is there a chance that PyArrayObject.data
could change from within a Python script, necessitating code such as:

if (data_ == pyArray_.data) {
  delete[] data_;
}
else {
  delete[] data_;
  delete[] pyArray_.data);
}

Or is there a better way to do this without expensive copy operations that I
haven't come across yet?

Thanks,

Kenny
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20090707/5832eb99/attachment.html 


More information about the NumPy-Discussion mailing list