[Numpy-discussion] NumPy initiated reference counting

Ralf Juengling juenglin at informatik.uni-freiburg.de
Sat Apr 20 09:59:13 EDT 2002

I'm currently tinkering with the following problem and what like to
hear your suggestions:

Within a C module I define a new Python type 'IM' (representing an
The indexing or slicing facilities of NumPy arrays were tailormade 
for the manipulation of the internal data of its instances. Thus,
I could provide a method 'asarray', which creates a properly
typed array object 'a' referring to the data of an IM instance 'im':

a = im.asarray()

I could use PyArray_FromDimsAndData() to create the array instance.
Unfortunately, this wouldn't work, since 'a' would not get notified 
about the death of 'im'.
However, if I could prevent 'im' from being garbage collected before
all array instances referring to its data are deleted, it should work.

NumPy's array type uses a mechanism to prevent garbage collection
of array instances if there are other instances that share data with
it. My idea was, to use this mechanism, that is to let the asarray
method increment im's reference count and let a->base refer to im.

Do you think this is a reliable approach?


Ralf Jüngling
Institut für Informatik - Lehrstuhl für Mustererkennung &
Gebäude 52                                        Tel:
79110 Freiburg                                    Fax:

More information about the NumPy-Discussion mailing list