From 1d4f3217d77be950d933ea9c36b0b58603a372e2 Mon Sep 17 00:00:00 2001 From: CF Bolz-Tereick Date: Tue, 30 Jul 2024 14:01:42 +0200 Subject: [PATCH 1/2] gh-82378: fix sys.tracebacklimit in pyrepl make sure that pyrepl uses the same logic for sys.tracebacklimit as both the basic repl and the standard sys.excepthook --- Lib/_pyrepl/console.py | 6 ++++-- Lib/code.py | 8 ++++++-- Lib/test/test_pyrepl/test_pyrepl.py | 25 +++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/Lib/_pyrepl/console.py b/Lib/_pyrepl/console.py index a8d3f520340dcf..3c9c45bb49440f 100644 --- a/Lib/_pyrepl/console.py +++ b/Lib/_pyrepl/console.py @@ -162,10 +162,12 @@ def __init__( self.can_colorize = _colorize.can_colorize() def showsyntaxerror(self, filename=None): - super().showsyntaxerror(colorize=self.can_colorize) + import traceback + super().showsyntaxerror(colorize=self.can_colorize, limit=traceback.BUILTIN_EXCEPTION_LIMIT) def showtraceback(self): - super().showtraceback(colorize=self.can_colorize) + import traceback + super().showtraceback(colorize=self.can_colorize, limit=traceback.BUILTIN_EXCEPTION_LIMIT) def runsource(self, source, filename="", symbol="single"): try: diff --git a/Lib/code.py b/Lib/code.py index a55fced0704b1d..19567a893146fd 100644 --- a/Lib/code.py +++ b/Lib/code.py @@ -107,6 +107,7 @@ def showsyntaxerror(self, filename=None, **kwargs): """ colorize = kwargs.pop('colorize', False) + limit = kwargs.pop('limit', None) type, value, tb = sys.exc_info() sys.last_exc = value sys.last_type = type @@ -124,7 +125,8 @@ def showsyntaxerror(self, filename=None, **kwargs): value = SyntaxError(msg, (filename, lineno, offset, line)) sys.last_exc = sys.last_value = value if sys.excepthook is sys.__excepthook__: - lines = traceback.format_exception_only(type, value, colorize=colorize) + lines = traceback.format_exception_only(type, value, colorize=colorize, + limit=limit) self.write(''.join(lines)) else: # If someone has set sys.excepthook, we let that take precedence @@ -140,11 +142,13 @@ def showtraceback(self, **kwargs): """ colorize = kwargs.pop('colorize', False) + limit = kwargs.pop('limit', None) sys.last_type, sys.last_value, last_tb = ei = sys.exc_info() sys.last_traceback = last_tb sys.last_exc = ei[1] try: - 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, + limit=limit) if sys.excepthook is sys.__excepthook__: self.write(''.join(lines)) else: diff --git a/Lib/test/test_pyrepl/test_pyrepl.py b/Lib/test/test_pyrepl/test_pyrepl.py index 3a1bacef8a1756..f0a77991e4de75 100644 --- a/Lib/test/test_pyrepl/test_pyrepl.py +++ b/Lib/test/test_pyrepl/test_pyrepl.py @@ -1076,6 +1076,31 @@ 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_proper_tracebacklimit(self): + env = os.environ.copy() + commands = ("import sys\n" + "sys.tracebacklimit = 1\n" + "def x1(): 1/0\n\n" + "def x2(): x1()\n\n" + "def x3(): x2()\n\n" + "x3()\n" + "exit()\n") + + env.pop("PYTHON_BASIC_REPL", None) + output, exit_code = self.run_repl(commands, env=env) + if "can\'t use pyrepl" in output: + self.skipTest("pyrepl not available") + self.assertIn("in x1", output) + self.assertNotIn("in x3", output) + self.assertNotIn("in ", output) + + env["PYTHON_BASIC_REPL"] = "1" + output, exit_code = self.run_repl(commands, env=env) + self.assertIn("in x1", output) + self.assertNotIn("in x3", output) + self.assertNotIn("in ", output) + def run_repl( self, repl_input: str | list[str], From 2e18e4c01ef454f9c7f843868d46f1257073c6b5 Mon Sep 17 00:00:00 2001 From: CF Bolz-Tereick Date: Wed, 31 Jul 2024 14:55:43 +0200 Subject: [PATCH 2/2] add news entry --- .../next/Library/2024-07-31-14-55-41.gh-issue-82378.eZvYmR.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2024-07-31-14-55-41.gh-issue-82378.eZvYmR.rst diff --git a/Misc/NEWS.d/next/Library/2024-07-31-14-55-41.gh-issue-82378.eZvYmR.rst b/Misc/NEWS.d/next/Library/2024-07-31-14-55-41.gh-issue-82378.eZvYmR.rst new file mode 100644 index 00000000000000..8af016e7c82fcb --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-07-31-14-55-41.gh-issue-82378.eZvYmR.rst @@ -0,0 +1,2 @@ +Make sure that the new :term:`REPL` interprets :data:`sys.tracebacklimit` in +the same way that the classic REPL did.