From 062a0e3e68fef4f6c927442daf35a3e47e72cd8a Mon Sep 17 00:00:00 2001 From: Ofir Date: Tue, 19 Sep 2017 15:14:08 +0300 Subject: [PATCH 1/4] If an exception happens while loading a plugin, PyTest no longer hides the original traceback. In python2 it will show the original traceback with a new message that explains in which plugin. In python3 it will show 2 canonized exceptions, the original exception while loading the plugin in addition to an exception that PyTest throws about loading a plugin. --- _pytest/config.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/_pytest/config.py b/_pytest/config.py index 690f98587..595bebe35 100644 --- a/_pytest/config.py +++ b/_pytest/config.py @@ -423,12 +423,12 @@ class PytestPluginManager(PluginManager): try: __import__(importspec) except ImportError as e: - new_exc = ImportError('Error importing plugin "%s": %s' % (modname, safe_str(e.args[0]))) - # copy over name and path attributes - for attr in ('name', 'path'): - if hasattr(e, attr): - setattr(new_exc, attr, getattr(e, attr)) - raise new_exc + new_exc_type = ImportError + new_exc_message = 'Error importing plugin "%s": %s' % (modname, safe_str(e.args[0])) + new_exc = new_exc_type(new_exc_message) + + six.reraise(new_exc_type, new_exc, sys.exc_info()[2]) + except Exception as e: import pytest if not hasattr(pytest, 'skip') or not isinstance(e, pytest.skip.Exception): From c89827b9f2b42352a388fa1a013f6b51d0f33cb8 Mon Sep 17 00:00:00 2001 From: Ofir Date: Tue, 19 Sep 2017 15:23:07 +0300 Subject: [PATCH 2/4] updating import plugin error test in order to make sure it also checks that the original traceback has been shown to the users --- testing/test_pluginmanager.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/testing/test_pluginmanager.py b/testing/test_pluginmanager.py index 2838f83c5..55e2ea10f 100644 --- a/testing/test_pluginmanager.py +++ b/testing/test_pluginmanager.py @@ -199,12 +199,17 @@ def test_importplugin_error_message(testdir, pytestpm): testdir.syspathinsert(testdir.tmpdir) testdir.makepyfile(qwe=""" # encoding: UTF-8 - raise ImportError(u'Not possible to import: ☺') + def test_traceback(): + raise ImportError(u'Not possible to import: ☺') + test_traceback() """) with pytest.raises(ImportError) as excinfo: pytestpm.import_plugin("qwe") - expected = '.*Error importing plugin "qwe": Not possible to import: .' - assert py.std.re.match(expected, str(excinfo.value)) + + expected_message = '.*Error importing plugin "qwe": Not possible to import: .' + expected_traceback = ".*in test_traceback" + assert py.std.re.match(expected_message, str(excinfo.value)) + assert py.std.re.match(expected_traceback, str(excinfo.traceback[-1])) class TestPytestPluginManager(object): From b57a84d065846076ab8df6316700ff2ecb0903aa Mon Sep 17 00:00:00 2001 From: Ofir Date: Tue, 19 Sep 2017 15:36:12 +0300 Subject: [PATCH 3/4] updating bugfix changelog --- changelog/2491.bugfix | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 changelog/2491.bugfix diff --git a/changelog/2491.bugfix b/changelog/2491.bugfix new file mode 100644 index 000000000..0516b0b5e --- /dev/null +++ b/changelog/2491.bugfix @@ -0,0 +1,4 @@ +If an exception happens while loading a plugin, PyTest no longer hides the original traceback. +In python2 it will show the original traceback with a new message that explains in which plugin. +In python3 it will show 2 canonized exceptions, the original exception while loading the plugin +in addition to an exception that PyTest throws about loading a plugin. \ No newline at end of file From d96869ff66def703c410c4ab258864997adddbd7 Mon Sep 17 00:00:00 2001 From: OfirOshir Date: Wed, 20 Sep 2017 09:45:40 +0300 Subject: [PATCH 4/4] fixing cr --- changelog/2491.bugfix | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/changelog/2491.bugfix b/changelog/2491.bugfix index 0516b0b5e..fbb16e17e 100644 --- a/changelog/2491.bugfix +++ b/changelog/2491.bugfix @@ -1,4 +1,4 @@ -If an exception happens while loading a plugin, PyTest no longer hides the original traceback. +If an exception happens while loading a plugin, pytest no longer hides the original traceback. In python2 it will show the original traceback with a new message that explains in which plugin. In python3 it will show 2 canonized exceptions, the original exception while loading the plugin -in addition to an exception that PyTest throws about loading a plugin. \ No newline at end of file +in addition to an exception that PyTest throws about loading a plugin.