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 all 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
41 changes: 30 additions & 11 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,20 +216,26 @@ 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
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 not stack:
return

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
Expand All @@ -238,12 +244,10 @@ def set_trace(self, frame=None, as_breakpoint=None, paused=True):
self.ui.source_code_provider, force_update=True)

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

def save_breakpoints(self):
from pudb.settings import save_breakpoints
Expand Down Expand Up @@ -313,6 +317,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