Skip to content

[3.13] gh-122546: use same filename for different exceptions in new repl (GH-123217) #123226

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 1 commit into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion Lib/_pyrepl/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def __init__(
self.can_colorize = _colorize.can_colorize()

def showsyntaxerror(self, filename=None, **kwargs):
super().showsyntaxerror(colorize=self.can_colorize, **kwargs)
super().showsyntaxerror(filename=filename, **kwargs)

def showtraceback(self):
super().showtraceback(colorize=self.can_colorize)
Expand Down
11 changes: 1 addition & 10 deletions Lib/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,7 @@ def showsyntaxerror(self, filename=None, **kwargs):
sys.last_value = value
sys.last_traceback = tb
if filename and type is SyntaxError:
# Work hard to stuff the correct filename in the exception
try:
msg, (dummy_filename, lineno, offset, line) = value.args
except ValueError:
# Not the format we expect; leave it alone
pass
else:
# Stuff in the right filename
value = SyntaxError(msg, (filename, lineno, offset, line))
sys.last_exc = sys.last_value = value
value.filename = filename
# Set the line of text that the exception refers to
source = kwargs.pop('source', '')
lines = source.splitlines()
Expand Down
10 changes: 10 additions & 0 deletions Lib/test/test_pyrepl/test_pyrepl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,16 @@ def test_not_wiping_history_file(self):
self.assertIn("spam", output)
self.assertNotEqual(pathlib.Path(hfile.name).stat().st_size, 0)

@force_not_colorized
def test_correct_filename_in_syntaxerrors(self):
env = os.environ.copy()
commands = "a b c\nexit()\n"
output, exit_code = self.run_repl(commands, env=env)
if "can't use pyrepl" in output:
self.skipTest("pyrepl not available")
self.assertIn("SyntaxError: invalid syntax", output)
self.assertIn("<python-input-0>", output)

def run_repl(
self,
repl_input: str | list[str],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Consistently use same file name for different exceptions in the new repl.
Patch by Sergey B Kirpichev.
Loading