Skip to content

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

Open
serhiy-storchaka wants to merge 3 commits into
python:mainfrom
serhiy-storchaka:gh-69919-catch-compile-errors
Open

serhiy-storchaka wants to merge 3 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 <[email protected]>
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@read-the-docs-community

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

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #34577916 | 📁 Comparing 9a9dd20 against main (82952e3)

  🔍 Preview build  

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting core review needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant