Skip to content

Commit 1d4f321

Browse files
committed
pythongh-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
1 parent d27a53f commit 1d4f321

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

Lib/_pyrepl/console.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,12 @@ def __init__(
162162
self.can_colorize = _colorize.can_colorize()
163163

164164
def showsyntaxerror(self, filename=None):
165-
super().showsyntaxerror(colorize=self.can_colorize)
165+
import traceback
166+
super().showsyntaxerror(colorize=self.can_colorize, limit=traceback.BUILTIN_EXCEPTION_LIMIT)
166167

167168
def showtraceback(self):
168-
super().showtraceback(colorize=self.can_colorize)
169+
import traceback
170+
super().showtraceback(colorize=self.can_colorize, limit=traceback.BUILTIN_EXCEPTION_LIMIT)
169171

170172
def runsource(self, source, filename="<input>", symbol="single"):
171173
try:

Lib/code.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ def showsyntaxerror(self, filename=None, **kwargs):
107107
108108
"""
109109
colorize = kwargs.pop('colorize', False)
110+
limit = kwargs.pop('limit', None)
110111
type, value, tb = sys.exc_info()
111112
sys.last_exc = value
112113
sys.last_type = type
@@ -124,7 +125,8 @@ def showsyntaxerror(self, filename=None, **kwargs):
124125
value = SyntaxError(msg, (filename, lineno, offset, line))
125126
sys.last_exc = sys.last_value = value
126127
if sys.excepthook is sys.__excepthook__:
127-
lines = traceback.format_exception_only(type, value, colorize=colorize)
128+
lines = traceback.format_exception_only(type, value, colorize=colorize,
129+
limit=limit)
128130
self.write(''.join(lines))
129131
else:
130132
# If someone has set sys.excepthook, we let that take precedence
@@ -140,11 +142,13 @@ def showtraceback(self, **kwargs):
140142
141143
"""
142144
colorize = kwargs.pop('colorize', False)
145+
limit = kwargs.pop('limit', None)
143146
sys.last_type, sys.last_value, last_tb = ei = sys.exc_info()
144147
sys.last_traceback = last_tb
145148
sys.last_exc = ei[1]
146149
try:
147-
lines = traceback.format_exception(ei[0], ei[1], last_tb.tb_next, colorize=colorize)
150+
lines = traceback.format_exception(ei[0], ei[1], last_tb.tb_next, colorize=colorize,
151+
limit=limit)
148152
if sys.excepthook is sys.__excepthook__:
149153
self.write(''.join(lines))
150154
else:

Lib/test/test_pyrepl/test_pyrepl.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,6 +1076,31 @@ def test_not_wiping_history_file(self):
10761076
self.assertIn("spam", output)
10771077
self.assertNotEqual(pathlib.Path(hfile.name).stat().st_size, 0)
10781078

1079+
@force_not_colorized
1080+
def test_proper_tracebacklimit(self):
1081+
env = os.environ.copy()
1082+
commands = ("import sys\n"
1083+
"sys.tracebacklimit = 1\n"
1084+
"def x1(): 1/0\n\n"
1085+
"def x2(): x1()\n\n"
1086+
"def x3(): x2()\n\n"
1087+
"x3()\n"
1088+
"exit()\n")
1089+
1090+
env.pop("PYTHON_BASIC_REPL", None)
1091+
output, exit_code = self.run_repl(commands, env=env)
1092+
if "can\'t use pyrepl" in output:
1093+
self.skipTest("pyrepl not available")
1094+
self.assertIn("in x1", output)
1095+
self.assertNotIn("in x3", output)
1096+
self.assertNotIn("in <module>", output)
1097+
1098+
env["PYTHON_BASIC_REPL"] = "1"
1099+
output, exit_code = self.run_repl(commands, env=env)
1100+
self.assertIn("in x1", output)
1101+
self.assertNotIn("in x3", output)
1102+
self.assertNotIn("in <module>", output)
1103+
10791104
def run_repl(
10801105
self,
10811106
repl_input: str | list[str],

0 commit comments

Comments
 (0)