Skip to content

[3.13] gh-122546: Relax SyntaxError check when raising errors on the new REPL (GH-123233) #123246

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

Closed
wants to merge 1 commit into from
Closed
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
11 changes: 7 additions & 4 deletions Lib/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
__all__ = ["InteractiveInterpreter", "InteractiveConsole", "interact",
"compile_command"]


class InteractiveInterpreter:
"""Base class for InteractiveConsole.

Expand Down Expand Up @@ -112,7 +113,7 @@ def showsyntaxerror(self, filename=None, **kwargs):
sys.last_type = type
sys.last_value = value
sys.last_traceback = tb
if filename and type is SyntaxError:
if filename and type and issubclass(type, SyntaxError):
value.filename = filename
# Set the line of text that the exception refers to
source = kwargs.pop('source', '')
Expand All @@ -121,7 +122,8 @@ def showsyntaxerror(self, filename=None, **kwargs):
and not value.text and len(lines) >= value.lineno):
value.text = lines[value.lineno - 1]
if sys.excepthook is sys.__excepthook__:
lines = traceback.format_exception_only(type, value, colorize=colorize)
lines = traceback.format_exception_only(
type, value, colorize=colorize)
self.write(''.join(lines))
else:
# If someone has set sys.excepthook, we let that take precedence
Expand All @@ -142,7 +144,8 @@ def showtraceback(self, **kwargs):
sys.last_exc = ei[1]
try:
if sys.excepthook is sys.__excepthook__:
lines = traceback.format_exception(ei[0], ei[1], last_tb.tb_next, colorize=colorize)
lines = traceback.format_exception(
ei[0], ei[1], last_tb.tb_next, colorize=colorize)
self.write(''.join(lines))
else:
# If someone has set sys.excepthook, we let that take precedence
Expand Down Expand Up @@ -376,7 +379,7 @@ def interact(banner=None, readfunc=None, local=None, exitmsg=None, local_exit=Fa

parser = argparse.ArgumentParser()
parser.add_argument('-q', action='store_true',
help="don't print version and copyright messages")
help="don't print version and copyright messages")
args = parser.parse_args()
if args.q or sys.flags.quiet:
banner = ''
Expand Down
4 changes: 4 additions & 0 deletions Lib/test/test_pyrepl/test_pyrepl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,10 @@ def test_correct_filename_in_syntaxerrors(self):
self.skipTest("pyrepl not available")
self.assertIn("SyntaxError: invalid syntax", output)
self.assertIn("<python-input-0>", output)
commands = " b\nexit()\n"
output, exit_code = self.run_repl(commands, env=env)
self.assertIn("IndentationError: unexpected indent", output)
self.assertIn("<python-input-0>", output)

def run_repl(
self,
Expand Down
Loading