[Pyrex] __dealloc__ (was: Problems with pyrex and glib)

Drirr miston.drirr at gmail.com
Mon Jan 9 00:13:00 CET 2006


Toby wrote:

> The culprit is calling Python stuff from __dealloc__. Quoting Greg:
>
>	You need to be careful what you do in a __dealloc__ method.
>	[...] you should avoid invoking any Python operations [...] 
>	It's best if you stick to just deallocating C data.
>
>In fact, taking Python operations (such as print and the chain of stuff
>called from print) out of __dealloc__ seems to solve the problem:
>
>	cdef extern int puts(char* s)
>
>	cdef class Container:
>	   def __init__(self):
>	       print "Init"
>
>	   def __dealloc__(self):
>	       puts("Dealloc")
>
>	global_context = Container()
>  
>
Thanks, it works now. I had read that paragraph in the docs once before 
and only remembered the "don't call any other methods of the object or 
do anything which might cause the object to be resurrected." part, it 
never occurred to me "print" would cause any trouble.

Now I have another question. Is there something similar to __dealloc__ 
but for the whole module? I want to do a cleanup once python decides the 
module's no longer needed (like at program exit).

Thanks!
Drirr.



More information about the Pyrex mailing list