Skip to content

Commit cb10dd5

Browse files
skirpichevpablogsal
authored andcommitted
[3.13] pythongh-122546: Relax SyntaxError check when raising errors on the new REPL (pythonGH-123233)
(cherry picked from commit 4c3f0cb) Co-authored-by: Sergey B Kirpichev <[email protected]>
1 parent 5271f8f commit cb10dd5

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

Lib/code.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
__all__ = ["InteractiveInterpreter", "InteractiveConsole", "interact",
1414
"compile_command"]
1515

16+
1617
class InteractiveInterpreter:
1718
"""Base class for InteractiveConsole.
1819
@@ -112,7 +113,7 @@ def showsyntaxerror(self, filename=None, **kwargs):
112113
sys.last_type = type
113114
sys.last_value = value
114115
sys.last_traceback = tb
115-
if filename and type is SyntaxError:
116+
if filename and type and issubclass(type, SyntaxError):
116117
value.filename = filename
117118
# Set the line of text that the exception refers to
118119
source = kwargs.pop('source', '')
@@ -121,7 +122,8 @@ def showsyntaxerror(self, filename=None, **kwargs):
121122
and not value.text and len(lines) >= value.lineno):
122123
value.text = lines[value.lineno - 1]
123124
if sys.excepthook is sys.__excepthook__:
124-
lines = traceback.format_exception_only(type, value, colorize=colorize)
125+
lines = traceback.format_exception_only(
126+
type, value, colorize=colorize)
125127
self.write(''.join(lines))
126128
else:
127129
# If someone has set sys.excepthook, we let that take precedence
@@ -142,7 +144,8 @@ def showtraceback(self, **kwargs):
142144
sys.last_exc = ei[1]
143145
try:
144146
if sys.excepthook is sys.__excepthook__:
145-
lines = traceback.format_exception(ei[0], ei[1], last_tb.tb_next, colorize=colorize)
147+
lines = traceback.format_exception(
148+
ei[0], ei[1], last_tb.tb_next, colorize=colorize)
146149
self.write(''.join(lines))
147150
else:
148151
# If someone has set sys.excepthook, we let that take precedence
@@ -376,7 +379,7 @@ def interact(banner=None, readfunc=None, local=None, exitmsg=None, local_exit=Fa
376379

377380
parser = argparse.ArgumentParser()
378381
parser.add_argument('-q', action='store_true',
379-
help="don't print version and copyright messages")
382+
help="don't print version and copyright messages")
380383
args = parser.parse_args()
381384
if args.q or sys.flags.quiet:
382385
banner = ''

Lib/test/test_pyrepl/test_pyrepl.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,10 @@ def test_correct_filename_in_syntaxerrors(self):
11091109
self.skipTest("pyrepl not available")
11101110
self.assertIn("SyntaxError: invalid syntax", output)
11111111
self.assertIn("<python-input-0>", output)
1112+
commands = " b\nexit()\n"
1113+
output, exit_code = self.run_repl(commands, env=env)
1114+
self.assertIn("IndentationError: unexpected indent", output)
1115+
self.assertIn("<python-input-0>", output)
11121116

11131117
def run_repl(
11141118
self,

0 commit comments

Comments
 (0)