Skip to content

Commit 7b38552

Browse files
committed
When an exception is caught in sys.excepthook, still print it to stderr.
1 parent 4307653 commit 7b38552

File tree

5 files changed

+64
-1
lines changed

5 files changed

+64
-1
lines changed

.project

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>pytest-qt</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.python.pydev.PyDevBuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.python.pydev.pythonNature</nature>
16+
</natures>
17+
</projectDescription>

.pydevproject

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<?eclipse-pydev version="1.0"?><pydev_project>
3+
<pydev_pathproperty name="org.python.pydev.PROJECT_SOURCE_PATH">
4+
<path>/${PROJECT_DIR_NAME}</path>
5+
</pydev_pathproperty>
6+
<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python 2.7</pydev_property>
7+
<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">Default</pydev_property>
8+
</pydev_project>

README.rst

+5
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,11 @@ Many thanks to:
149149
.. |pycharm| image:: https://www.jetbrains.com/pycharm/docs/logo_pycharm.png
150150
:target: https://www.jetbrains.com/pycharm
151151

152+
.. |pydev| image:: http://www.pydev.org/images/pydev_banner3.png
153+
:target: https://www.pydev.org
154+
152155
|pycharm|
153156

157+
|pydev|
158+
154159
.. _tox: http://tox.readthedocs.org

pytestqt/exceptions.py

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def start(self):
3434
"""
3535
def hook(type_, value, tback):
3636
self.exceptions.append((type_, value, tback))
37+
sys.stderr.write(format_captured_exceptions([(type_, value, tback)]))
3738

3839
self.old_hook = sys.excepthook
3940
sys.excepthook = hook

tests/test_exceptions.py

+33-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pytest
22
import sys
3-
from pytestqt.exceptions import format_captured_exceptions
3+
from pytestqt.exceptions import format_captured_exceptions, capture_exceptions
44

55

66
@pytest.mark.parametrize('raise_error', [False, True])
@@ -244,3 +244,35 @@ def event(self, ev):
244244
qapp.processEvents()
245245

246246
assert [str(e) for (t, e, tb) in exceptions] == ['mistakes were made']
247+
248+
249+
def test_exceptions_to_stderr(qapp):
250+
"""
251+
Exceptions should still be reported to stderr.
252+
"""
253+
try:
254+
from io import StringIO
255+
except:
256+
from StringIO import StringIO
257+
old = sys.stderr
258+
new = sys.stderr = StringIO()
259+
260+
try:
261+
called = []
262+
from pytestqt.qt_compat import QWidget, QEvent
263+
264+
class MyWidget(QWidget):
265+
266+
def event(self, ev):
267+
called.append(1)
268+
raise RuntimeError('event processed')
269+
270+
w = MyWidget()
271+
with capture_exceptions() as exceptions:
272+
qapp.postEvent(w, QEvent(QEvent.User))
273+
qapp.processEvents()
274+
assert called
275+
del exceptions[:]
276+
assert "raise RuntimeError('event processed')" in new.getvalue()
277+
finally:
278+
sys.stderr = old

0 commit comments

Comments
 (0)