|
11 | 11 | from unittest import TestCase, skipUnless, skipIf
|
12 | 12 | from unittest.mock import patch
|
13 | 13 | from test.support import force_not_colorized, make_clean_env
|
14 |
| -from test.support import SHORT_TIMEOUT |
| 14 | +from test.support import SHORT_TIMEOUT, STDLIB_DIR |
15 | 15 | from test.support.import_helper import import_module
|
16 |
| -from test.support.os_helper import unlink |
| 16 | +from test.support.os_helper import EnvironmentVarGuard, unlink |
17 | 17 |
|
18 | 18 | from .support import (
|
19 | 19 | FakeConsole,
|
@@ -1217,6 +1217,31 @@ def test_python_basic_repl(self):
|
1217 | 1217 | self.assertNotIn("Exception", output)
|
1218 | 1218 | self.assertNotIn("Traceback", output)
|
1219 | 1219 |
|
| 1220 | + @force_not_colorized |
| 1221 | + def test_no_pyrepl_source_in_exc(self): |
| 1222 | + # Avoid using _pyrepl/__main__.py in traceback reports |
| 1223 | + # See https://github.com/python/cpython/issues/129098. |
| 1224 | + pyrepl_main_file = os.path.join(STDLIB_DIR, "_pyrepl", "__main__.py") |
| 1225 | + self.assertTrue(os.path.exists(pyrepl_main_file), pyrepl_main_file) |
| 1226 | + with open(pyrepl_main_file) as fp: |
| 1227 | + excluded_lines = fp.readlines() |
| 1228 | + excluded_lines = list(filter(None, map(str.strip, excluded_lines))) |
| 1229 | + |
| 1230 | + for filename in ['?', 'unknown-filename', '<foo>', '<...>']: |
| 1231 | + self._test_no_pyrepl_source_in_exc(filename, excluded_lines) |
| 1232 | + |
| 1233 | + def _test_no_pyrepl_source_in_exc(self, filename, excluded_lines): |
| 1234 | + with EnvironmentVarGuard() as env, self.subTest(filename=filename): |
| 1235 | + env.unset("PYTHON_BASIC_REPL") |
| 1236 | + commands = (f"eval(compile('spam', {filename!r}, 'eval'))\n" |
| 1237 | + f"exit()\n") |
| 1238 | + output, _ = self.run_repl(commands, env=env) |
| 1239 | + self.assertIn("Traceback (most recent call last)", output) |
| 1240 | + self.assertIn("NameError: name 'spam' is not defined", output) |
| 1241 | + for line in excluded_lines: |
| 1242 | + with self.subTest(line=line): |
| 1243 | + self.assertNotIn(line, output) |
| 1244 | + |
1220 | 1245 | @force_not_colorized
|
1221 | 1246 | def test_bad_sys_excepthook_doesnt_crash_pyrepl(self):
|
1222 | 1247 | env = os.environ.copy()
|
|
0 commit comments