Skip to content

Commit ba6e6fd

Browse files
Improve bdb breakpoint check
1 parent 28efeef commit ba6e6fd

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

Lib/bdb.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import fnmatch
44
import sys
55
import os
6+
import weakref
67
from inspect import CO_GENERATOR, CO_COROUTINE, CO_ASYNC_GENERATOR
78

89
__all__ = ["BdbQuit", "Bdb", "Breakpoint"]
@@ -36,6 +37,7 @@ def __init__(self, skip=None):
3637
self.frame_returning = None
3738
self.trace_opcodes = False
3839
self.enterframe = None
40+
self.code_lineno = weakref.WeakKeyDictionary()
3941

4042
self._load_breaks()
4143

@@ -155,6 +157,9 @@ def dispatch_return(self, frame, arg):
155157
if self.stop_here(frame) or frame == self.returnframe:
156158
# Ignore return events in generator except when stepping.
157159
if self.stopframe and frame.f_code.co_flags & GENERATOR_AND_COROUTINE_FLAGS:
160+
# It's possible to trigger a StopIteration exception in
161+
# the caller so we must set the trace function in the caller
162+
self._set_caller_tracefunc(frame)
158163
return self.trace_dispatch
159164
try:
160165
self.frame_returning = frame
@@ -275,7 +280,25 @@ def do_clear(self, arg):
275280
def break_anywhere(self, frame):
276281
"""Return True if there is any breakpoint for frame's filename.
277282
"""
278-
return self.canonic(frame.f_code.co_filename) in self.breaks
283+
filename = self.canonic(frame.f_code.co_filename)
284+
if filename not in self.breaks:
285+
return False
286+
for lineno in self.breaks[filename]:
287+
if self.lineno_in_frame(lineno, frame):
288+
return True
289+
return False
290+
291+
def lineno_in_frame(self, lineno, frame):
292+
"""Return True if the line number is in the frame's code object.
293+
"""
294+
code = frame.f_code
295+
if lineno < code.co_firstlineno:
296+
return False
297+
if code not in self.code_lineno:
298+
self.code_lineno[code] = set()
299+
for _, _, lineno in code.co_lines():
300+
self.code_lineno[code].add(lineno)
301+
return lineno in self.code_lineno[frame.f_code]
279302

280303
# Derived classes should override the user_* methods
281304
# to gain control.
@@ -360,7 +383,7 @@ def set_next(self, frame):
360383
def set_return(self, frame):
361384
"""Stop when returning from the given frame."""
362385
if frame.f_code.co_flags & GENERATOR_AND_COROUTINE_FLAGS:
363-
self._set_stopinfo(frame, None, -1)
386+
self._set_stopinfo(frame, frame, -1)
364387
else:
365388
self._set_stopinfo(frame.f_back, frame)
366389

0 commit comments

Comments
 (0)