以下是说明问题的代码:
Here's the code that illustrates the problem:
from PyQt4 import QtGui app = QtGui.QApplication([]) dialog = QtGui.QDialog() button = QtGui.QPushButton('I crash') layout = QtGui.QHBoxLayout() layout.addWidget(button) dialog.setLayout(layout) def crash(): raise Exception('Crash!') button.clicked.connect(crash) button.click() print 'I should not happen'当我运行它,PyQt4处理我的错误。我的控制台在其中显示一个堆栈跟踪,其中包含Crash!等,我看到我不应该发生。
When I run that, PyQt4 handles the error for me. My console displays a stack trace with 'Crash!' etc. in it, and I see 'I should not happen'.
这没有用,因为我被许多处理程序提交了大量的应用程序,我需要将所有的错误强制到我的脸上(并进入我的自动化测试)。每次运行时,错误都会逃离我的网络,而且它们需要过多的无用的尝试:除了每个处理程序中的块之外,只需要抓住它们。
This is not useful, because I've been handed a large application with many handlers, and I need to force all their errors up into my face (and into my - ahem - automated tests). Each time I run, errors escape my nets, and they would require excessive and useless try:except blocks, inside every handler, just to catch them all.
换句话说,我希望好的代码是非常好的,坏的代码是非常糟糕的。不要粉饰。
Put another way, I want good code to be very good, and bad code to be very bad. Not whitewashed.
如果这是已经被问到,抱歉,但是当我搜索它时,我自然会得到成千上万的新手来询问基本的错误处理问题(或者更糟的是,我得到新手询问如何关闭他们的方式异常!;)
Apologies if this is already asked, but when I e-search for it, I naturally get thousands of newbies asking basic error handling questions (or, worse, I get newbies asking how to turn OFF their wayward exceptions!;)
如何覆盖PyQt4的默认错误处理,所以我可以自己传播或记录错误?请不要回答sys.excepthook,它会捕获PyQt4 不会捕获的错误。
How do I override PyQt4's default error handling, so I can propagate or log errors myself? And please don't answer sys.excepthook, either - it catches the errors that PyQt4 doesn't catch.
推荐答案我认为答案是,这不是 PyQt 的功能,而是让信号/插槽工作的设计所固有的结果(记住信号/插槽通信也正在通过c ++层)。
I think the answer is that this isn't a 'feature' of PyQt, but a consequence inherent to the design that lets signals/slots work (remember that the signal/slot communication is going through a c++ layer as well).
这是丑陋的,但是在您的问题上做了一些并且终止运行
This is ugly, but does a bit of and end-run around your problem
from PyQt4 import QtGui import time app = QtGui.QApplication([]) class exception_munger(object): def __init__(self): self.flag = True self.txt = '' self.type = None def indicate_fail(self,etype=None, txt=None): self.flag = False if txt is not None: self.txt = txt self.type = etype def reset(self): tmp_txt = self.txt tmp_type = self.type tmp_flag = self.flag self.flag = True self.txt = '' self.type = None return tmp_flag, tmp_type, tmp_txt class e_manager(): def __init__(self): self.old_hook = None def __enter__(self): em = exception_munger() def my_hook(type, value, tback): em.indicate_fail(type, value) sys.__excepthook__(type, value, tback) self.old_hook = sys.excepthook sys.excepthook = my_hook self.em = em return self def __exit__(self,*args,**kwargs): sys.excepthook = self.old_hook def mang_fac(): return e_manager() def assert_dec(original_fun): def new_fun(*args,**kwargs): with mang_fac() as mf: res = original_fun(*args, **kwargs) flag, etype, txt = mf.em.reset() if not flag: raise etype(txt) return res return new_fun @assert_dec def my_test_fun(): dialog = QtGui.QDialog() button = QtGui.QPushButton('I crash') layout = QtGui.QHBoxLayout() layout.addWidget(button) dialog.setLayout(layout) def crash(): time.sleep(1) raise Exception('Crash!') button.clicked.connect(crash) button.click() my_test_fun() print 'should not happen'这不会打印不应该发生,并给你一些东西赶上你的自动化测试使用正确的异常类型)。
This will not print 'should not happen' and gives you something to catch with your automated tests (with the correct exception type).
In [11]: Traceback (most recent call last): File "/tmp/ipython2-3426rwB.py", line 68, in crash Exception: Crash! --------------------------------------------------------------------------- Exception Traceback (most recent call last) <ipython-input-11-6ef4090ab3de> in <module>() ----> 1 execfile(r'/tmp/ipython2-3426rwB.py') # PYTHON-MODE /tmp/ipython2-3426rwB.py in <module>() /tmp/ipython2-3426rwB.py in new_fun(*args, **kwargs) Exception: Crash! In [12]:堆栈跟踪被提升,但您仍然可以阅读打印出来的第一个。
The stack trace is jacked up, but you can still read the first one that was printed out.
更多推荐
PyQt事件处理程序snarf异常
发布评论