Skip to content

Commit a22e11f

Browse files
[3.13] gh-125422: Don't set the caller's f_trace if it's botframe (GH-125427) (#125530)
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 <[email protected]>
1 parent 3b8477b commit a22e11f

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
@@ -329,9 +329,10 @@ def _set_caller_tracefunc(self, current_frame):
329329
# Issue #13183: pdb skips frames after hitting a breakpoint and running
330330
# step commands.
331331
# Restore the trace function in the caller (that may not have been set
332-
# for performance reasons) when returning from the current frame.
332+
# for performance reasons) when returning from the current frame, unless
333+
# the caller is the botframe.
333334
caller_frame = current_frame.f_back
334-
if caller_frame and not caller_frame.f_trace:
335+
if caller_frame and not caller_frame.f_trace and caller_frame is not self.botframe:
335336
caller_frame.f_trace = self.trace_dispatch
336337

337338
# 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
@@ -1216,6 +1216,19 @@ def main():
12161216
with TracerRun(self) as tracer:
12171217
tracer.runcall(tfunc_import)
12181218

1219+
def test_next_to_botframe(self):
1220+
# gh-125422
1221+
# Check that next command won't go to the bottom frame.
1222+
code = """
1223+
lno = 2
1224+
"""
1225+
self.expect_set = [
1226+
('line', 2, '<module>'), ('step', ),
1227+
('return', 2, '<module>'), ('next', ),
1228+
]
1229+
with TracerRun(self) as tracer:
1230+
tracer.run(compile(textwrap.dedent(code), '<string>', 'exec'))
1231+
12191232

12201233
class TestRegressions(unittest.TestCase):
12211234
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
@@ -3181,6 +3181,20 @@ def test_issue26053(self):
31813181
self.assertRegex(res, "Restarting .* with arguments:\na b c")
31823182
self.assertRegex(res, "Restarting .* with arguments:\nd e f")
31833183

3184+
def test_step_into_botframe(self):
3185+
# gh-125422
3186+
# pdb should not be able to step into the botframe (bdb.py)
3187+
script = "x = 1"
3188+
commands = """
3189+
step
3190+
step
3191+
step
3192+
quit
3193+
"""
3194+
stdout, _ = self.run_pdb_script(script, commands)
3195+
self.assertIn("The program finished", stdout)
3196+
self.assertNotIn("bdb.py", stdout)
3197+
31843198
def test_pdbrc_basic(self):
31853199
script = textwrap.dedent("""
31863200
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)