Skip to content

refactor: walk tracebacks by attribute in error_already_set::what() - #6228

Merged
rwgk merged 3 commits into
pybind:masterfrom
henryiii:refactor/traceback-attr-walk
Nov 1, 2026
Merged

rwgk merged 3 commits into
pybind:masterfrom
henryiii:refactor/traceback-attr-walk

Conversation

@henryiii

Copy link
Copy Markdown
Collaborator

🤖 AI text below 🤖

Description

error_already_set::what() builds its At: section by reading traceback, frame, and code object struct fields directly. This PR reads tb_next, tb_frame, f_back, co_filename, and co_name by attribute access instead, and uses the public PyFrame_GetCode / PyFrame_GetLineNumber. py::object handles the reference counts. If an attribute lookup fails, the error is cleared and the walk stops. The output does not change. The PyPy/GraalPy guard stays as it is.

This comes from finding B6 in #6104. It is groundwork for stable-ABI support, but it is useful by itself because it removes a dependency on internal object layouts.

The existing tests checked the file, line, and function names of a multi-frame traceback only on Python < 3.12. A new test, test_error_already_set_what_traceback, checks them for a nested Python call on all CPython versions. It also passes with the old code.

Suggested changelog entry:

  • error_already_set::what() now walks the traceback through attribute access and public frame functions instead of reading object struct fields.

henryiii and others added 3 commits October 30, 2026 21:51
Read tb_next, tb_frame, f_back, co_filename and co_name through attribute
access and use the public PyFrame_GetCode/PyFrame_GetLineNumber functions,
instead of poking traceback, frame and code object struct fields, which are
not part of the stable ABI.

See pybind#6104 (B6).

Assisted-by: ClaudeCode:claude-opus-5-5
— codex GPT-6 on behalf of Ralf —

Document that failed notes and traceback attribute lookups silently discard
all secondary exception types, including KeyboardInterrupt and SystemError.
Also note the audit hooks triggered by tb_frame access.
@rwgk

rwgk commented Nov 1, 2026

Copy link
Copy Markdown
Collaborator

Commit e76bfb4 documents a known flaw in exception formatting: secondary exceptions from these attribute lookups are silently discarded, regardless of type — including KeyboardInterrupt and SystemError.

The flaw predates this PR in the __notes__ lookup; this PR adds a second path through traceback attribute access.

I considered two possible remedies:

  • Append a diagnostic breadcrumb to what() and continue.
  • Write the original diagnostic and a breadcrumb identifying the secondary failure to stderr, then terminate the process so the failure cannot pass silently.

However, I decided resolving this is beyond the scope of this PR.

@rwgk
rwgk merged commit 481725b into pybind:master Nov 1, 2026
87 checks passed
@rwgk
rwgk deleted the refactor/traceback-attr-walk branch November 1, 2026 05:41
@github-actions github-actions Bot added the needs changelog Possibly needs a changelog entry label Nov 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs changelog Possibly needs a changelog entry

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants