-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bugfix for issue #2491 #2762
bugfix for issue #2491 #2762
Conversation
in python2 we will reraise with the original traceback in python3 will raise 2 canonized exceptions
_pytest/config.py
Outdated
new_exc_type = ImportError | ||
new_exc_message = 'Error importing plugin "%s": %s' % (modname, safe_str(e.args[0])) | ||
|
||
# Raising the original traceback with a more informative exception |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please take a look at using six.reraise/six.raise_from
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, six.reraise
seems to be more appropriate here (six.raises_from
is the same as raise new_exc
in Python 2).
We want to preserve the original traceback as much as possible, but also want to include which plugin was the cause of the exception. After playing around this code seems to do the trick:
import sys
import six
def do_import(name):
raise ValueError('some bizarre error not related to the plugin name at all')
def import_some_plugin(name):
do_import(name)
def import_plugins():
try:
import_some_plugin("myplugin")
except Exception as e:
new_e = type(e)(str(e) + '\n' + 'Error importing pytest plugin: "myplugin"')
six.reraise(type(e), new_e, sys.exc_info()[2])
import_plugins()
I get in Python 2.7:
.tmp\foo.py:18: in <module>
import_plugins()
.tmp\foo.py:16: in import_plugins
six.reraise(type(e), new_e, sys.exc_info()[2])
.tmp\foo.py:13: in import_plugins
import_some_plugin("myplugin")
.tmp\foo.py:8: in import_some_plugin
do_import(name)
.tmp\foo.py:5: in do_import
raise ValueError('some bizarre error not related to the plugin name at all')
E ValueError: some bizarre error not related to the plugin name at all
E Error importing pytest plugin: "myplugin"
!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!
And Python 3.6:
Traceback (most recent call last):
File "C:/pytest/.tmp/foo.py", line 17, in import_plugins
import_some_plugin("myplugin")
File "C:/pytest/.tmp/foo.py", line 11, in import_some_plugin
do_import(name)
File "C:/pytest/.tmp/foo.py", line 7, in do_import
raise ValueError('some bizarre error not related to the plugin name at all')
ValueError: some bizarre error not related to the plugin name at all
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/pytest/.tmp/foo.py", line 23, in <module>
import_plugins()
File "C:/pytest/.tmp/foo.py", line 20, in import_plugins
six.reraise(type(e), new_e, sys.exc_info()[2])
File "C:\pytest\.env36\lib\site-packages\six.py", line 685, in reraise
raise value.with_traceback(tb)
File "C:/pytest/.tmp/foo.py", line 17, in import_plugins
import_some_plugin("myplugin")
File "C:/pytest/.tmp/foo.py", line 11, in import_some_plugin
do_import(name)
File "C:/pytest/.tmp/foo.py", line 7, in do_import
raise ValueError('some bizarre error not related to the plugin name at all')
ValueError: some bizarre error not related to the plugin name at all
Error importing pytest plugin: "myplugin"
What do you think @RonnyPfannschmidt? In Python 3 we might consider not adding the extra message.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use six.reraise
as suggested
…to allow better compatibility with python2 and python3
I changed the code according to your suggestions. |
Thanks @OfirOshir, LGTM now! 👍 Some things still need to be done if you can:
|
i have no problem adding a regression test. |
Add a file named
The explanation ideally should be helpful to end users to understand the issue that was fixed; technical details are not interesting to them, those should be present in the commit messages. |
I updated the test as instructed and I also added a changelog. |
Since this adds a new dependency, this should probably go to the |
testing/test_pluginmanager.py
Outdated
@@ -216,12 +216,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() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing :
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
I dont think a new dependecy is qualified as a feature. |
while i understand the sentiment in this, a new dependency requires a change to anyone using a pinned dependency set since a new dependency requires a controlled change in users, its reasonable to consider this as a "feature" (since its not breaking, but still more noticeable than just a bugfix) |
@RonnyPfannschmidt is right, and sorry @OfirOshir for not realizing this earlier. In fact, the Unfortunately I think you will need to rebase your commits on |
@OfirOshir Did you intend to close this? |
created a new pr from feature branch, #2791 |
in python2 we will reraise with the original traceback
in python3 will raise 2 canonized exceptions
Thanks for submitting a PR, your contribution is really appreciated!
Here's a quick checklist that should be present in PRs:
$issue_id.$type
for example (588.bug)removal
,feature
,bugfix
,vendor
,doc
ortrivial
bugfix
,vendor
,doc
ortrivial
fixes, targetmaster
; for removals or features targetfeatures
;Unless your change is a trivial or a documentation fix (e.g., a typo or reword of a small section) please:
AUTHORS
;