diff --git a/ipykernel/debugger.py b/ipykernel/debugger.py index 5d5116995..db5fbc841 100644 --- a/ipykernel/debugger.py +++ b/ipykernel/debugger.py @@ -315,17 +315,21 @@ async def source(self, message): async def stackTrace(self, message): reply = await self._forward_message(message) - # The stackFrames array has the following content: + # The stackFrames array can have the following content: # { frames from the notebook} # ... # { 'id': xxx, 'name': '', ... } <= this is the first frame of the code from the notebook # { frames from ipykernel } # ... # {'id': yyy, 'name': '', ... } <= this is the first frame of ipykernel code - # We want to remove all the frames from ipykernel - sf_list = reply['body']['stackFrames'] - module_idx = len(sf_list) - next(i for i, v in enumerate(reversed(sf_list), 1) if v['name'] == '' and i != 1) - reply['body']['stackFrames'] = reply['body']['stackFrames'][:module_idx+1] + # or only the frames from the notebook. + # We want to remove all the frames from ipykernel when they are present. + try: + sf_list = reply['body']['stackFrames'] + module_idx = len(sf_list) - next(i for i, v in enumerate(reversed(sf_list), 1) if v['name'] == '' and i != 1) + reply['body']['stackFrames'] = reply['body']['stackFrames'][:module_idx+1] + except StopIteration: + pass return reply def accept_variable(self, variable_name):