@@ -326,7 +326,15 @@ class Debugger:
326326 ]
327327
328328 def __init__ (
329- self , log , debugpy_stream , event_callback , shell_socket , session , just_my_code = True
329+ self ,
330+ log ,
331+ debugpy_stream ,
332+ event_callback ,
333+ shell_socket ,
334+ session ,
335+ kernel_modules ,
336+ just_my_code = False ,
337+ filter_internal_frames = True ,
330338 ):
331339 """Initialize the debugger."""
332340 self .log = log
@@ -335,7 +343,9 @@ def __init__(
335343 self .session = session
336344 self .is_started = False
337345 self .event_callback = event_callback
346+ self .kernel_modules = kernel_modules
338347 self .just_my_code = just_my_code
348+ self .filter_internal_frames = filter_internal_frames
339349 self .stopped_queue : Queue [t .Any ] = Queue ()
340350
341351 self .started_debug_handlers = {}
@@ -498,25 +508,7 @@ async def source(self, message):
498508
499509 async def stackTrace (self , message ):
500510 """Handle a stack trace message."""
501- reply = await self ._forward_message (message )
502- # The stackFrames array can have the following content:
503- # { frames from the notebook}
504- # ...
505- # { 'id': xxx, 'name': '<module>', ... } <= this is the first frame of the code from the notebook
506- # { frames from ipykernel }
507- # ...
508- # {'id': yyy, 'name': '<module>', ... } <= this is the first frame of ipykernel code
509- # or only the frames from the notebook.
510- # We want to remove all the frames from ipykernel when they are present.
511- try :
512- sf_list = reply ["body" ]["stackFrames" ]
513- module_idx = len (sf_list ) - next (
514- i for i , v in enumerate (reversed (sf_list ), 1 ) if v ["name" ] == "<module>" and i != 1
515- )
516- reply ["body" ]["stackFrames" ] = reply ["body" ]["stackFrames" ][: module_idx + 1 ]
517- except StopIteration :
518- pass
519- return reply
511+ return await self ._forward_message (message )
520512
521513 def accept_variable (self , variable_name ):
522514 """Accept a variable by name."""
@@ -574,6 +566,12 @@ async def attach(self, message):
574566 # Set debugOptions for breakpoints in python standard library source.
575567 if not self .just_my_code :
576568 message ["arguments" ]["debugOptions" ] = ["DebugStdLib" ]
569+
570+ # Dynamic skip rules (computed at kernel startup)
571+ if self .filter_internal_frames :
572+ rules = [{"path" : path , "include" : False } for path in self .kernel_modules ]
573+ message ["arguments" ]["rules" ] = rules
574+
577575 return await self ._forward_message (message )
578576
579577 async def configurationDone (self , message ):
0 commit comments