Repository navigation
gh-129605: Fix crashing pyrepl when NameError occurres in __getattr__ method - #129615
srinivasreddy wants to merge 15 commits into
Conversation
| self = frame.f_locals['self'] | ||
| if hasattr(self, wrong_name): | ||
| return f"self.{wrong_name}" | ||
| if frame.f_code.co_name != '__getattr__': |
There was a problem hiding this comment.
This also happens with __getattribute__.
skirpichev
left a comment
There was a problem hiding this comment.
LGTM
But see the issue thread, maybe there should be a better way.
|
This PR is stale because it has been open for 30 days with no activity. |
…xception around hasattr block
There was a problem hiding this comment.
🟡 Changes recommended
The tests emit unnecessary stdout noise contrary to the surrounding test convention and the news entry has an awkward grammatical phrasing, both of which should be cleaned up before approval.
2 open issues
What changed in this PR
This PR adds regression coverage and a news entry for gh-129605, where the interactive interpreter (pyrepl) would crash while computing a "Did you mean" suggestion for a NameError. The underlying fix guards the hasattr(self, wrong_name) lookup in traceback._compute_suggestion_error so that a re-entrant NameError raised from a user-defined __getattr__/__getattribute__/property (triggered by the hasattr probe itself) no longer propagates and breaks traceback formatting. The diff under review contains only the tests and the news fragment.
Changes:
- Add
test_name_error_with_getattrcoveringNameErrorraised inside__getattr__and__getattribute__. - Add
test_name_error_with_property_namecoveringNameErrorraised inside a property getter whose name matches the looked-up attribute. - Add a
Misc/NEWS.dentry describing the fix.
| File | Description |
|---|---|
| Lib/test/test_traceback.py | Adds two regression tests verifying suggestion formatting does not crash and reports exactly one NameError when attribute access re-raises during suggestion computation. |
| Misc/NEWS.d/next/Library/2025-02-03-16-39-35.gh-issue-129605.sN_0uq.rst | News entry documenting the pyrepl crash fix. |
Review effort: Balanced
| def __getattr__(self, x): | ||
| print(x) | ||
| print(qq) | ||
| class B: | ||
| def __getattribute__(self, x): | ||
| print(x) | ||
| print(qq) |
| @@ -0,0 +1 @@ | |||
| Fix pyrepl crash when a :exc:`NameError` occurs in the :meth:`~object.__getattr__`, :meth:`~object.__getattribute__` and any method of a class. | |||
|
This was addressed by #129632. After rebase the only things left were NEWS and the test. |


__getattr__raising #129605