Skip to content

Commit 1d585e5

Browse files
committed
bpo-46441: Add a boilerplate to test syntax errors in interactive mode
1 parent 650720a commit 1d585e5

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

Lib/test/test_repl.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,21 @@ def spawn_repl(*args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, **kw):
3636
stdout=stdout, stderr=stderr,
3737
**kw)
3838

39+
def run_on_interactive_mode(source):
40+
"""Spawn a new Python interpreter, pass the given
41+
input source code from the stdin and return the
42+
result back. If the interpreter exits non-zero, it
43+
raises a ValueError."""
44+
45+
process = spawn_repl()
46+
process.stdin.write(source)
47+
output = kill_python(process)
48+
49+
if process.returncode != 0:
50+
raise ValueError("Process didn't exit properly.")
51+
return output
52+
53+
3954
class TestInteractiveInterpreter(unittest.TestCase):
4055

4156
@cpython_only
@@ -108,5 +123,23 @@ def test_close_stdin(self):
108123
self.assertIn('before close', output)
109124

110125

126+
class TestInteractiveModeSyntaxErrors(unittest.TestCase):
127+
128+
def test_interactive_syntax_error_correct_line(self):
129+
output = run_on_interactive_mode(dedent("""\
130+
def f():
131+
print(0)
132+
return yield 42
133+
"""))
134+
135+
traceback_lines = output.splitlines()[-4:-1]
136+
expected_lines = [
137+
' return yield 42',
138+
' ^^^^^',
139+
'SyntaxError: invalid syntax'
140+
]
141+
self.assertEqual(traceback_lines, expected_lines)
142+
143+
111144
if __name__ == "__main__":
112145
unittest.main()

0 commit comments

Comments
 (0)