I'm using twisted with GTK, and the following code runs when a connection could not be established:
def connectionFailed(self, reason):
#show a "connect failed" dialog
dlg = gtk.MessageDialog(
type=gtk.MESSAGE_ERROR,
buttons=gtk.BUTTONS_CLOSE,
message_format="Could not connect to server:\n%s" % (
reason.getErrorMessage()))
responseDF = defer.Deferred()
dlg.set_title("Connection Error")
def response(dialog, rid):
dlg.hide_all()
responseDF.callback(rid)
dlg.connect("response", response)
dlg.show_all()
self.shutdownDeferreds.append(responseDF)
self.shutdownDeferreds
is a list of deferreds that is set up so that the reactor does not stop until they are all called.
Now, I happened to press CTRL+C
at the same time as the connection failed. The dialog did pop up, but when I press Close
, I get:
Traceback (most recent call last)开发者_开发问答:
File "C:\Users\DrClaud\bumhunter\gui\controller.py", line 82, in response
dlg.hide_all()
NameError: free variable 'dlg' referenced before assignment in enclosing scope
Traceback (most recent call last):
File "C:\Users\DrClaud\bumhunter\gui\controller.py", line 82, in response
dlg.hide_all()
NameError: free variable 'dlg' referenced before assignment in enclosing scope
Any ideas why that might happen?
Shouldn't that be:
def response(dialog, rid):
dialog.hide_all()
responseDF.callback(rid)
or really, for clarity,
def response(self, rid):
self.hide_all()
responseDF.callback(rid)
(I might be wrong about this, I've done barely any GTK.) If so, the problem is that you are referencing dlg
in the function, which makes it a closure (it captures dlg
from its surrounding scope). The KeyboardInterrupt
will cause weird and wonderful behaviour, because it could destroy that scope.
精彩评论