Skip to content
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
16 changes: 16 additions & 0 deletions ipykernel/debugger.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@
if t.TYPE_CHECKING:
from IPython.core.interactiveshell import InteractiveShell

# Modules that IPython imports lazily when formatting a traceback (since
# IPython 9.17, see https://github.com/ipython/ipython/pull/15333): they are
# not loaded when the kernel starts, so their files are not in
# `kernel_modules` and the debugger would otherwise stop in them when the
# `raised` exception filter is on. They are skipped by module name so that
# submodules imported later, such as the pygments lexers and formatters, are
# covered too, without importing anything at kernel startup.
# See https://github.com/ipython/ipykernel/issues/1596
_TRACEBACK_FORMATTER_MODULES = ("asttokens", "executing", "pure_eval", "pygments", "stack_data")

# Required for backwards compatibility
ROUTING_ID = getattr(zmq, "ROUTING_ID", None) or zmq.IDENTITY

Expand Down Expand Up @@ -598,6 +608,12 @@ async def attach(self, message):
# Dynamic skip rules (computed at kernel startup)
if self.filter_internal_frames:
rules = [{"path": path, "include": False} for path in self.kernel_modules]
# Modules imported lazily by IPython when formatting a traceback
# are not in `kernel_modules`; skip them by module name so that
# submodules imported after the debugger attaches are covered too.
rules.extend(
{"module": module, "include": False} for module in _TRACEBACK_FORMATTER_MODULES
)
message["arguments"]["rules"] = rules

return await self._forward_message(message)
Expand Down
37 changes: 37 additions & 0 deletions tests/test_debugger.py
Original file line number Diff line number Diff line change
Expand Up @@ -684,3 +684,40 @@ def test_debug_requests_sequential(kernel_with_debug):
assert execution_states.count("busy") == 4
assert execution_states.count("idle") == 4
assert execution_states == ["busy", "idle"] * 4


def test_no_stop_in_traceback_formatter_on_raised_exception(kernel_with_debug):
# With the `raised` exception filter on, the debugger must stop where the
# user code raises, but not in the code IPython runs to show the traceback
# (pygments, stack_data, executing, asttokens), which IPython imports
# lazily when formatting the first traceback.
# See https://github.com/ipython/ipykernel/issues/1596
if not debugpy:
return

wait_for_debug_request(kernel_with_debug, "setExceptionBreakpoints", {"filters": ["raised"]})

wait_for_debug_request(kernel_with_debug, "configurationDone", full_reply=True)

msg_id = kernel_with_debug.execute('import json\njson.loads("x")')

stops = []
msg: dict = {"msg_type": "", "content": {}}
while True:
msg = kernel_with_debug.get_iopub_msg(timeout=TIMEOUT)
if msg["msg_type"] == "debug_event" and msg["content"].get("event") == "stopped":
thread_id = msg["content"]["body"]["threadId"]
frames = wait_for_debug_request(
kernel_with_debug, "stackTrace", {"threadId": thread_id}
)["body"]["stackFrames"]
stops.append(frames[0])
wait_for_debug_request(kernel_with_debug, "continue", {"threadId": thread_id})
elif (
msg["parent_header"].get("msg_id") == msg_id
and msg["content"].get("execution_state") == "idle"
):
break

assert len(stops) == 1
assert stops[0]["name"] == "<module>"
assert "site-packages" not in stops[0]["source"]["path"]
Loading