Impossible Python Error (garbage collection problem with slot?)

  • I'm using the property-setting code below, to ensure that every time a QWebView 'mainFrame' is reloaded, that the new javascript context is provided once again with an object for future callbacks. This is as suggested in the documentation.

    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:
    self._view.settings().setAttribute(QWebSettings.DeveloperExtrasEnabled, True)
    mainFrame =
    def jsbinder():

    The error fired is...", 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":

    I followed the suggestion there to work around it by providing the local variables as default arguments to the function, in your case:
    @def jsbinder(mainFrame=mainFrame):@

    It looks like the general bug "was fixed a long time ago":, but there are still corner cases where it may happen.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.