Embedded Python/NumPy and reference counts

Nigel Wade nmw at ion.le.ac.uk
Fri Oct 6 10:07:25 EDT 2000


Hi,

I'm in the process of embedding Python in an application of mine, 
and want to be able to transfer Numerical Python arrays from the
application to the interpreter.

In essence what I need to do is initialize the interpreter, 
create a local dictionary, then loop repeatedly passing an array from 
C to the interpreter in the same item in the local dictionary.

It looks something like this:

int dims[1];
PyObject *locals, *numpy_array;
float *ptr;

Py_Initialize();
PyImport_ImportModule("Numeric");
locals = Py_DictNew()


loop {
   ptr = pointer to a C data array

   dims[0] = length of C data array;   
   numpy_array = 
      PyArray_FromDimsAndData(1, dims, PyArray_CFLOAT, (char *)ptr)

   PyDict_SetItemString(locals, "d_data", numpy_array)

   PyRun_String("print d_data", Py_single_input, NULL, locals)
}

Now, what I am not sure about is what to do about numpy_array and
the d_data item in the locals dictionary each time around the loop.

After I have called PyDict_SetItemString has the reference count on 
the object numpy_array been incremented, so I can call 
Py_DECREF(numpy_array) without losing d_data in the locals dictionary?

After I have called PyRun_String if I call 

PyDict_DelItemString(locals,"d_data")
 
will this remove all remaining references to d_data? Also, what 
happens to the data to which the numpy_array points? Is it
free'd, or is it up to me to free it?

For my purposes it would be much more efficient if I created 
numpy_array and d_data once and then manipulated the internal
values of dimensions and the data pointer for numpy_array.
But I don't really like doing this because messing around in the 
internal structure seems like a good way to cause future maintenance
headaches. Is there any defined interface for doing something
like this? I can't find anything in the Numerical Python manual.


-- 
-----------------------------------------------------------
Nigel Wade, System Administrator, Space Plasma Physics Group,
            University of Leicester, Leicester, LE1 7RH, UK 
E-mail :    nmw at ion.le.ac.uk 
Phone :     +44 (0)116 2523568, Fax : +44 (0)116 2523555



More information about the Python-list mailing list