[PYTHON MATRIX-SIG] C API questions

Doug Heisterkamp drh@cse.unl.edu
Mon, 4 Nov 1996 14:50:52 -0600 (CST)


> For the first time I have to use the C API to the array objects in a
> non-trivial way, and I'd like to profit a bit from the expertise of
> those who have done it before.
> 
> I want to provide a C interface to functions written in Python, which
> means I have to construct array objects referring to a given C array
> of arbitrary origin. This raises a few questions about memory allocation.
> and reference counting.
> 

I'm am not an expert on the C API, but I am doing similar things.  I'll
relate what I'm currently doing in the hope that I'll be corrected
if it is not right.

> 1) How should I construct the array object to make sure that Python
>    will not try to allocate or free memory for the actual data?

I am using PyArray_FromDimsAndData.  If data is passed into this function,
then the flag OWN_DATA is not set.  In array_dealloc, if OWN_DATA is not
set, then the data pointer of the array object is not freed.
 
> 
> 2) Should my code simply pass array objects to the Python code and
>    then forget about them, or should it increase the reference count
>    before the call and decrement it afterwards? I suppose that if
>    everything goes well that shouldn't make a difference.

I decrement the reference to the array object after the python function
call.  I do not increment it before the function call.  The array
object reference count was incremented in the PyArray_FromDimsAndData
function by its call to PyObject_NEW (I think--not very sure on some of
this stuff).

> 
> 3) What should I do to prevent the Python code from keeping a reference
>    to the arrays even after returning (e.g. by putting it into a
>    global list)? Is there any legitimate reason to do this? Should I
>    check the reference count after the call and raise an exception if
>    it is too high? I must somehow make sure that the temporary array
>    object will be deleted, since I can't guarantee for the existence
>    of the data part after my function finishes its work.
> 

I have not done this, but I should.  Checking the reference count and 
raising an exception should work in most cases. 

> 4) Is there any other problem I should watch out for?

Just the typical stuff: remember to DECREF the argument list if you
created it with Py_BuildValue, remember the DECREF the results returned
from the python function when you are done with them, and if calling 
from a Fortran function remember to reverse the indexing order when
building the array object.


Doug Heisterkamp
drh@cse.unl.edu



=================
MATRIX-SIG  - SIG on Matrix Math for Python

send messages to: matrix-sig@python.org
administrivia to: matrix-sig-request@python.org
=================