Skip to content
Merged
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
21 changes: 20 additions & 1 deletion marimo/_messaging/tracebacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,31 @@ def _highlight_traceback(traceback: str) -> str:
def write_traceback(traceback: str) -> None:
if isinstance(sys.stderr, Stderr):
sys.stderr._write_with_mimetype(
_highlight_traceback(traceback),
_highlight_traceback(_trim_traceback(traceback)),
mimetype="application/vnd.marimo+traceback",
)
else:
sys.stderr.write(traceback)


def _trim_traceback(traceback: str) -> str:
"""
Skip first DefaultExecutor.execute_cell traceback item which all traces start with.
"""

lines = traceback.split("\n")
if (
len(lines) > 2
and lines[0] == "Traceback (most recent call last):"
and '/marimo/_runtime/executor.py", line ' in lines[1]
and lines[1].endswith(", in execute_cell")
):
for i in range(2, len(lines)):
if lines[i].startswith(" File "):
return "\n".join(lines[:1] + lines[i:])

return traceback


def is_code_highlighting(value: str) -> bool:
return 'class="codehilite"' in value
9 changes: 9 additions & 0 deletions tests/_messaging/test_tracebacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from marimo._messaging.tracebacks import (
_highlight_traceback,
_trim_traceback,
is_code_highlighting,
write_traceback,
)
Expand Down Expand Up @@ -79,3 +80,11 @@ def test_is_code_highlighting(self) -> None:
assert is_code_highlighting("<span>code</span>") is False
assert is_code_highlighting("") is False
assert is_code_highlighting('class="not-codehilite"') is False

def test_trim(self) -> None:
prefix = "Traceback (most recent call last):\n"
head = ' File ".../marimo/_runtime/executor.py", line 139, in execute_cell\n return eval(cell.last_expr, glbls)\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^\n'
rest = (
' File ".../__marimo__cell_Hbol_.py", line 2, in <module>\n...\n'
)
assert _trim_traceback(f"{prefix}{head}{rest}") == f"{prefix}{rest}"
Loading