Skip to content

gh-136057: Allow step and next to step over for loops #136160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

gaogaotiantian
Copy link
Member

@gaogaotiantian gaogaotiantian commented Jul 1, 2025

Due to historical reasons, sys.settrace issues a line event for for loops even if the jump target is on the same line:

# 10 line events on this line
for _ in range(10): pass

We don't want to touch this behavior, because it's been like this for like forever.

However, it's not desirable to pdb. When the user does next or step command, they want to step over this line and reach the next line.

Our documentation also clearly states such behavior:

s(tep)
Execute the current line, stop at the first possible occasion (either in a function that is called or on the next line in the current function).

n(ext)
Continue execution until the next line in the current function is reached or it returns. (The difference between next and step is that step stops inside a called function, while next executes called functions at (nearly) full speed, only stopping at the next line in the current function.)

So I consider the current behavior as a bug, which we should fix.

A thought might be - why don't we check this in stop_here? The reason is because dispatch_exception also uses stop_here and we should trigger user_exception even if it's on the same line.

An extra check in dispatch_line is the cleanest way I can think of to make this happen - only line events should be dealt with. We don't need to worry about the extra reference to a frame because the eventual continue (or anything other than step or next) will clear this reference.

Let me know if anyone has a better idea of how to implement this.

if self.stop_here(frame) or self.break_here(frame):
# GH-136057
# For line events, besides whether we should stop at the frame, we
# also need to check if it's the same line as we issue the command.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is very clear: "it's the same line as we issue the command". I think you mean "the same frame and line at which the break command was issued by the user".

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not the break command, it's either next or step. But yes I'm open to all suggestions to make this clearer.

@@ -548,6 +554,10 @@ def _set_stopinfo(self, stopframe, returnframe, stoplineno=0, opcode=False):
# stoplineno >= 0 means: stop at line >= the stoplineno
# stoplineno -1 means: don't stop at all
self.stoplineno = stoplineno
# startframe/startlineno is the frame/line number when the user does
# step or next. We don't want to stop at the same line for those commands.
self.startframe = startframe
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could pick a more informative name, something like "command frame", because it's ambiguous here what exactly started at that frame.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah we can change it, but there's actually a caveat here - it's not really the command frame. You can do up to an upper frame and do next/step - the behavior is different. next will next to the line at the upper frame, so the frame we need is the "command frame". step however, will stop at the innermost frame, no matter which frame you are inspecting - that's just how step works.

So it's more like a "do not stop here again if it's a line event" frame/lineno. Do you think there's better name to describe it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants