[PYTHON MATRIX-SIG] C API questions

Konrad Hinsen hinsen@ibs.ibs.fr
Mon, 4 Nov 96 10:34:19 +0100


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.

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?

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.

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.

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

Konrad.
-- 
-------------------------------------------------------------------------------
Konrad Hinsen                          | E-Mail: hinsen@ibs.ibs.fr
Laboratoire de Dynamique Moleculaire   | Tel.: +33-76.88.99.28
Institut de Biologie Structurale       | Fax:  +33-76.88.54.94
41, av. des Martyrs                    | Deutsch/Esperanto/English/
38027 Grenoble Cedex 1, France         | Nederlands/Francais
-------------------------------------------------------------------------------

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

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