Skip to content

gh-129098: avoid using content of _pyrepl/__main__.py when reporting tracebacks #130721

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 20, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
add tests
  • Loading branch information
picnixz committed Mar 2, 2025
commit 9b0565da5966ac829b5d556b40477ad268eb7afd
29 changes: 27 additions & 2 deletions Lib/test/test_pyrepl/test_pyrepl.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
from unittest import TestCase, skipUnless, skipIf
from unittest.mock import patch
from test.support import force_not_colorized, make_clean_env
from test.support import SHORT_TIMEOUT
from test.support import SHORT_TIMEOUT, STDLIB_DIR
from test.support.import_helper import import_module
from test.support.os_helper import unlink
from test.support.os_helper import EnvironmentVarGuard, unlink

from .support import (
FakeConsole,
Expand Down Expand Up @@ -1216,6 +1216,31 @@ def test_python_basic_repl(self):
self.assertNotIn("Exception", output)
self.assertNotIn("Traceback", output)

@force_not_colorized
def test_no_pyrepl_source_in_exc(self):
# Avoid using _pyrepl/__main__.py in traceback reports
# See https://github.com/python/cpython/issues/129098.
pyrepl_main_file = os.path.join(STDLIB_DIR, "_pyrepl", "__main__.py")
self.assertTrue(os.path.exists(pyrepl_main_file), pyrepl_main_file)
with open(pyrepl_main_file) as fp:
excluded_lines = fp.readlines()
excluded_lines = list(filter(None, map(str.strip, excluded_lines)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit nuclear and maybe fragile as the lines can be in other future test but I know this is thinking too far ahead. I think this is reasonable for now. Well done!


for filename in ['?', 'unknown-filename', '<foo>', '<...>']:
self._test_no_pyrepl_source_in_exc(filename, excluded_lines)

def _test_no_pyrepl_source_in_exc(self, filename, excluded_lines):
with EnvironmentVarGuard() as env, self.subTest(filename=filename):
env.unset("PYTHON_BASIC_REPL")
commands = (f"eval(compile('spam', {filename!r}, 'eval'))\n"
f"exit()\n")
output, _ = self.run_repl(commands, env=env)
self.assertIn("Traceback (most recent call last)", output)
self.assertIn("NameError: name 'spam' is not defined", output)
for line in excluded_lines:
with self.subTest(line=line):
self.assertNotIn(line, output)

@force_not_colorized
def test_bad_sys_excepthook_doesnt_crash_pyrepl(self):
env = os.environ.copy()
Expand Down
Loading