Skip to content

Commit 24857e4

Browse files
Allow step and next to step over for loops
1 parent 5334732 commit 24857e4

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

Lib/bdb.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,12 @@ def dispatch_line(self, frame):
306306
self.user_line(). Raise BdbQuit if self.quitting is set.
307307
Return self.trace_dispatch to continue tracing in this scope.
308308
"""
309-
if self.stop_here(frame) or self.break_here(frame):
309+
# GH-136057
310+
# For line events, besides whether we should stop at the frame, we
311+
# also need to check if it's the same line as we issue the command.
312+
if (self.stop_here(frame) or self.break_here(frame)) and not (
313+
self.startframe == frame and self.startlineno == frame.f_lineno
314+
):
310315
self.user_line(frame)
311316
self.restart_events()
312317
if self.quitting: raise BdbQuit
@@ -535,7 +540,8 @@ def _set_trace_opcodes(self, trace_opcodes):
535540
if self.monitoring_tracer:
536541
self.monitoring_tracer.update_local_events()
537542

538-
def _set_stopinfo(self, stopframe, returnframe, stoplineno=0, opcode=False):
543+
def _set_stopinfo(self, stopframe, returnframe, stoplineno=0, opcode=False,
544+
startframe=None, startlineno=None):
539545
"""Set the attributes for stopping.
540546
541547
If stoplineno is greater than or equal to 0, then stop at line
@@ -548,6 +554,10 @@ def _set_stopinfo(self, stopframe, returnframe, stoplineno=0, opcode=False):
548554
# stoplineno >= 0 means: stop at line >= the stoplineno
549555
# stoplineno -1 means: don't stop at all
550556
self.stoplineno = stoplineno
557+
# startframe/startlineno is the frame/line number when the user does
558+
# step or next. We don't want to stop at the same line for those commands.
559+
self.startframe = startframe
560+
self.startlineno = startlineno
551561
self._set_trace_opcodes(opcode)
552562

553563
def _set_caller_tracefunc(self, current_frame):
@@ -573,15 +583,16 @@ def set_until(self, frame, lineno=None):
573583

574584
def set_step(self):
575585
"""Stop after one line of code."""
576-
self._set_stopinfo(None, None)
586+
self._set_stopinfo(None, None, startframe=self.enterframe,
587+
startlineno=self.enterframe.f_lineno)
577588

578589
def set_stepinstr(self):
579590
"""Stop before the next instruction."""
580591
self._set_stopinfo(None, None, opcode=True)
581592

582593
def set_next(self, frame):
583594
"""Stop on the next line in or below the given frame."""
584-
self._set_stopinfo(frame, None)
595+
self._set_stopinfo(frame, None, startframe=frame, startlineno=frame.f_lineno)
585596

586597
def set_return(self, frame):
587598
"""Stop when returning from the given frame."""

Lib/test/test_pdb.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3232,6 +3232,37 @@ def test_pdb_issue_gh_127321():
32323232
"""
32333233

32343234

3235+
def test_pdb_issue_gh_136057():
3236+
"""See GH-136057
3237+
"step" and "next" commands should be able to get over list comprehensions
3238+
>>> def test_function():
3239+
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
3240+
... lst = [i for i in range(10)]
3241+
... for i in lst: pass
3242+
3243+
>>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE
3244+
... 'next',
3245+
... 'next',
3246+
... 'step',
3247+
... 'continue',
3248+
... ]):
3249+
... test_function()
3250+
> <doctest test.test_pdb.test_pdb_issue_gh_136057[0]>(2)test_function()
3251+
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
3252+
(Pdb) next
3253+
> <doctest test.test_pdb.test_pdb_issue_gh_136057[0]>(3)test_function()
3254+
-> lst = [i for i in range(10)]
3255+
(Pdb) next
3256+
> <doctest test.test_pdb.test_pdb_issue_gh_136057[0]>(4)test_function()
3257+
-> for i in lst: pass
3258+
(Pdb) step
3259+
--Return--
3260+
> <doctest test.test_pdb.test_pdb_issue_gh_136057[0]>(4)test_function()->None
3261+
-> for i in lst: pass
3262+
(Pdb) continue
3263+
"""
3264+
3265+
32353266
def test_pdb_issue_gh_80731():
32363267
"""See GH-80731
32373268

0 commit comments

Comments
 (0)