Skip to content

Commit 6d638c2

Browse files
[3.12] gh-128991: Release the enter frame reference within bdb callba… (#129003)
[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 61b35f7)
1 parent ae8c8b7 commit 6d638c2

File tree

2 files changed

+33
-24
lines changed

2 files changed

+33
-24
lines changed

Lib/bdb.py

+32-24
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import fnmatch
44
import sys
55
import os
6+
from contextlib import contextmanager
67
from inspect import CO_GENERATOR, CO_COROUTINE, CO_ASYNC_GENERATOR
78

89
__all__ = ["BdbQuit", "Bdb", "Breakpoint"]
@@ -60,6 +61,12 @@ def reset(self):
6061
self.botframe = None
6162
self._set_stopinfo(None, None)
6263

64+
@contextmanager
65+
def set_enterframe(self, frame):
66+
self.enterframe = frame
67+
yield
68+
self.enterframe = None
69+
6370
def trace_dispatch(self, frame, event, arg):
6471
"""Dispatch a trace function for debugged frames based on the event.
6572
@@ -84,24 +91,26 @@ def trace_dispatch(self, frame, event, arg):
8491
8592
The arg parameter depends on the previous event.
8693
"""
87-
if self.quitting:
88-
return # None
89-
if event == 'line':
90-
return self.dispatch_line(frame)
91-
if event == 'call':
92-
return self.dispatch_call(frame, arg)
93-
if event == 'return':
94-
return self.dispatch_return(frame, arg)
95-
if event == 'exception':
96-
return self.dispatch_exception(frame, arg)
97-
if event == 'c_call':
98-
return self.trace_dispatch
99-
if event == 'c_exception':
100-
return self.trace_dispatch
101-
if event == 'c_return':
94+
95+
with self.set_enterframe(frame):
96+
if self.quitting:
97+
return # None
98+
if event == 'line':
99+
return self.dispatch_line(frame)
100+
if event == 'call':
101+
return self.dispatch_call(frame, arg)
102+
if event == 'return':
103+
return self.dispatch_return(frame, arg)
104+
if event == 'exception':
105+
return self.dispatch_exception(frame, arg)
106+
if event == 'c_call':
107+
return self.trace_dispatch
108+
if event == 'c_exception':
109+
return self.trace_dispatch
110+
if event == 'c_return':
111+
return self.trace_dispatch
112+
print('bdb.Bdb.dispatch: unknown debugging event:', repr(event))
102113
return self.trace_dispatch
103-
print('bdb.Bdb.dispatch: unknown debugging event:', repr(event))
104-
return self.trace_dispatch
105114

106115
def dispatch_line(self, frame):
107116
"""Invoke user function and return trace function for line event.
@@ -335,12 +344,12 @@ def set_trace(self, frame=None):
335344
if frame is None:
336345
frame = sys._getframe().f_back
337346
self.reset()
338-
while frame:
339-
frame.f_trace = self.trace_dispatch
340-
self.botframe = frame
341-
frame = frame.f_back
342-
self.set_step()
343-
self.enterframe = None
347+
with self.set_enterframe(frame):
348+
while frame:
349+
frame.f_trace = self.trace_dispatch
350+
self.botframe = frame
351+
frame = frame.f_back
352+
self.set_step()
344353
sys.settrace(self.trace_dispatch)
345354

346355
def set_continue(self):
@@ -357,7 +366,6 @@ def set_continue(self):
357366
while frame and frame is not self.botframe:
358367
del frame.f_trace
359368
frame = frame.f_back
360-
self.enterframe = None
361369

362370
def set_quit(self):
363371
"""Set quitting attribute to True.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Release the enter frame reference within :mod:`bdb` callback

0 commit comments

Comments
 (0)