Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. Impossible Python Error (garbage collection problem with slot?)

Impossible Python Error (garbage collection problem with slot?)

Scheduled Pinned Locked Moved Language Bindings
2 Posts 2 Posters 1.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cefn
    wrote on last edited by
    #1

    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.

    @@view.setter
    def view(self, view):
    self._view = view
    if self._view != None:
    self._view.settings().setAttribute(QWebSettings.DeveloperExtrasEnabled, True)
    mainFrame = self._view.page().mainFrame()
    def jsbinder():
    mainFrame.addToJavaScriptWindowObject('logx',self.javascriptnames)
    mainFrame.javaScriptWindowObjectCleared.connect(jsbinder)
    @

    The error fired is...

    init.py", line 131, in jsbinder
    mainFrame.addToJavaScriptWindowObject('logx',self.javascriptnames)
    NameError: free variable 'mainFrame' referenced before assignment in enclosing scope

    1 Reply Last reply
    0
    • U Offline
      U Offline
      urimsh
      wrote on last edited by
      #2

      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:
      @def jsbinder(mainFrame=mainFrame):@

      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.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved