gh-69919: Catch all compile errors in the code module, pyrepl and IDLE - #157585
serhiy-storchaka merged 5 commits into
Conversation
…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>
Documentation build overview
|
terryjreedy
left a comment
There was a problem hiding this comment.
I learned something new: mock.mock_open.
There was a problem hiding this comment.
@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.
|
No, SystemError should not be raised at first place. |
|
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. |
|
Thanks @serhiy-storchaka for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14, 3.15. |
|
Sorry, @serhiy-storchaka, I could not cleanly backport this to |
|
Sorry, @serhiy-storchaka, I could not cleanly backport this to |
|
GH-158025 is a backport of this pull request to the 3.15 branch. |
|
GH-158029 is a backport of this pull request to the 3.14 branch. |
|
GH-158030 is a backport of this pull request to the 3.13 branch. |
…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>
…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>
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>
compile()can raise more thanSyntaxError,OverflowErrorandValueError: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 bareMemoryErrorwhen memory really runs out.code.InteractiveInterpreter.runsource()let those propagate, socode.interact()died. The new REPL survives through its outer loop, but prints a bareMemoryErrorwithout the message, or aRecursionErrortraceback 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()incodeand_pyrepl,_pyrepl's incomplete-input check, the startup-file compile inpyshell.py,runscript.checksyntax(), andpdb'sfind_function()and the check ofcondition/display/ignoreexpressions catchException. TheSyntaxError-specific code in IDLE (marking the position in the text) runs only forSyntaxError; other exceptions are reported through the base class, i.e. bytraceback. IDLE now also names the actual exception, so anIndentationErroris no longer reported asSyntaxError.The
compile()documentation now lists the exceptions it can raise (includingOverflowErrorfor a too large source), and notes the 3.12 change for null bytes fromValueErrortoSyntaxError.An alternative that fixes this in
compile()itself, for main only, is #157586.🤖 Generated with Claude Code