Skip to content
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

Fix exception cause #492

Closed
wants to merge 1 commit into from

Conversation

cool-RR
Copy link

@cool-RR cool-RR commented Jun 11, 2020

I recently went over Matplotlib, Pandas and NumPy, fixing a small mistake in the way that Python 3's exception chaining is used. In mock, I found just one instance of this mistake, which I fixed in this PR.

The mistake is this: In some parts of the code, an exception is being caught and replaced with a more user-friendly error. In these cases the syntax raise new_error from old_error needs to be used.

Python 3's exception chaining means it shows not only the traceback of the current exception, but that of the original exception (and possibly more.) This is regardless of raise from. The usage of raise from tells Python to put a more accurate message between the tracebacks. Instead of this:

During handling of the above exception, another exception occurred:

You'll get this:

The above exception was the direct cause of the following exception:

The first is inaccurate, because it signifies a bug in the exception-handling code itself, which is a separate situation than wrapping an exception.

Let me know what you think!

@cjw296
Copy link
Collaborator

cjw296 commented Jun 11, 2020

This package is a rolling backport of unittest.mock.
As such, any problems you encounter most likely need to be fixed upstream.

Please can you try and reproduce the problem on the latest release of Python 3, including alphas, and replace any import from mock with ones from unittest.mock.

If the issue still occurs, then please report upstream through https://bugs.python.org/ as it will need to be fixed there so that it can be backported here and released to you.

If not, reply with what you find out and we can re-open this issue.

@cjw296 cjw296 closed this Jun 11, 2020
@cjw296
Copy link
Collaborator

cjw296 commented Jun 11, 2020

Also, if you PR upstream, please make sure you add a unit test to exercise the failure mode you're trying to fix.

@cool-RR
Copy link
Author

cool-RR commented Jun 11, 2020

Ah, unfortunately python-dev disagrees with making this change in the stdlib. So I guess we'll have to wait for them to change their mind some day. Thanks anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants