summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBatuhan Taskaya <batuhan@python.org>2022-01-20 20:07:43 (GMT)
committerGitHub <noreply@github.com>2022-01-20 20:07:43 (GMT)
commit30fb6d073d9ca00dff8e4155c523cdfa63abab6b (patch)
treeb5c07465ea6cbd090cb2f7521a0349b419906b28
parenta1c88414926610a3527398a478c3e63c531dc742 (diff)
downloadcpython-30fb6d073d9ca00dff8e4155c523cdfa63abab6b.zip
cpython-30fb6d073d9ca00dff8e4155c523cdfa63abab6b.tar.gz
cpython-30fb6d073d9ca00dff8e4155c523cdfa63abab6b.tar.bz2
bpo-46441: Add a boilerplate to test syntax errors in interactive mode (GH-30720)
-rw-r--r--Lib/test/test_repl.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/Lib/test/test_repl.py b/Lib/test/test_repl.py
index 03bf8d8..a8d04a4 100644
--- a/Lib/test/test_repl.py
+++ b/Lib/test/test_repl.py
@@ -36,6 +36,21 @@ def spawn_repl(*args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, **kw):
stdout=stdout, stderr=stderr,
**kw)
+def run_on_interactive_mode(source):
+ """Spawn a new Python interpreter, pass the given
+ input source code from the stdin and return the
+ result back. If the interpreter exits non-zero, it
+ raises a ValueError."""
+
+ process = spawn_repl()
+ process.stdin.write(source)
+ output = kill_python(process)
+
+ if process.returncode != 0:
+ raise ValueError("Process didn't exit properly.")
+ return output
+
+
class TestInteractiveInterpreter(unittest.TestCase):
@cpython_only
@@ -108,5 +123,23 @@ class TestInteractiveInterpreter(unittest.TestCase):
self.assertIn('before close', output)
+class TestInteractiveModeSyntaxErrors(unittest.TestCase):
+
+ def test_interactive_syntax_error_correct_line(self):
+ output = run_on_interactive_mode(dedent("""\
+ def f():
+ print(0)
+ return yield 42
+ """))
+
+ traceback_lines = output.splitlines()[-4:-1]
+ expected_lines = [
+ ' return yield 42',
+ ' ^^^^^',
+ 'SyntaxError: invalid syntax'
+ ]
+ self.assertEqual(traceback_lines, expected_lines)
+
+
if __name__ == "__main__":
unittest.main()