PyQt事件处理程序snarf异常

编程入门 行业动态 更新时间:2024-10-07 16:23:21
本文介绍了PyQt事件处理程序snarf异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

以下是说明问题的代码:

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异常

本文发布于:2023-11-05 10:33:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1560597.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:异常   事件   程序   PyQt   snarf

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!