Impossible Python Error (garbage collection problem with slot?)
However, I seem to be experiencing an error which I thought would actually be impossible within python, based on a variable in the function scope apparently being deleted, even when a reference to it is retained.
The paradox is this; mainFrame is defined directly within the function scope shown below, and in order for the event subscription to be successfully executed, mainFrame must therefore be properly set. However, when the jsbinder() function is triggered by the subscribed event being fired by the mainFrame, at that time mainFrame is no longer set, and a NameError is fired. No other reference to the mainFrame name exists, so it's impossible that I'm deleting it elsewhere.
I think it must be a garbage collection artifact to do with the indirection of PyQt signal mechanisms. Any ideas for workarounds.
def view(self, view):
self._view = view
if self._view != None:
mainFrame = self._view.page().mainFrame()
The error fired is...
init.py", line 131, in jsbinder
NameError: free variable 'mainFrame' referenced before assignment in enclosing scope
I've just encountered a similar error in my code. Looks like it is similar to what is described "here":http://www.daa.com.au/pipermail/pygtk/2008-August/015767.html.
I followed the suggestion there to work around it by providing the local variables as default arguments to the function, in your case:
It looks like the general bug "was fixed a long time ago":http://www.riverbankcomputing.com/hg/sip/rev/8bf735cda5bf, but there are still corner cases where it may happen.