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 @frozen exceptions to allow __traceback__ to be set. #1081

Merged

Conversation

Julian
Copy link
Member

@Julian Julian commented Jan 6, 2023

Summary

This code previously raised an attrs.FrozenInstanceError:

from contextlib import contextmanager
from attrs import frozen

@frozen
class Boom(Exception):
    pass


@contextmanager
def do_nothing():
    yield


with do_nothing() as _:
    raise Boom()

on CPython (3.11 specifically, but probably other versions). On PyPy, it works, at least on 3.9 here -- which means I think it seems reasonable to merge the two codepaths here as I've done rather than whitelisting different attributes on different interpreters, but lemme know if you disagree.

(In real life a likely way you're going to encounter this, just to help anyone searching, is that click will wrap all exceptions during execution of a program, and does so via a contextlib.contextmanager)

Refs #712

Pull Request Check List

  • Added tests for changed code.
    Our CI fails if coverage is not 100%.
  • New features have been added to our Hypothesis testing strategy.
  • Changes or additions to public APIs are reflected in our type stubs (files ending in .pyi).
    • ...and used in the stub test file tests/typing_example.py.
    • If they've been added to attr/__init__.pyi, they've also been re-imported in attrs/__init__.pyi.
  • Updated documentation for changed code.
    • New functions/classes have to be added to docs/api.rst by hand.
    • Changes to the signature of @attr.s() have to be added by hand too.
    • Changed/added classes/methods/functions have appropriate versionadded, versionchanged, or deprecated directives.
      Find the appropriate next version in our __init__.py file.
  • Documentation in .rst files is written using semantic newlines.
  • Changes (and possible deprecations) have news fragments in changelog.d.
  • Consider granting push permissions to the PR branch, so maintainers can fix minor issues themselves without pestering you.

@hynek
Copy link
Member

hynek commented Jan 7, 2023

Ah nice I'm pretty sure I ran junk this myself when switching to 3.11 but decided to just thaw the class and get on with my life.

Can you finish it up with a news fragment pls?

@Julian Julian force-pushed the fix-frozen-exceptions-in-contextmanagers branch from 6e3a7eb to a604fc2 Compare January 7, 2023 15:41
@Julian
Copy link
Member Author

Julian commented Jan 7, 2023

Fixed!

@@ -0,0 +1 @@
Fix frozen exception classes when raised within e.g. `contextlib.contextmanager`, which mutates their ``__traceback__`` attributes.
Copy link
Member

Choose a reason for hiding this comment

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

Sir this is a Wendy's Markdown shop. 🤓

Copy link
Member Author

Choose a reason for hiding this comment

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

Ups, fix fixed then.

E.g. contextlib.contextmanager does so whenever an exception is
raised in its body, and does so even on CPython, so merging the
two code paths now seems reasonable.
@Julian Julian force-pushed the fix-frozen-exceptions-in-contextmanagers branch from a604fc2 to 388ecab Compare January 8, 2023 16:18
Copy link
Member

@hynek hynek left a comment

Choose a reason for hiding this comment

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

Excellent thank you!

@hynek hynek merged commit a71fbba into python-attrs:main Jan 8, 2023
@Julian Julian deleted the fix-frozen-exceptions-in-contextmanagers branch January 8, 2023 20:57
nadove-ucsc added a commit to DataBiosphere/azul that referenced this pull request Feb 28, 2024
hannes-ucsc pushed a commit to DataBiosphere/azul that referenced this pull request Mar 1, 2024
dsotirho-ucsc pushed a commit to DataBiosphere/azul that referenced this pull request Mar 1, 2024
hannes-ucsc pushed a commit to DataBiosphere/azul that referenced this pull request Mar 2, 2024
dsotirho-ucsc pushed a commit to DataBiosphere/azul that referenced this pull request Mar 5, 2024
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