Skip to content
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

Added option to hide importlib frames in stack #246

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 42 additions & 25 deletions pudb/debugger.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def set_continue(self):
if not self.breaks:
# no breakpoints; run without debugger overhead
sys.settrace(None)
frame = sys._getframe().f_back
frame = sys._getframe(1)
while frame:
del frame.f_trace
if frame is self.botframe:
Expand Down Expand Up @@ -216,34 +216,36 @@ def set_trace(self, frame=None, as_breakpoint=None, paused=True):
as_breakpoint = True

if frame is None:
frame = thisframe = sys._getframe().f_back
else:
thisframe = frame
frame = sys._getframe(1)

# See pudb issue #52. If this works well enough we should upstream to
# stdlib bdb.py.
#self.reset()

while frame:
frame.f_trace = self.trace_dispatch
self.botframe = frame
frame = frame.f_back

thisframe_info = (
self.canonic(thisframe.f_code.co_filename), thisframe.f_lineno)
if thisframe_info not in self.set_traces or self.set_traces[thisframe_info]:
if as_breakpoint:
self.set_traces[thisframe_info] = True
if self.ui.source_code_provider is not None:
self.ui.set_source_code_provider(
self.ui.source_code_provider, force_update=True)

if paused:
self.set_step()
else:
self.set_continue()
sys.settrace(self.trace_dispatch)
else:
return
self.botframe = frame
self.botframe.f_trace = self.trace_dispatch
while self.botframe.f_back:
self.botframe = self.botframe.f_back
self.botframe.f_trace = self.trace_dispatch

stack, _ = self.get_stack(frame, None)
if stack:
Copy link
Collaborator

Choose a reason for hiding this comment

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

When is this empty?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well. Normally it shouldn't. But what if it would happen that all of frames were called from importlib._bootstrap? Sounds weird, but I don't think we have guarantee that stack list has at least one element.

Copy link
Collaborator

Choose a reason for hiding this comment

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

OK, the check is fine. I agree it shouldn't happen, unless you do something really weird (like inject a set_trace in the importlib._bootstrap code). Might make the code easier to read if you inverted the check to avoid indenting the whole block:

if not stack:
    return
...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. Thanks for idea.

thisframe, _ = stack[-1]
thisframe_info = (
self.canonic(thisframe.f_code.co_filename), thisframe.f_lineno)

if thisframe_info not in self.set_traces or self.set_traces[thisframe_info]:
if as_breakpoint:
self.set_traces[thisframe_info] = True
if self.ui.source_code_provider is not None:
self.ui.set_source_code_provider(
self.ui.source_code_provider, force_update=True)

if paused:
self.set_next(thisframe)
else:
self.set_continue()
sys.settrace(self.trace_dispatch)

def save_breakpoints(self):
from pudb.settings import save_breakpoints
Expand Down Expand Up @@ -313,6 +315,21 @@ def move_down_frame(self):
if self.curindex < len(self.stack)-1:
self.set_frame_index(self.curindex+1)

def get_stack(self, f, t):
stack, index = bdb.Bdb.get_stack(self, f, t)

if CONFIG['hide_importlib_frames']:
# see Python/import.c remove_importlib_frames function for more info
hidden_filenames = [
"<frozen importlib._bootstrap>",
"<frozen importlib._bootstrap_external>",
]
unhidden_len = len(stack)
stack = [f for f in stack if f[0].f_code.co_filename not in hidden_filenames]
index -= unhidden_len - len(stack)

return stack, index

def get_shortened_stack(self, frame, tb):
stack, index = self.get_stack(frame, tb)

Expand Down
3 changes: 3 additions & 0 deletions pudb/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ def load_config():

conf_dict.setdefault("prompt_on_quit", True)

conf_dict.setdefault("hide_importlib_frames", True)

def normalize_bool_inplace(name):
try:
if conf_dict[name].lower() in ["0", "false", "off"]:
Expand All @@ -96,6 +98,7 @@ def normalize_bool_inplace(name):
normalize_bool_inplace("line_numbers")
normalize_bool_inplace("wrap_variables")
normalize_bool_inplace("prompt_on_quit")
normalize_bool_inplace("hide_importlib_frames")

return conf_dict

Expand Down