From 7b385522cb697ce409957c948ef860d4c20a46db Mon Sep 17 00:00:00 2001 From: fabioz Date: Tue, 5 Apr 2016 20:28:19 -0300 Subject: [PATCH 1/4] When an exception is caught in sys.excepthook, still print it to stderr. --- .project | 17 +++++++++++++++++ .pydevproject | 8 ++++++++ README.rst | 5 +++++ pytestqt/exceptions.py | 1 + tests/test_exceptions.py | 34 +++++++++++++++++++++++++++++++++- 5 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 .project create mode 100644 .pydevproject diff --git a/.project b/.project new file mode 100644 index 00000000..9a9fb502 --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + pytest-qt + + + + + + org.python.pydev.PyDevBuilder + + + + + + org.python.pydev.pythonNature + + diff --git a/.pydevproject b/.pydevproject new file mode 100644 index 00000000..a946dfc2 --- /dev/null +++ b/.pydevproject @@ -0,0 +1,8 @@ + + + +/${PROJECT_DIR_NAME} + +python 2.7 +Default + diff --git a/README.rst b/README.rst index 32d649fa..f556dc36 100644 --- a/README.rst +++ b/README.rst @@ -149,6 +149,11 @@ Many thanks to: .. |pycharm| image:: https://www.jetbrains.com/pycharm/docs/logo_pycharm.png :target: https://www.jetbrains.com/pycharm +.. |pydev| image:: http://www.pydev.org/images/pydev_banner3.png + :target: https://www.pydev.org + |pycharm| +|pydev| + .. _tox: http://tox.readthedocs.org diff --git a/pytestqt/exceptions.py b/pytestqt/exceptions.py index f22cb2cc..90018a8b 100644 --- a/pytestqt/exceptions.py +++ b/pytestqt/exceptions.py @@ -34,6 +34,7 @@ def start(self): """ def hook(type_, value, tback): self.exceptions.append((type_, value, tback)) + sys.stderr.write(format_captured_exceptions([(type_, value, tback)])) self.old_hook = sys.excepthook sys.excepthook = hook diff --git a/tests/test_exceptions.py b/tests/test_exceptions.py index bacd7919..1c98839c 100644 --- a/tests/test_exceptions.py +++ b/tests/test_exceptions.py @@ -1,6 +1,6 @@ import pytest import sys -from pytestqt.exceptions import format_captured_exceptions +from pytestqt.exceptions import format_captured_exceptions, capture_exceptions @pytest.mark.parametrize('raise_error', [False, True]) @@ -244,3 +244,35 @@ def event(self, ev): qapp.processEvents() assert [str(e) for (t, e, tb) in exceptions] == ['mistakes were made'] + + +def test_exceptions_to_stderr(qapp): + """ + Exceptions should still be reported to stderr. + """ + try: + from io import StringIO + except: + from StringIO import StringIO + old = sys.stderr + new = sys.stderr = StringIO() + + try: + called = [] + from pytestqt.qt_compat import QWidget, QEvent + + class MyWidget(QWidget): + + def event(self, ev): + called.append(1) + raise RuntimeError('event processed') + + w = MyWidget() + with capture_exceptions() as exceptions: + qapp.postEvent(w, QEvent(QEvent.User)) + qapp.processEvents() + assert called + del exceptions[:] + assert "raise RuntimeError('event processed')" in new.getvalue() + finally: + sys.stderr = old From f9288485b4bbd942ec7baa2add4fb319d52b58f0 Mon Sep 17 00:00:00 2001 From: Fabio Zadrozny Date: Wed, 6 Apr 2016 07:55:37 -0300 Subject: [PATCH 2/4] Using capsys to handle stderr capture. --- tests/test_exceptions.py | 41 ++++++++++++++++------------------------ 1 file changed, 16 insertions(+), 25 deletions(-) diff --git a/tests/test_exceptions.py b/tests/test_exceptions.py index 1c98839c..4a5929ff 100644 --- a/tests/test_exceptions.py +++ b/tests/test_exceptions.py @@ -1,6 +1,6 @@ +from pytestqt.exceptions import capture_exceptions, format_captured_exceptions import pytest import sys -from pytestqt.exceptions import format_captured_exceptions, capture_exceptions @pytest.mark.parametrize('raise_error', [False, True]) @@ -246,33 +246,24 @@ def event(self, ev): assert [str(e) for (t, e, tb) in exceptions] == ['mistakes were made'] -def test_exceptions_to_stderr(qapp): +def test_exceptions_to_stderr(qapp, capsys): """ Exceptions should still be reported to stderr. """ - try: - from io import StringIO - except: - from StringIO import StringIO - old = sys.stderr - new = sys.stderr = StringIO() - - try: - called = [] - from pytestqt.qt_compat import QWidget, QEvent + called = [] + from pytestqt.qt_compat import QWidget, QEvent - class MyWidget(QWidget): + class MyWidget(QWidget): - def event(self, ev): - called.append(1) - raise RuntimeError('event processed') + def event(self, ev): + called.append(1) + raise RuntimeError('event processed') - w = MyWidget() - with capture_exceptions() as exceptions: - qapp.postEvent(w, QEvent(QEvent.User)) - qapp.processEvents() - assert called - del exceptions[:] - assert "raise RuntimeError('event processed')" in new.getvalue() - finally: - sys.stderr = old + w = MyWidget() + with capture_exceptions() as exceptions: + qapp.postEvent(w, QEvent(QEvent.User)) + qapp.processEvents() + assert called + del exceptions[:] + _out, err = capsys.readouterr() + assert "raise RuntimeError('event processed')" in err From 75c090ab0b6f71c2d06c47451a91287fec78fc2e Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Thu, 7 Apr 2016 19:26:25 -0300 Subject: [PATCH 3/4] Fix 'docs' toxenv --- tox.ini | 1 + 1 file changed, 1 insertion(+) diff --git a/tox.ini b/tox.ini index cedf99eb..fc80523c 100644 --- a/tox.ini +++ b/tox.ini @@ -23,6 +23,7 @@ passenv=DISPLAY XAUTHORITY USERNAME basepython=python2.7 deps=pytest sphinx + sphinx_rtd_theme changedir=docs setenv= READTHEDOCS=True From 0c1883402e0bb1cd2d62a089fd1811b07da18a6a Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Thu, 7 Apr 2016 19:32:42 -0300 Subject: [PATCH 4/4] Add changelog entry for #126 --- CHANGELOG.rst | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 3e447522..5ed80c56 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,3 +1,12 @@ +1.12.dev +-------- + +- Exceptions caught by ``pytest-qt`` in ``sys.excepthook`` are now also printed + to ``stderr``, making debugging them easier from within an IDE. + Thanks `@fabioz`_ for the PR (`126`_)! + +.. _126: https://github.com/pytest-dev/pytest-qt/pull/126 + 1.11.0 ------