Sure, I'll elaborate a bit more... PETSc objects are refcounted,
however there is no gc features in PETSc. I have classes that are
proxies to the actual PETSc objects, and the methods on that classes
internally call the PETSc C API. From now, when I said 'PETSc object',
it is a pointer to an opaque C structure; when I said 'proxy class',
it is 'cdef' class having a PETSc object, like this:

cdef class Mat: # <- proxy class
   cdef PetscMat # <- PETSc object, a pointer to a refcounted struct.


I need to store arbitrary Python objects inside the PETSc object, I'm
actually storing a dictionary. This dictionary have to survive until
the PETSc object is deallocated, but that deallocation do not
necesarily occurs when the instance of my proxy class is deallocated,
because the PETSc object can still 'live' (that is, a referece being
owned) inside other PETSc objects.

Then, as the PETSc object contains a Python dictionary, I need to
'visit' that dictionary in the 'tp_visit' of my proxy class (of
course, I first need to make a call to get the dict from the PETSc
object, then visit it). Furthermore, I need to release the ownership
(decref) my PETSc object inside the 'tp_clear' of my proxy class.

I've implemented this machinery some time ago for my SWIG-based
implementations, in a hand-written C type object, and all works just
fine. I do not know if there is any other way of doing this. If not, I
endup 'leaking' PETSc objects, some of them with big memory
footprints.

Perhaps you think that my approach is over-complicated. But my intent
is that in the near future petsc4py will be more than a PETSc wrapper
for Python level consumption. I want to become Python an extension
language for PETSc, were a user with codes in C/C++/Fortran can
implement some stuff, like the loops of a custom nonlinear solver, in
Python, and next use it from C/C++/Fortran with just passing a command
line flag. This feature is working in the former SWIG-based version,
now I want the new Cython-based version to become even more featured.

I hope I was clear enough about my needs, it's not easy to explain.
Perhaps, in short, we should think about this scenario:

* We have a object-oriented C/C++ library wich let you put arbitrary
data inside some refcounted C structs or C++ classed.

* Then you can put any Python object in the C/C++ level objects, in
particular, you can put mutable containers, like a list or a dict.

* Now you want to wrap that library with Cython, by implementing
classes that are proxies to the C/C++ object.

Then, the question is: how do you avoid circular references? Short
answer: you cannot. You need gc to mitigate the problem, then you need
that your proxies can be able to traverse the contained stored inside
the C/C++ level object.

Is there any other way?




On 5/31/08, Stefan Behnel <[EMAIL PROTECTED]> wrote:
> Hi,
>
>
>  Lisandro Dalcin wrote:
>  > In short, I want to be able to add some special method to my objects,
>  > and then Cython will always generate gc support (regardeless if my
>  > cdef class has or do not have python attributes). In the generated
>  > tp_visit and tp_clear, Cython work as current, visiting/clearing
>  > python attributes, but near the end of these function, it calll the
>  > user-defined special method.
>
>
> Could you explain a bit what you are going to do in these methods?
>
>  Stefan
>
>  _______________________________________________
>  Cython-dev mailing list
>  [email protected]
>  http://codespeak.net/mailman/listinfo/cython-dev
>


-- 
Lisandro Dalcín
---------------
Centro Internacional de Métodos Computacionales en Ingeniería (CIMEC)
Instituto de Desarrollo Tecnológico para la Industria Química (INTEC)
Consejo Nacional de Investigaciones Científicas y Técnicas (CONICET)
PTLC - Güemes 3450, (3000) Santa Fe, Argentina
Tel/Fax: +54-(0)342-451.1594
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to