]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-128991: Release the enter frame reference within bdb callba… (#129003)
authorTian Gao <gaogaotiantian@hotmail.com>
Sat, 18 Jan 2025 22:21:34 +0000 (17:21 -0500)
committerGitHub <noreply@github.com>
Sat, 18 Jan 2025 22:21:34 +0000 (17:21 -0500)
[3.12] gh-128991: Release the enter frame reference within bdb callback (GH-128992)

* Release the enter frame reference within bdb callback

* 📜🤖 Added by blurb_it.

---------

(cherry picked from commit 61b35f74aa4a6ac606635e245147ff3658628d99)

Lib/bdb.py
Misc/NEWS.d/next/Library/2025-01-18-16-58-10.gh-issue-128991.EzJit9.rst [new file with mode: 0644]

index 3486deacd86a7c63130b3324bf555c2f1cb07aa9..8b63d9eca6da5f1c589747e5f3cd0438e7f97d7c 100644 (file)
@@ -3,6 +3,7 @@
 import fnmatch
 import sys
 import os
+from contextlib import contextmanager
 from inspect import CO_GENERATOR, CO_COROUTINE, CO_ASYNC_GENERATOR
 
 __all__ = ["BdbQuit", "Bdb", "Breakpoint"]
@@ -60,6 +61,12 @@ class Bdb:
         self.botframe = None
         self._set_stopinfo(None, None)
 
+    @contextmanager
+    def set_enterframe(self, frame):
+        self.enterframe = frame
+        yield
+        self.enterframe = None
+
     def trace_dispatch(self, frame, event, arg):
         """Dispatch a trace function for debugged frames based on the event.
 
@@ -84,24 +91,26 @@ class Bdb:
 
         The arg parameter depends on the previous event.
         """
-        if self.quitting:
-            return # None
-        if event == 'line':
-            return self.dispatch_line(frame)
-        if event == 'call':
-            return self.dispatch_call(frame, arg)
-        if event == 'return':
-            return self.dispatch_return(frame, arg)
-        if event == 'exception':
-            return self.dispatch_exception(frame, arg)
-        if event == 'c_call':
-            return self.trace_dispatch
-        if event == 'c_exception':
-            return self.trace_dispatch
-        if event == 'c_return':
+
+        with self.set_enterframe(frame):
+            if self.quitting:
+                return # None
+            if event == 'line':
+                return self.dispatch_line(frame)
+            if event == 'call':
+                return self.dispatch_call(frame, arg)
+            if event == 'return':
+                return self.dispatch_return(frame, arg)
+            if event == 'exception':
+                return self.dispatch_exception(frame, arg)
+            if event == 'c_call':
+                return self.trace_dispatch
+            if event == 'c_exception':
+                return self.trace_dispatch
+            if event == 'c_return':
+                return self.trace_dispatch
+            print('bdb.Bdb.dispatch: unknown debugging event:', repr(event))
             return self.trace_dispatch
-        print('bdb.Bdb.dispatch: unknown debugging event:', repr(event))
-        return self.trace_dispatch
 
     def dispatch_line(self, frame):
         """Invoke user function and return trace function for line event.
@@ -335,12 +344,12 @@ class Bdb:
         if frame is None:
             frame = sys._getframe().f_back
         self.reset()
-        while frame:
-            frame.f_trace = self.trace_dispatch
-            self.botframe = frame
-            frame = frame.f_back
-        self.set_step()
-        self.enterframe = None
+        with self.set_enterframe(frame):
+            while frame:
+                frame.f_trace = self.trace_dispatch
+                self.botframe = frame
+                frame = frame.f_back
+            self.set_step()
         sys.settrace(self.trace_dispatch)
 
     def set_continue(self):
@@ -357,7 +366,6 @@ class Bdb:
             while frame and frame is not self.botframe:
                 del frame.f_trace
                 frame = frame.f_back
-            self.enterframe = None
 
     def set_quit(self):
         """Set quitting attribute to True.
diff --git a/Misc/NEWS.d/next/Library/2025-01-18-16-58-10.gh-issue-128991.EzJit9.rst b/Misc/NEWS.d/next/Library/2025-01-18-16-58-10.gh-issue-128991.EzJit9.rst
new file mode 100644 (file)
index 0000000..64fa04f
--- /dev/null
@@ -0,0 +1 @@
+Release the enter frame reference within :mod:`bdb` callback