Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 62663bf

Browse filesBrowse files
[3.13] gh-129098: avoid using content of _pyrepl/__main__.py when reporting tracebacks (GH-130721) (#132755)
gh-129098: avoid using content of `_pyrepl/__main__.py` when reporting tracebacks (GH-130721) (cherry picked from commit 492e3e6) Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
1 parent 5ab628f commit 62663bf
Copy full SHA for 62663bf

File tree

Expand file treeCollapse file tree

3 files changed

+33
-2
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+33
-2
lines changed

‎Lib/_pyrepl/__main__.py

Copy file name to clipboard
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Important: don't add things to this module, as they will end up in the REPL's
22
# default globals. Use _pyrepl.main instead.
33

4+
# Avoid caching this file by linecache and incorrectly report tracebacks.
5+
# See https://github.com/python/cpython/issues/129098.
6+
__spec__ = __loader__ = None
7+
48
if __name__ == "__main__":
59
from .main import interactive_console as __pyrepl_interactive_console
610
__pyrepl_interactive_console()

‎Lib/test/test_pyrepl/test_pyrepl.py

Copy file name to clipboardExpand all lines: Lib/test/test_pyrepl/test_pyrepl.py
+27-2Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
from unittest import TestCase, skipUnless, skipIf
1212
from unittest.mock import patch
1313
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
1515
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
1717

1818
from .support import (
1919
FakeConsole,
@@ -1216,6 +1216,31 @@ def test_python_basic_repl(self):
12161216
self.assertNotIn("Exception", output)
12171217
self.assertNotIn("Traceback", output)
12181218

1219+
@force_not_colorized
1220+
def test_no_pyrepl_source_in_exc(self):
1221+
# Avoid using _pyrepl/__main__.py in traceback reports
1222+
# See https://github.com/python/cpython/issues/129098.
1223+
pyrepl_main_file = os.path.join(STDLIB_DIR, "_pyrepl", "__main__.py")
1224+
self.assertTrue(os.path.exists(pyrepl_main_file), pyrepl_main_file)
1225+
with open(pyrepl_main_file) as fp:
1226+
excluded_lines = fp.readlines()
1227+
excluded_lines = list(filter(None, map(str.strip, excluded_lines)))
1228+
1229+
for filename in ['?', 'unknown-filename', '<foo>', '<...>']:
1230+
self._test_no_pyrepl_source_in_exc(filename, excluded_lines)
1231+
1232+
def _test_no_pyrepl_source_in_exc(self, filename, excluded_lines):
1233+
with EnvironmentVarGuard() as env, self.subTest(filename=filename):
1234+
env.unset("PYTHON_BASIC_REPL")
1235+
commands = (f"eval(compile('spam', {filename!r}, 'eval'))\n"
1236+
f"exit()\n")
1237+
output, _ = self.run_repl(commands, env=env)
1238+
self.assertIn("Traceback (most recent call last)", output)
1239+
self.assertIn("NameError: name 'spam' is not defined", output)
1240+
for line in excluded_lines:
1241+
with self.subTest(line=line):
1242+
self.assertNotIn(line, output)
1243+
12191244
@force_not_colorized
12201245
def test_bad_sys_excepthook_doesnt_crash_pyrepl(self):
12211246
env = os.environ.copy()
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix REPL traceback reporting when using :func:`compile` with an inexisting
2+
file. Patch by Bénédikt Tran.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.