Skip to content

Commit 0a82c4c

Browse files
[3.12] gh-125422: Don't set the caller's f_trace if it's botframe (GH-125427) (#125531)
gh-125422: Don't set the caller's f_trace if it's botframe (GH-125427) (cherry picked from commit 703227d) Co-authored-by: Tian Gao <gaogaotiantian@hotmail.com>
1 parent 26725d1 commit 0a82c4c

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

Lib/bdb.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,10 @@ def _set_caller_tracefunc(self, current_frame):
295295
# Issue #13183: pdb skips frames after hitting a breakpoint and running
296296
# step commands.
297297
# Restore the trace function in the caller (that may not have been set
298-
# for performance reasons) when returning from the current frame.
298+
# for performance reasons) when returning from the current frame, unless
299+
# the caller is the botframe.
299300
caller_frame = current_frame.f_back
300-
if caller_frame and not caller_frame.f_trace:
301+
if caller_frame and not caller_frame.f_trace and caller_frame is not self.botframe:
301302
caller_frame.f_trace = self.trace_dispatch
302303

303304
# Derived classes and clients can call the following methods

Lib/test/test_bdb.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,6 +1203,19 @@ def main():
12031203
with TracerRun(self) as tracer:
12041204
tracer.runcall(tfunc_import)
12051205

1206+
def test_next_to_botframe(self):
1207+
# gh-125422
1208+
# Check that next command won't go to the bottom frame.
1209+
code = """
1210+
lno = 2
1211+
"""
1212+
self.expect_set = [
1213+
('line', 2, '<module>'), ('step', ),
1214+
('return', 2, '<module>'), ('next', ),
1215+
]
1216+
with TracerRun(self) as tracer:
1217+
tracer.run(compile(textwrap.dedent(code), '<string>', 'exec'))
1218+
12061219

12071220
class TestRegressions(unittest.TestCase):
12081221
def test_format_stack_entry_no_lineno(self):

Lib/test/test_pdb.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2283,6 +2283,20 @@ def test_issue26053(self):
22832283
self.assertRegex(res, "Restarting .* with arguments:\na b c")
22842284
self.assertRegex(res, "Restarting .* with arguments:\nd e f")
22852285

2286+
def test_step_into_botframe(self):
2287+
# gh-125422
2288+
# pdb should not be able to step into the botframe (bdb.py)
2289+
script = "x = 1"
2290+
commands = """
2291+
step
2292+
step
2293+
step
2294+
quit
2295+
"""
2296+
stdout, _ = self.run_pdb_script(script, commands)
2297+
self.assertIn("The program finished", stdout)
2298+
self.assertNotIn("bdb.py", stdout)
2299+
22862300
def test_pdbrc_basic(self):
22872301
script = textwrap.dedent("""
22882302
a = 1
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed the bug where :mod:`pdb` and :mod:`bdb` can step into the bottom caller frame.

0 commit comments

Comments
 (0)