PyQt 事件处理程序 snarf 异常 [英] PyQt event handlers snarf exceptions

查看:27
本文介绍了PyQt 事件处理程序 snarf 异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是说明问题的代码:

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 会为我处理错误.我的控制台显示带有崩溃!"的堆栈跟踪等等,我看到我不应该发生".

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'.

这没有用,因为我收到了一个包含许多处理程序的大型应用程序,我需要强制所有他们的错误出现在我面前(以及我的 - 咳咳 - 自动化测试).每次我运行时,错误都会从我的网络中逃脱,并且它们需要过多且无用的 try:except 块,在每个处理程序中,只是为了将它们全部捕获.

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.

推荐答案

这不是答案,你这个愚蠢的网站.不要强迫我们适应理想化线程模板的先入为主的观念.

This is not the answer, you silly website. Stop forcing us to fit into a preconceived notion of an idealized thread template.

答案是使用我的测试框架 setUp() 来挂钩异常处理程序:

The un-answer is to use my test framework setUp() to hook in an exception handler:

def setUp(self):
    self.no_exceptions = True

    def testExceptionHook(type, value, tback):
        self.no_exceptions = False
        sys.__excepthook__(type, value, tback)

    sys.excepthook = testExceptionHook

在说self.no_exceptions = False"的地方,我宁愿简单地说 self.fail('').然而,因为 Python 的单元测试库坚持抛出异常只是为了注册测试失败,并且因为 PyQt 坚持捕捉所有异常,我们陷入了死锁.

Where that says "self.no_exceptions = False", I would much rather simply say self.fail(''). However, because Python's unit test library insists on throwing exceptions just to register test failures, and because PyQt insists on snarfing all exceptions, we have a deadlock.

为了适应 unittest.TestCase 的理想化测试用例的愚蠢先入为主的概念,我必须改为设置一个变量,然后在拆卸中检测它:

To fit into unittest.TestCase's silly preconceived notion of an idealized test case, I have to instead set a variable, then detect it in the teardown:

def tearDown(self):
    self.assertTrue(self.no_exceptions)

这仍然不理想,但至少它会迫使我花更多时间关注错误,而不是花时间在技术网站上抱怨它们.

This is still not ideal, but at least it will force me to spend more time paying attention to the errors, instead of spending that time complaining about them on technical websites.

根本问题:如何关闭 PyQt 的魔法错误处理程序?- 仍未得到答复...

The root question: How to turn off PyQt's magic error handler? - remains unanswered...

这篇关于PyQt 事件处理程序 snarf 异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆