[PyQt] since SIP 4.10: closures loosing outer variables!

Wilbert Berendsen wbsoft at xs4all.nl
Sat Feb 13 21:47:35 GMT 2010


since my system upgraded SIP to 4.10 (and KDE to 4.4) my own PyKDE4 app 
Frescobaldi has strange garbage collection issues all over the place, with 
error messages like:

NameError: free variable 'docGroup' referenced before assignment in enclosing 

It seems that when I connect a local or lambda function to a Qt signal, the 
function remains alive but it looses the references its outer variables! The 
objects those outer variables point to are still there (they work as usual), 
but they just become unreachable from local or lambda functions. 

I can circumvent the problem by passing in those outer variables as default 
arguments, but that is kind of clumsy.

How come the outer variables are garbage collected but the function containing 
them not? Is this a bug in SIP or PyQt or should I adapt a different coding 
style? (i.e. without anonymous or local functions) ? ...

w best regards,
Wilbert Berendsen

"You must be the change you wish to see in the world."
        -- Mahatma Gandhi

More information about the PyQt mailing list