sqlalchemy / mako

Mako Templates for Python
https://www.makotemplates.org
MIT License
364 stars 62 forks source link

Fix exception causes in lookup.py #319

Closed cool-RR closed 4 years ago

cool-RR commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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.

sqla-tester commented 4 years ago

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

cool-RR commented 4 years ago

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.

cool-RR commented 4 years ago

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 commented 4 years ago

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

zzzeek commented 4 years ago

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

sqla-tester commented 3 years ago

mike bayer (zzzeek) wrote:

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

sqla-tester commented 3 years ago

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 commented 2 years ago

Michael Bourke (bourke) wrote:

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

sqla-tester commented 2 years ago

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

sqla-tester commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

mike bayer (zzzeek) wrote:

we are on main now

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

sqla-tester commented 2 years ago

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 commented 2 years ago

mike bayer (zzzeek) wrote:

i made some changes

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

sqla-tester commented 2 years ago

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