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 causes in lookup.py #319

Closed
wants to merge 1 commit into from

Conversation

cool-RR
Copy link
Contributor

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

I recently went over Matplotlib, Pandas and NumPy, fixing a small mistake in the way that Python 3's exception chaining is used. If you're interested, I can do it here too. I've done it on just one file right now.

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!

@zzzeek
Copy link
Member

zzzeek commented Jun 12, 2020

hey there -

absolutely, "raise from" is great, but for a PR right now Mako is still supporting Python 2.

All is not lost however as SQLAlchemy has a compat layer that does both: py3k version: https://github.com/sqlalchemy/sqlalchemy/blob/master/lib/sqlalchemy/util/compat.py#L140 py2k version: https://github.com/sqlalchemy/sqlalchemy/blob/master/lib/sqlalchemy/util/compat.py#L279 (uses py2k-only syntax)

@zzzeek
Copy link
Member

zzzeek commented Jun 12, 2020

in fact Mako already has this function, so if you can modify your PR to use this function it can go in:

https://github.com/sqlalchemy/mako/blob/master/mako/compat.py#L119

@cool-RR cool-RR force-pushed the patch-1 branch 2 times, most recently from ecddd75 to d06526a Compare June 12, 2020 17:21
@cool-RR
Copy link
Contributor Author

cool-RR commented Jun 12, 2020

Cool, done, waiting for your feedback.

I do think that compatibility function is a bit weird. For one, I don't see why it would only set __cause__ if it's Python 3. It wouldn't hurt Python 2. Also, getting the exception type seems redundant. And also, it should be setting __context__ in addition to __cause__. But if it works for you, I'm okay with that.

@zzzeek zzzeek requested a review from sqla-tester June 12, 2020 17:42
Copy link
Collaborator

@sqla-tester sqla-tester left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, this is sqla-tester setting up my work on behalf of zzzeek to try to get revision d06526a of this pull request into gerrit so we can run tests and reviews and stuff

@sqla-tester
Copy link
Collaborator

New Gerrit review created for change d06526a: https://gerrit.sqlalchemy.org/c/sqlalchemy/mako/+/2041

@cool-RR
Copy link
Contributor Author

cool-RR commented Jun 13, 2020

I can't figure out how to use your Gerrit. It failed me on this:

./mako/lookup.py:14:1: I100 Import statements are in the wrong order. 'from mako import compat' should be before 'from mako import util'

I fixed it and pushed an amended version, but it's still showing a failure.

Copy link
Member

@zzzeek zzzeek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since it raised a pep8 error anyway and a change is needed, I think we should use util.reraise() as that is where all of "compat" is exported to.

@@ -10,6 +10,7 @@
import stat

from mako import exceptions
from mako import compat
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we actually don't need this import as "reraise" should be available as util.reraise().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not available as util.reraise:

import mako.util
mako.util.reraise
Traceback (most recent call last):
  Python Shell, prompt 3, line 1
builtins.AttributeError: module 'mako.util' has no attribute 'reraise'

@cool-RR
Copy link
Contributor Author

cool-RR commented Jun 16, 2020

This PR is turning out to be less fun than I thought, because of the Python 2 compatibility layer and the Gerrit integration, so I'm gonna back out. Sorry.

@cool-RR cool-RR closed this Jun 16, 2020
@cool-RR
Copy link
Contributor Author

cool-RR commented Jun 16, 2020

If anyone wants to pick it up instead of me, that'd be cool.

@zzzeek
Copy link
Member

zzzeek commented Jun 16, 2020

sorry to hear that. the gerrit integration was nothing you need to be concerned with.

@sqla-tester
Copy link
Collaborator

mike bayer (zzzeek) wrote:

View this in Gerrit at https://gerrit.sqlalchemy.org/c/sqlalchemy/mako/+/2041

@sqla-tester
Copy link
Collaborator

Gerrit review https://gerrit.sqlalchemy.org/c/sqlalchemy/mako/+/2041 has been abandoned. That means that at least for the moment I need to close this pull request. Sorry it didn't work out :(

@bourke bourke added this to Backlog in Mako prioritization Nov 17, 2021
@bourke bourke moved this from Backlog to Active in Mako prioritization Nov 17, 2021
@sqla-tester
Copy link
Collaborator

Michael Bourke (bourke) wrote:

View this in Gerrit at https://gerrit.sqlalchemy.org/c/sqlalchemy/mako/+/2041

@sqla-tester
Copy link
Collaborator

Gerrit review https://gerrit.sqlalchemy.org/c/sqlalchemy/mako/+/2041 has been restored. That means I can reopen this pull request! Hooray :)

@sqla-tester
Copy link
Collaborator

Michael Bourke (bourke) wrote:

Now that main is Python 3 only, this was straightforward to implement. Needs a review though.

View this in Gerrit at https://gerrit.sqlalchemy.org/c/sqlalchemy/mako/+/3337

@sqla-tester
Copy link
Collaborator

Ram Rachum (cool-RR) wrote:

I'm happy to see that you picked it up Michael. Thank you.

View this in Gerrit at https://gerrit.sqlalchemy.org/c/sqlalchemy/mako/+/3337

@sqla-tester
Copy link
Collaborator

mike bayer (zzzeek) wrote:

we are on main now

View this in Gerrit at https://gerrit.sqlalchemy.org/c/sqlalchemy/mako/+/2041

@sqla-tester
Copy link
Collaborator

Gerrit review https://gerrit.sqlalchemy.org/c/sqlalchemy/mako/+/2041 has been abandoned. That means that at least for the moment I need to close this pull request. Sorry it didn't work out :(

@sqla-tester
Copy link
Collaborator

mike bayer (zzzeek) wrote:

i made some changes

View this in Gerrit at https://gerrit.sqlalchemy.org/c/sqlalchemy/mako/+/3337

@sqla-tester
Copy link
Collaborator

Gerrit review https://gerrit.sqlalchemy.org/c/sqlalchemy/mako/+/3337 has been merged. Congratulations! :)

sqlalchemy-bot pushed a commit that referenced this pull request Dec 10, 2021
Mako now performs exception chaining using ``raise from``, correctly
identifying underlying exception conditions when it raises its own
exceptions. Pull request courtesy Ram Rachum.

Additionally includes cleanup of the test suite to include
better exception fixtures.

Closes: #319
Pull-request: #319
Pull-request-sha: d06526a

Additionally:
Fixes: #348

Change-Id: Ibb2864de822bf4b63adf22a6bb32cf0758d296bd
@bourke bourke moved this from Active to Done in Mako prioritization Dec 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

Successfully merging this pull request may close these issues.

None yet

3 participants