]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-58933: Make pdb return to caller frame correctly when f_trace is not set (#118979)
authorTian Gao <gaogaotiantian@hotmail.com>
Mon, 13 May 2024 12:38:21 +0000 (05:38 -0700)
committerGitHub <noreply@github.com>
Mon, 13 May 2024 12:38:21 +0000 (13:38 +0100)
Lib/bdb.py
Lib/test/test_pdb.py
Misc/NEWS.d/next/Library/2024-05-12-21-38-42.gh-issue-58933.0kgU2l.rst [new file with mode: 0644]

index 7d63fce6ca63f18b8ddb350a9ef73678d8377e7b..aa621053cfb4bcfaffc4f6321ca331992ecc1cb3 100644 (file)
@@ -165,6 +165,11 @@ class Bdb:
             # The user issued a 'next' or 'until' command.
             if self.stopframe is frame and self.stoplineno != -1:
                 self._set_stopinfo(None, None)
+            # The previous frame might not have f_trace set, unless we are
+            # issuing a command that does not expect to stop, we should set
+            # f_trace
+            if self.stoplineno != -1:
+                self._set_caller_tracefunc(frame)
         return self.trace_dispatch
 
     def dispatch_exception(self, frame, arg):
@@ -320,15 +325,14 @@ class Bdb:
         self.stoplineno = stoplineno
         self._set_trace_opcodes(opcode)
 
-    def _set_caller_tracefunc(self):
+    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.
-        if self.frame_returning:
-            caller_frame = self.frame_returning.f_back
-            if caller_frame and not caller_frame.f_trace:
-                caller_frame.f_trace = self.trace_dispatch
+        caller_frame = current_frame.f_back
+        if caller_frame and not caller_frame.f_trace:
+            caller_frame.f_trace = self.trace_dispatch
 
     # Derived classes and clients can call the following methods
     # to affect the stepping state.
@@ -343,12 +347,10 @@ class Bdb:
 
     def set_step(self):
         """Stop after one line of code."""
-        self._set_caller_tracefunc()
         self._set_stopinfo(None, None)
 
     def set_stepinstr(self):
         """Stop before the next instruction."""
-        self._set_caller_tracefunc()
         self._set_stopinfo(None, None, opcode=True)
 
     def set_next(self, frame):
index f47466410082ef5eb16650a49516adcf91d13147..1b329b205d2d0f92bcabb2cc5ef84179f0cce83b 100644 (file)
@@ -1453,6 +1453,58 @@ def test_post_mortem():
     """
 
 
+def test_pdb_return_to_different_file():
+    """When pdb returns to a different file, it should not skip if f_trace is
+       not already set
+
+    >>> import pprint
+
+    >>> class A:
+    ...    def __repr__(self):
+    ...        return 'A'
+
+    >>> def test_function():
+    ...     import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
+    ...     pprint.pprint(A())
+
+    >>> reset_Breakpoint()
+    >>> with PdbTestInput([  # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
+    ...     'b A.__repr__',
+    ...     'continue',
+    ...     'return',
+    ...     'next',
+    ...     'return',
+    ...     'return',
+    ...     'continue',
+    ... ]):
+    ...    test_function()
+    > <doctest test.test_pdb.test_pdb_return_to_different_file[2]>(2)test_function()
+    -> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
+    (Pdb) b A.__repr__
+    Breakpoint 1 at <doctest test.test_pdb.test_pdb_return_to_different_file[1]>:3
+    (Pdb) continue
+    > <doctest test.test_pdb.test_pdb_return_to_different_file[1]>(3)__repr__()
+    -> return 'A'
+    (Pdb) return
+    --Return--
+    > <doctest test.test_pdb.test_pdb_return_to_different_file[1]>(3)__repr__()->'A'
+    -> return 'A'
+    (Pdb) next
+    > ...pprint.py..._safe_repr()
+    -> return rep,...
+    (Pdb) return
+    --Return--
+    > ...pprint.py..._safe_repr()->('A'...)
+    -> return rep,...
+    (Pdb) return
+    --Return--
+    > ...pprint.py...format()->('A'...)
+    -> return...
+    (Pdb) continue
+    A
+    """
+
+
 def test_pdb_skip_modules():
     """This illustrates the simple case of module skipping.
 
diff --git a/Misc/NEWS.d/next/Library/2024-05-12-21-38-42.gh-issue-58933.0kgU2l.rst b/Misc/NEWS.d/next/Library/2024-05-12-21-38-42.gh-issue-58933.0kgU2l.rst
new file mode 100644 (file)
index 0000000..fa70b95
--- /dev/null
@@ -0,0 +1 @@
+Make :mod:`pdb` return to caller frame correctly when ``f_trace`` of the caller frame is not set