Skip to content

SyntaxError thrown from the compiler exits the REPL #119700

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
JelleZijlstra opened this issue May 29, 2024 · 3 comments
Closed

SyntaxError thrown from the compiler exits the REPL #119700

JelleZijlstra opened this issue May 29, 2024 · 3 comments
Labels
3.13 bugs and security fixes 3.14 bugs and security fixes topic-repl Related to the interactive shell type-bug An unexpected behavior, bug, or error

Comments

@JelleZijlstra
Copy link
Member

JelleZijlstra commented May 29, 2024

Bug report

Bug description:

If you write a SyntaxError that gets caught by the parser, a nice error is shown and you go back to the main prompt.

But if you hit a SyntaxError thrown from the compiler, the REPL exits.

% ./python.exe
Python 3.14.0a0 (heads/pep649-compile-dirty:3b4a645ee8, May 28 2024, 18:48:32) [Clang 14.0.0 (clang-1400.0.29.202)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> def f():
...     x++
  File "<unknown>", line 2
    x++
       ^
SyntaxError: invalid syntax
>>> def f():
...     nonlocal y
Traceback (most recent call last):
  File "/Users/jelle/py/cpython/Lib/runpy.py", line 198, in _run_module_as_main
    return _run_code(code, main_globals, None,
                     "__main__", mod_spec)
  File "/Users/jelle/py/cpython/Lib/runpy.py", line 88, in _run_code
    exec(code, run_globals)
    ~~~~^^^^^^^^^^^^^^^^^^^
  File "/Users/jelle/py/cpython/Lib/_pyrepl/__main__.py", line 47, in <module>
    interactive_console()
    ~~~~~~~~~~~~~~~~~~~^^
  File "/Users/jelle/py/cpython/Lib/_pyrepl/__main__.py", line 44, in interactive_console
    return run_interactive(mainmodule)
  File "/Users/jelle/py/cpython/Lib/_pyrepl/simple_interact.py", line 181, in run_multiline_interactive_console
    more = console.push(_strip_final_indent(statement), filename=input_name, _symbol="single")  # type: ignore[call-arg]
  File "/Users/jelle/py/cpython/Lib/code.py", line 303, in push
    more = self.runsource(source, filename, symbol=_symbol)
  File "/Users/jelle/py/cpython/Lib/_pyrepl/simple_interact.py", line 103, in runsource
    code = compile(item, filename, the_symbol, dont_inherit=True)
  File "<python-input-1>", line 2
SyntaxError: no binding for nonlocal 'y' found

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Linked PRs

@JelleZijlstra JelleZijlstra added type-bug An unexpected behavior, bug, or error 3.13 bugs and security fixes 3.14 bugs and security fixes topic-repl Related to the interactive shell labels May 29, 2024
@brandtbucher
Copy link
Member

Yeah, I'm able to reproduce this too.

@brandtbucher
Copy link
Member

This fixes it for me:

diff --git a/Lib/_pyrepl/simple_interact.py b/Lib/_pyrepl/simple_interact.py
index 8ab4dab757..f6857c5967 100644
--- a/Lib/_pyrepl/simple_interact.py
+++ b/Lib/_pyrepl/simple_interact.py
@@ -96,7 +96,7 @@ def runsource(self, source, filename="<input>", symbol="single"):
             item = wrapper([stmt])
             try:
                 code = compile(item, filename, the_symbol)
-            except (OverflowError, ValueError):
+            except (OverflowError, ValueError, SyntaxError):
                     self.showsyntaxerror(filename)
                     return False

@skirpichev
Copy link
Member

See also #119555 Looks as a duplicate...

@JelleZijlstra JelleZijlstra closed this as not planned Won't fix, can't repro, duplicate, stale May 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.13 bugs and security fixes 3.14 bugs and security fixes topic-repl Related to the interactive shell type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants