]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-58956: Set f_trace on frames with breakpoints after setting a new breakpoin...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sat, 25 Jan 2025 20:12:19 +0000 (21:12 +0100)
committerGitHub <noreply@github.com>
Sat, 25 Jan 2025 20:12:19 +0000 (15:12 -0500)
* gh-58956: Set f_trace on frames with breakpoints after setting a new breakpoint (GH-124454)
(cherry picked from commit 12eaadc0ad33411bb02945d700b6ed7e758bb188)

Co-authored-by: Tian Gao <gaogaotiantian@hotmail.com>
Lib/bdb.py
Lib/test/test_pdb.py
Misc/NEWS.d/next/Library/2024-09-24-18-16-59.gh-issue-58956.0wFrBR.rst [new file with mode: 0644]

index 8b63d9eca6da5f1c589747e5f3cd0438e7f97d7c..085c17ce05d2fdf130d4a52d224c5d27247e2461 100644 (file)
@@ -34,6 +34,7 @@ class Bdb:
         self.breaks = {}
         self.fncache = {}
         self.frame_returning = None
+        self.enterframe = None
 
         self._load_breaks()
 
@@ -341,6 +342,7 @@ class Bdb:
 
         If frame is not specified, debugging starts from caller's frame.
         """
+        sys.settrace(None)
         if frame is None:
             frame = sys._getframe().f_back
         self.reset()
@@ -404,6 +406,14 @@ class Bdb:
             return 'Line %s:%d does not exist' % (filename, lineno)
         self._add_to_breaks(filename, lineno)
         bp = Breakpoint(filename, lineno, temporary, cond, funcname)
+        # After we set a new breakpoint, we need to search through all frames
+        # and set f_trace to trace_dispatch if there could be a breakpoint in
+        # that frame.
+        frame = self.enterframe
+        while frame:
+            if self.break_anywhere(frame):
+                frame.f_trace = self.trace_dispatch
+            frame = frame.f_back
         return None
 
     def _load_breaks(self):
index 778aa03a63ab63ad1bff993787910e07a306e126..7a921a36b997f96d9d47afc9bcacc79db3a28ff0 100644 (file)
@@ -2371,6 +2371,36 @@ def bœr():
         self.assertRegex(res, "Restarting .* with arguments:\na b c")
         self.assertRegex(res, "Restarting .* with arguments:\nd e f")
 
+    def test_issue58956(self):
+        # Set a breakpoint in a function that already exists on the call stack
+        # should enable the trace function for the frame.
+        script = """
+            import bar
+            def foo():
+                ret = bar.bar()
+                pass
+            foo()
+        """
+        commands = """
+            b bar.bar
+            c
+            b main.py:5
+            c
+            p ret
+            quit
+        """
+        bar = """
+            def bar():
+                return 42
+        """
+        with open('bar.py', 'w') as f:
+            f.write(textwrap.dedent(bar))
+        self.addCleanup(os_helper.unlink, 'bar.py')
+        stdout, stderr = self.run_pdb_script(script, commands)
+        lines = stdout.splitlines()
+        self.assertIn('-> pass', lines)
+        self.assertIn('(Pdb) 42', lines)
+
     def test_step_into_botframe(self):
         # gh-125422
         # pdb should not be able to step into the botframe (bdb.py)
diff --git a/Misc/NEWS.d/next/Library/2024-09-24-18-16-59.gh-issue-58956.0wFrBR.rst b/Misc/NEWS.d/next/Library/2024-09-24-18-16-59.gh-issue-58956.0wFrBR.rst
new file mode 100644 (file)
index 0000000..a882a63
--- /dev/null
@@ -0,0 +1 @@
+Fixed a bug in :mod:`pdb` where sometimes the breakpoint won't trigger if it was set on a function which is already in the call stack.