Skip to content

Rejoin split try/except/else/finally suites when printing - #15

Closed
EiNSTeiN- wants to merge 1 commit into
ejfkdev:mainfrom
EiNSTeiN-:fix/split-try-code-generation
Closed

EiNSTeiN- wants to merge 1 commit into
ejfkdev:mainfrom
EiNSTeiN-:fix/split-try-code-generation

Conversation

@EiNSTeiN-

Copy link
Copy Markdown
Contributor

I am opening this PR to print a split try/except/else/finally AST as one valid Python statement, preserving all of its recovered suites.

Exception reconstruction can represent a statement as an outer Try with else/finally and a single inner Try holding the except clauses. Printing that structure literally produces an else without an associated except at the same level:

try:
    try:
        work()
    except ValueError as error:
        handle(error)
else:
    success()
finally:
    cleanup()

That output is invalid Python. The constrained split shape should print as:

try:
    work()
except ValueError as error:
    handle(error)
else:
    success()
finally:
    cleanup()

The code generator rejoins only an outer try with no handlers, nonempty success and finally suites, and exactly one inner try that has handlers but no success or finally suite of its own. It preserves the inner body/handlers and the outer success/finally suites. A legitimate nested try/finally retains its nesting.

Unified try/except/finally syntax begins in Python 2.5, so the printer repair starts at that version. It is an AST repair rather than an opcode-specific one.

docs/LIMITATIONS.md is updated to distinguish this repaired printing error from the remaining exception-region reconstruction problems in multiprocessing.connection.

@ejfkdev

ejfkdev commented Oct 23, 2026

Copy link
Copy Markdown
Owner

Merged via consolidated PR #28 (commit e629b6c). Your fix, fixtures, behavior cases and integration test are all included and verified together: real-world corpus 6411/6411, stdlib corpus 528/528, behavior tests green on 3.9-3.14. Thanks for the detailed reports and repros!

@ejfkdev ejfkdev closed this Oct 23, 2026
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.

2 participants