https://github.com/python/cpython/commit/a22e11f8dcfd902f43aa69be34f2a9a89ab514ac
commit: a22e11f8dcfd902f43aa69be34f2a9a89ab514ac
branch: 3.13
author: Miss Islington (bot) <[email protected]>
committer: gaogaotiantian <[email protected]>
date: 2024-10-15T15:26:46Z
summary:

[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 703227dd021491ceb9343f69fa48f4b6a05adbb3)

Co-authored-by: Tian Gao <[email protected]>

files:
A Misc/NEWS.d/next/Library/2024-10-14-04-44-12.gh-issue-125422.MlVuC6.rst
M Lib/bdb.py
M Lib/test/test_bdb.py
M Lib/test/test_pdb.py

diff --git a/Lib/bdb.py b/Lib/bdb.py
index aa621053cfb4bc..a0e46e7f413816 100644
--- a/Lib/bdb.py
+++ b/Lib/bdb.py
@@ -329,9 +329,10 @@ def _set_caller_tracefunc(self, current_frame):
         # Issue #13183: pdb skips frames after hitting a breakpoint and running
         # step commands.
         # Restore the trace function in the caller (that may not have been set
-        # for performance reasons) when returning from the current frame.
+        # for performance reasons) when returning from the current frame, 
unless
+        # the caller is the botframe.
         caller_frame = current_frame.f_back
-        if caller_frame and not caller_frame.f_trace:
+        if caller_frame and not caller_frame.f_trace and caller_frame is not 
self.botframe:
             caller_frame.f_trace = self.trace_dispatch
 
     # Derived classes and clients can call the following methods
diff --git a/Lib/test/test_bdb.py b/Lib/test/test_bdb.py
index ed1a63daea1186..2e20c08a672d57 100644
--- a/Lib/test/test_bdb.py
+++ b/Lib/test/test_bdb.py
@@ -1216,6 +1216,19 @@ def main():
             with TracerRun(self) as tracer:
                 tracer.runcall(tfunc_import)
 
+    def test_next_to_botframe(self):
+        # gh-125422
+        # Check that next command won't go to the bottom frame.
+        code = """
+            lno = 2
+        """
+        self.expect_set = [
+            ('line', 2, '<module>'),   ('step', ),
+            ('return', 2, '<module>'), ('next', ),
+        ]
+        with TracerRun(self) as tracer:
+            tracer.run(compile(textwrap.dedent(code), '<string>', 'exec'))
+
 
 class TestRegressions(unittest.TestCase):
     def test_format_stack_entry_no_lineno(self):
diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py
index 9b2c885ed67f85..35285b7706bb70 100644
--- a/Lib/test/test_pdb.py
+++ b/Lib/test/test_pdb.py
@@ -3181,6 +3181,20 @@ def test_issue26053(self):
         self.assertRegex(res, "Restarting .* with arguments:\na b c")
         self.assertRegex(res, "Restarting .* with arguments:\nd e f")
 
+    def test_step_into_botframe(self):
+        # gh-125422
+        # pdb should not be able to step into the botframe (bdb.py)
+        script = "x = 1"
+        commands = """
+            step
+            step
+            step
+            quit
+        """
+        stdout, _ = self.run_pdb_script(script, commands)
+        self.assertIn("The program finished", stdout)
+        self.assertNotIn("bdb.py", stdout)
+
     def test_pdbrc_basic(self):
         script = textwrap.dedent("""
             a = 1
diff --git 
a/Misc/NEWS.d/next/Library/2024-10-14-04-44-12.gh-issue-125422.MlVuC6.rst 
b/Misc/NEWS.d/next/Library/2024-10-14-04-44-12.gh-issue-125422.MlVuC6.rst
new file mode 100644
index 00000000000000..c890ecec8beaf8
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-10-14-04-44-12.gh-issue-125422.MlVuC6.rst
@@ -0,0 +1 @@
+Fixed the bug where :mod:`pdb` and :mod:`bdb` can step into the bottom caller 
frame.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to