diff options
author | Pablo Galindo Salgado <Pablogsal@gmail.com> | 2023-10-13 09:25:37 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-13 09:25:37 (GMT) |
commit | e1d8c65e1df990ef8d61b8912742e1a021395e78 (patch) | |
tree | 84956c1158b3baf33d9836a5a87b8fa6b57a0428 /Lib/test | |
parent | 898f531996f2c5399b13811682c578c4fd08afaa (diff) | |
download | cpython-e1d8c65e1df990ef8d61b8912742e1a021395e78.zip cpython-e1d8c65e1df990ef8d61b8912742e1a021395e78.tar.gz cpython-e1d8c65e1df990ef8d61b8912742e1a021395e78.tar.bz2 |
gh-110805: Allow the repl to show source code and complete tracebacks (#110775)
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_cmd_line_script.py | 2 | ||||
-rw-r--r-- | Lib/test/test_repl.py | 62 |
2 files changed, 64 insertions, 0 deletions
diff --git a/Lib/test/test_cmd_line_script.py b/Lib/test/test_cmd_line_script.py index 1b58882..614c6b3 100644 --- a/Lib/test/test_cmd_line_script.py +++ b/Lib/test/test_cmd_line_script.py @@ -203,6 +203,8 @@ class CmdLineTest(unittest.TestCase): stderr = p.stderr if separate_stderr else p.stdout self.assertIn(b'Traceback ', stderr.readline()) self.assertIn(b'File "<stdin>"', stderr.readline()) + self.assertIn(b'1/0', stderr.readline()) + self.assertIn(b' ~^~', stderr.readline()) self.assertIn(b'ZeroDivisionError', stderr.readline()) def test_repl_stdout_flush(self): diff --git a/Lib/test/test_repl.py b/Lib/test/test_repl.py index 58392f2..2ee5117 100644 --- a/Lib/test/test_repl.py +++ b/Lib/test/test_repl.py @@ -131,6 +131,68 @@ class TestInteractiveInterpreter(unittest.TestCase): self.assertEqual(process.returncode, 0) self.assertIn('before close', output) + def test_interactive_traceback_reporting(self): + user_input = "1 / 0 / 3 / 4" + p = spawn_repl() + p.stdin.write(user_input) + output = kill_python(p) + self.assertEqual(p.returncode, 0) + + traceback_lines = output.splitlines()[-6:-1] + expected_lines = [ + "Traceback (most recent call last):", + " File \"<stdin>\", line 1, in <module>", + " 1 / 0 / 3 / 4", + " ~~^~~", + "ZeroDivisionError: division by zero", + ] + self.assertEqual(traceback_lines, expected_lines) + + def test_interactive_traceback_reporting_multiple_input(self): + user_input1 = dedent(""" + def foo(x): + 1 / x + + """) + p = spawn_repl() + p.stdin.write(user_input1) + user_input2 = "foo(0)" + p.stdin.write(user_input2) + output = kill_python(p) + self.assertEqual(p.returncode, 0) + + traceback_lines = output.splitlines()[-7:-1] + expected_lines = [ + ' File "<stdin>", line 1, in <module>', + ' foo(0)', + ' File "<stdin>", line 2, in foo', + ' 1 / x', + ' ~~^~~', + 'ZeroDivisionError: division by zero' + ] + self.assertEqual(traceback_lines, expected_lines) + + def test_interactive_source_is_in_linecache(self): + user_input = dedent(""" + def foo(x): + return x + 1 + + def bar(x): + return foo(x) + 2 + """) + p = spawn_repl() + p.stdin.write(user_input) + user_input2 = dedent(""" + import linecache + print(linecache.cache['<python-input-1>']) + """) + p.stdin.write(user_input2) + output = kill_python(p) + self.assertEqual(p.returncode, 0) + expected = "(30, None, [\'def foo(x):\\n\', \' return x + 1\\n\', \'\\n\'], \'<stdin>\')" + self.assertIn(expected, output, expected) + + class TestInteractiveModeSyntaxErrors(unittest.TestCase): |