Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: make hooks robust to base exceptions (#1713)

Akshay Agrawal51ff2a674363
diff --git a/marimo/_messaging/ops.py b/marimo/_messaging/ops.py
index 9be39c397a..869177a9a2 100644
--- a/marimo/_messaging/ops.py
+++ b/marimo/_messaging/ops.py
@@ -440,7 +440,12 @@ class VariableValue:
self.value = None
def _stringify(self, value: object) -> str:
- return str(value)[:50]
+ try:
+ return str(value)[:50]
+ except BaseException:
+ # Catch-all: some libraries like Polars have bugs and raise
+ # BaseExceptions, which shouldn't crash the kernel
+ return "<UNKNOWN>"
def _format_value(self, value: object) -> str:
resolved = value
diff --git a/marimo/_output/formatting.py b/marimo/_output/formatting.py
index 1ffc788206..eeb062ca2e 100644
--- a/marimo/_output/formatting.py
+++ b/marimo/_output/formatting.py
@@ -166,7 +166,8 @@ def try_format(obj: Any) -> FormattedOutput:
try:
mimetype, data = formatter(obj)
return FormattedOutput(mimetype=mimetype, data=data)
- except Exception: # noqa: E722
+ except BaseException: # noqa: E722
+ # Catching base exception so we're robust to bugs in libraries
return FormattedOutput(
mimetype="text/plain",
data="",
diff --git a/marimo/_smoke_tests/bugs/1706.py b/marimo/_smoke_tests/bugs/1706.py
index 2baf24ffc5..d025789e00 100644
--- a/marimo/_smoke_tests/bugs/1706.py
+++ b/marimo/_smoke_tests/bugs/1706.py
@@ -1,3 +1,4 @@
+# Copyright 2024 Marimo. All rights reserved.
import marimo
__generated_with = "0.6.25"
diff --git a/marimo/_smoke_tests/bugs/1711.py b/marimo/_smoke_tests/bugs/1711.py
new file mode 100644
index 0000000000..0fce81e78f
--- /dev/null
+++ b/marimo/_smoke_tests/bugs/1711.py
@@ -0,0 +1,27 @@
+import marimo
+
+__generated_with = "0.6.25"
+app = marimo.App()
+
+
+def __():
+ import polars as pl
+ import numpy as np
+ d = pl.DataFrame({"a": [np.arange(5) + i for i in range(5)]})
+ res = d.select(
+ pl.col("a").map_batches(
+ lambda x: {"filt_value": np.dot(x, x), "filt_phase": 5.0},
+ is_elementwise=True),
+ )
+ return d, np, pl, res
+
+
+def __(res):
+ res
+ return
+
+
+if __name__ == "__main__":
+ app.run()
diff --git a/tests/_messaging/test_ops.py b/tests/_messaging/test_ops.py
index 1c9a4cdce0..79e4ff75b5 100644
--- a/tests/_messaging/test_ops.py
+++ b/tests/_messaging/test_ops.py
@@ -15,3 +15,13 @@ def test_value_html() -> None:
variable_value = VariableValue(name="h", value=h)
assert variable_value.datatype == "Html"
assert variable_value.value == h.text
+
+
+def test_variable_value_broken_str() -> None:
+ class Broken:
+ def __str__(self) -> str:
+ raise BaseException
+
+ variable_value = VariableValue(name="o", value=Broken())
+ assert variable_value.datatype == "Broken"
+ assert variable_value.value == "<UNKNOWN>"
diff --git a/tests/_output/formatters/test_formatters.py b/tests/_output/formatters/test_formatters.py
index b02af265e6..c74a6fd762 100644
--- a/tests/_output/formatters/test_formatters.py
+++ b/tests/_output/formatters/test_formatters.py
@@ -6,8 +6,15 @@ import os.path
import pytest
from marimo._dependencies.dependencies import DependencyManager
+from marimo._messaging.mimetypes import KnownMimeType
from marimo._output.formatters.formatters import register_formatters
-from marimo._output.formatting import Plain, as_html, get_formatter
+from marimo._output.formatting import (
+ Plain,
+ as_html,
+ formatter,
+ get_formatter,
+ try_format,
+)
def test_path_finder_find_spec() -> None:
@@ -103,3 +110,18 @@ def test_as_html_opinionated_formatter():
# With polars DataFrame + Plain
html = as_html(Plain(pl_df))
assert "<marimo-table" not in html.text
+
+
+def test_broken_formatter():
+ class _ClsForBrokenFormatter: ...
+
+ def _format(cls: _ClsForBrokenFormatter) -> tuple[KnownMimeType, str]:
+ del cls
+ raise BaseException("Broken Formatter")
+
+ formatter(_ClsForBrokenFormatter)(_format)
+
+ obj = _ClsForBrokenFormatter()
+ formatted = try_format(obj)
+ assert formatted.traceback is not None
+ assert "Broken Formatter" in formatted.traceback