Skip to content

Commit 5271f8f

Browse files
authored
[3.13] gh-122546: use same filename for different exceptions in new repl (GH-123217) (#123226)
1 parent 5148e03 commit 5271f8f

File tree

4 files changed

+14
-11
lines changed

4 files changed

+14
-11
lines changed

Lib/_pyrepl/console.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ def __init__(
162162
self.can_colorize = _colorize.can_colorize()
163163

164164
def showsyntaxerror(self, filename=None, **kwargs):
165-
super().showsyntaxerror(colorize=self.can_colorize, **kwargs)
165+
super().showsyntaxerror(filename=filename, **kwargs)
166166

167167
def showtraceback(self):
168168
super().showtraceback(colorize=self.can_colorize)

Lib/code.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -113,16 +113,7 @@ def showsyntaxerror(self, filename=None, **kwargs):
113113
sys.last_value = value
114114
sys.last_traceback = tb
115115
if filename and type is SyntaxError:
116-
# Work hard to stuff the correct filename in the exception
117-
try:
118-
msg, (dummy_filename, lineno, offset, line) = value.args
119-
except ValueError:
120-
# Not the format we expect; leave it alone
121-
pass
122-
else:
123-
# Stuff in the right filename
124-
value = SyntaxError(msg, (filename, lineno, offset, line))
125-
sys.last_exc = sys.last_value = value
116+
value.filename = filename
126117
# Set the line of text that the exception refers to
127118
source = kwargs.pop('source', '')
128119
lines = source.splitlines()

Lib/test/test_pyrepl/test_pyrepl.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,16 @@ def test_not_wiping_history_file(self):
11001100
self.assertIn("spam", output)
11011101
self.assertNotEqual(pathlib.Path(hfile.name).stat().st_size, 0)
11021102

1103+
@force_not_colorized
1104+
def test_correct_filename_in_syntaxerrors(self):
1105+
env = os.environ.copy()
1106+
commands = "a b c\nexit()\n"
1107+
output, exit_code = self.run_repl(commands, env=env)
1108+
if "can't use pyrepl" in output:
1109+
self.skipTest("pyrepl not available")
1110+
self.assertIn("SyntaxError: invalid syntax", output)
1111+
self.assertIn("<python-input-0>", output)
1112+
11031113
def run_repl(
11041114
self,
11051115
repl_input: str | list[str],
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Consistently use same file name for different exceptions in the new repl.
2+
Patch by Sergey B Kirpichev.

0 commit comments

Comments
 (0)