Skip to content

gh-69919: Catch all compile errors in the code module, pyrepl and IDLE - #157585

Merged
serhiy-storchaka merged 5 commits into
python:mainfrom
serhiy-storchaka:gh-69919-catch-compile-errors
Sep 23, 2026
Merged

serhiy-storchaka merged 5 commits into
python:mainfrom
serhiy-storchaka:gh-69919-catch-compile-errors

Conversation

@serhiy-storchaka

@serhiy-storchaka serhiy-storchaka commented Sep 15, 2026 •

Copy link
Copy Markdown
Member

compile() can raise more than SyntaxError, OverflowError and ValueError: MemoryError ("Parser stack overflowed") for a few thousand chained unary operators, RecursionError ("Stack overflow during compilation") for tens of thousands of chained binary operators, and a bare MemoryError when memory really runs out.

code.InteractiveInterpreter.runsource() let those propagate, so code.interact() died. The new REPL survives through its outer loop, but prints a bare MemoryError without the message, or a RecursionError traceback through its own internals. In IDLE's Shell the exception escaped into the Tk callback, no new prompt was issued, and the next Enter re-compiled the same text: the Shell was stuck until the input was deleted.

Based on Terry's 2016 patch from the issue: runsource() in code and _pyrepl, _pyrepl's incomplete-input check, the startup-file compile in pyshell.py, runscript.checksyntax(), and pdb's find_function() and the check of condition/display/ignore expressions catch Exception. The SyntaxError-specific code in IDLE (marking the position in the text) runs only for SyntaxError; other exceptions are reported through the base class, i.e. by traceback. IDLE now also names the actual exception, so an IndentationError is no longer reported as SyntaxError.

The compile() documentation now lists the exceptions it can raise (including OverflowError for a too large source), and notes the 3.12 change for null bytes from ValueError to SyntaxError.

An alternative that fixes this in compile() itself, for main only, is #157586.

🤖 Generated with Claude Code

…nd IDLE

compile() can raise MemoryError or RecursionError for too deeply
nested source, not only SyntaxError, OverflowError and ValueError.
IDLE's Shell then lost its prompt until the input was deleted.

Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@read-the-docs-community

read-the-docs-community Bot commented Sep 15, 2026 •

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #34585425 | 📁 Comparing e5c5118 against main (82952e3)

  🔍 Preview build  

3 files changed
± builtins/functions.html
± library/code.html
± whatsnew/changelog.html

@terryjreedy terryjreedy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I learned something new: mock.mock_open.

Comment thread Lib/idlelib/pyshell.py
Comment thread Lib/idlelib/pyshell.py Outdated

@johnslavik johnslavik left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@serhiy-storchaka I think we should also catch SystemError. This could then supersede #151480, see issue #150732. Feel free to steal commits from my PR.

@serhiy-storchaka

Copy link
Copy Markdown
Member Author

No, SystemError should not be raised at first place.

@terryjreedy

Copy link
Copy Markdown
Member

The change to the non-idlelib .py files is to replace an inadequate and not consistent tuple of exception classes with Exception. This is the same as done in idlelib and should not need further review. The non-idlelib doc changes look good to me.

@serhiy-storchaka
serhiy-storchaka merged commit 4bc392c into python:main Sep 23, 2026
60 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @serhiy-storchaka for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14, 3.15.
🐍🍒⛏🤖

@serhiy-storchaka
serhiy-storchaka deleted the gh-69919-catch-compile-errors branch September 23, 2026 20:04
@miss-islington-app

Copy link
Copy Markdown

Sorry, @serhiy-storchaka, I could not cleanly backport this to 3.14 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 4bc392c13462096213777c6b2bdc470028c41536 3.14

@miss-islington-app

Copy link
Copy Markdown

Sorry, @serhiy-storchaka, I could not cleanly backport this to 3.13 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 4bc392c13462096213777c6b2bdc470028c41536 3.13

@bedevere-app

bedevere-app Bot commented Sep 23, 2026

Copy link
Copy Markdown

GH-158025 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Sep 23, 2026
@bedevere-app

bedevere-app Bot commented Sep 23, 2026

Copy link
Copy Markdown

GH-158029 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Sep 23, 2026
@bedevere-app

bedevere-app Bot commented Sep 23, 2026

Copy link
Copy Markdown

GH-158030 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.13 bugs and security fixes label Sep 23, 2026
terryjreedy added a commit that referenced this pull request Sep 24, 2026
…and IDLE (GH-157585) (#158030)

gh-69919: Catch all compile errors in the code module, pyrepl and IDLE (GH-157585)

compile() can raise MemoryError or RecursionError for too deeply
nested source, not only SyntaxError, OverflowError and ValueError.
IDLE's Shell then lost its prompt until the input was deleted.



(cherry picked from commit 4bc392c)

Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
terryjreedy added a commit that referenced this pull request Sep 24, 2026
…and IDLE (GH-157585) (#158029)

gh-69919: Catch all compile errors in the code module, pyrepl and IDLE (GH-157585)

compile() can raise MemoryError or RecursionError for too deeply
nested source, not only SyntaxError, OverflowError and ValueError.
IDLE's Shell then lost its prompt until the input was deleted.



(cherry picked from commit 4bc392c)

Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this pull request Sep 24, 2026
Too complex source now raises SyntaxError, so the tests inject
MemoryError to test that any compile() error is reported.
Compiling a too deeply nested AST object still raises RecursionError.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
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.

3 participants