Skip to content

Commit 44d0f88

Browse files
committed
Fix SystemError during REPL input compilation corrupting display
1 parent a700732 commit 44d0f88

6 files changed

Lines changed: 18 additions & 8 deletions

File tree

Lib/_pyrepl/simple_interact.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def _more_lines(console: code.InteractiveConsole, unicodetext: str) -> bool:
8383
src = _strip_final_indent(unicodetext)
8484
try:
8585
code = console.compile(src, "<stdin>", "single")
86-
except (OverflowError, SyntaxError, ValueError):
86+
except (OverflowError, SyntaxError, ValueError, SystemError):
8787
lines = src.splitlines(keepends=True)
8888
if len(lines) == 1:
8989
return False

Lib/code.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ def runsource(self, source, filename="<input>", symbol="single"):
4444
One of several things can happen:
4545
4646
1) The input is incorrect; compile_command() raised an
47-
exception (SyntaxError or OverflowError). A syntax traceback
48-
will be printed by calling the showsyntaxerror() method.
47+
exception. A syntax traceback will be printed by calling
48+
the showsyntaxerror() method.
4949
5050
2) The input is incomplete, and more input is required;
5151
compile_command() returned None. Nothing happens.
@@ -62,7 +62,7 @@ def runsource(self, source, filename="<input>", symbol="single"):
6262
"""
6363
try:
6464
code = self.compile(source, filename, symbol)
65-
except (OverflowError, SyntaxError, ValueError):
65+
except (OverflowError, SyntaxError, ValueError, SystemError):
6666
# Case 1
6767
self.showsyntaxerror(filename, source=source)
6868
return False

Lib/codeop.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ def _maybe_compile(compiler, source, filename, symbol, flags):
7171
except SyntaxError:
7272
pass
7373
# fallthrough
74+
except SystemError as exc:
75+
raise exc from None
7476

7577
return compiler(source, filename, symbol, incomplete_input=False)
7678

@@ -147,8 +149,8 @@ def __call__(self, source, filename="<input>", symbol="single"):
147149
148150
- Return a code object if the command is complete and valid
149151
- Return None if the command is incomplete
150-
- Raise SyntaxError, ValueError or OverflowError if the command is a
151-
syntax error (OverflowError and ValueError can be produced by
152-
malformed literals).
152+
- Raise SyntaxError, ValueError, OverflowError or SystemError.
153+
OverflowError and ValueError can be produced by malformed
154+
literals, SystemError if source cannot be compiled.
153155
"""
154156
return _maybe_compile(self.compiler, source, filename, symbol, flags=self.compiler.flags)

Lib/test/test_pyrepl/support.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def more_lines(text: str, namespace: dict | None = None):
4444
console = InteractiveConsole(namespace, filename="<stdin>")
4545
try:
4646
code = console.compile(src, "<stdin>", "single")
47-
except (OverflowError, SyntaxError, ValueError):
47+
except (OverflowError, SyntaxError, ValueError, SystemError):
4848
return False
4949
else:
5050
return code is None

Lib/test/test_pyrepl/test_interact.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,13 @@ def test_incomplete_statement(self):
275275
console = InteractiveColoredConsole(namespace, filename="<stdin>")
276276
self.assertTrue(_more_lines(console, code))
277277

278+
def test_system_error_during_compilation(self):
279+
namespace = {}
280+
console = InteractiveColoredConsole(namespace, filename="<stdin>")
281+
with patch.object(console, "compile", side_effect=SystemError("compiler bug")):
282+
result = _more_lines(console, "some code")
283+
self.assertFalse(result)
284+
278285

279286
class TestWarnings(unittest.TestCase):
280287
def test_pep_765_warning(self):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix ``PyREPL`` and :mode:`code` REPL mishandling a :exc:`SystemError` from the compiler. Patch by Bartosz Sławecki.

0 commit comments

Comments
 (0)