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.

Support `__buffer__` objects in widget comm buffers (#8283)

Trevor Manz36381ea9b9ba
diff --git a/marimo/_plugins/ui/_impl/comm.py b/marimo/_plugins/ui/_impl/comm.py
index 2fe9832175..599d977deb 100644
--- a/marimo/_plugins/ui/_impl/comm.py
+++ b/marimo/_plugins/ui/_impl/comm.py
@@ -67,17 +67,34 @@ Msg = dict[str, Any]
MsgCallback = Callable[[Msg], None]
DataType = Optional[dict[str, Any]]
MetadataType = Optional[dict[str, Any]]
-BufferType = Optional[list[bytes]]
+Buffer = bytes | memoryview | bytearray
+BufferType = Optional[list[Buffer]]
+
+
+def _ensure_bytes(buf: object) -> bytes:
+ """Coerce a buffer to plain ``bytes`` for msgspec serialization.
+
+ msgspec natively handles ``bytes``, ``memoryview``, and ``bytearray``.
+ Some libraries (e.g. obstore) use custom types that hold binary data
+ but aren't subclasses of these, so msgspec can't serialize them directly.
+
+ ``bytes()`` handles memoryview/bytearray on all Python versions, and
+ on Python 3.12+ also handles any object implementing ``__buffer__``.
+ """
+ if isinstance(buf, bytes):
+ return buf
+ return bytes(buf) # type: ignore[call-overload,no-any-return]
def _create_model_message(
data: dict[str, Any],
- buffers: list[bytes],
+ buffers: list[Buffer],
) -> Optional[ModelMessage]:
"""Create the appropriate ModelMessage based on the method field.
Returns None for methods that should be skipped (e.g., echo_update).
"""
+ bbuffers = [_ensure_bytes(b) for b in buffers]
method = data.get("method", "update")
state = data.get("state", {})
buffer_paths = data.get("buffer_paths", [])
@@ -86,18 +103,18 @@ def _create_model_message(
return ModelOpen(
state=state,
buffer_paths=buffer_paths,
- buffers=buffers,
+ buffers=bbuffers,
)
elif method == "update":
return ModelUpdate(
state=state,
buffer_paths=buffer_paths,
- buffers=buffers,
+ buffers=bbuffers,
)
elif method == "custom":
return ModelCustom(
content=data.get("content"),
- buffers=buffers,
+ buffers=bbuffers,
)
elif method == "echo_update":
# echo_update is for multi-client sync acknowledgment, skip it
@@ -203,7 +220,7 @@ class MarimoComm:
def __del__(self) -> None:
self.close(deleting=True)
- def _broadcast(self, data: dict[str, Any], buffers: list[bytes]) -> None:
+ def _broadcast(self, data: dict[str, Any], buffers: list[Buffer]) -> None:
"""Broadcast a model lifecycle notification."""
message = _create_model_message(data, buffers)
if message is None:
diff --git a/tests/_plugins/ui/_impl/test_comm.py b/tests/_plugins/ui/_impl/test_comm.py
index de35dee1d4..46f0b8d014 100644
--- a/tests/_plugins/ui/_impl/test_comm.py
+++ b/tests/_plugins/ui/_impl/test_comm.py
@@ -1,5 +1,6 @@
from __future__ import annotations
+import sys
from unittest.mock import MagicMock, patch
import pytest
@@ -217,6 +218,76 @@ def test_comm_lifecycle_item_dispose_closes_comm(
mock_broadcast.assert_called_once()
+class _CustomBytes:
+ """Simulates obstore.Bytes — implements buffer protocol but not a
+ subclass of bytes/memoryview/bytearray."""
+
+ def __init__(self, data: bytes):
+ self._data = data
+
+ def __buffer__(self, flags: int = 0) -> memoryview:
+ return memoryview(self._data)
+
+
+class TestBroadcastBufferTypes:
+ """Broadcast through the comm with various buffer types and verify the
+ notification carries the right data after a serialize/deserialize
+ roundtrip."""
+
+ PAYLOAD = b"RIFF\x00\x01binary\xff\xfe"
+
+ def _roundtrip_buffer(self, comm, buf):
+ """Broadcast a buffer through the comm and return the deserialized
+ notification."""
+ from marimo._messaging.notification import ModelLifecycleNotification
+ from marimo._messaging.serde import (
+ deserialize_kernel_message,
+ serialize_kernel_message,
+ )
+
+ with patch(
+ "marimo._plugins.ui._impl.comm.broadcast_notification"
+ ) as mock_broadcast:
+ comm._broadcast(
+ {"method": "update", "state": {}, "buffer_paths": []},
+ [buf],
+ )
+ notification = mock_broadcast.call_args[0][0]
+
+ assert isinstance(notification, ModelLifecycleNotification)
+ # Full roundtrip through JSON serialization
+ raw = serialize_kernel_message(notification)
+ return deserialize_kernel_message(raw)
+
+ def test_bytes_buffer(self, comm):
+ result = self._roundtrip_buffer(comm, self.PAYLOAD)
+ assert result.message.buffers == [self.PAYLOAD]
+
+ def test_memoryview_buffer(self, comm):
+ result = self._roundtrip_buffer(comm, memoryview(self.PAYLOAD))
+ assert result.message.buffers == [self.PAYLOAD]
+
+ def test_bytearray_buffer(self, comm):
+ result = self._roundtrip_buffer(comm, bytearray(self.PAYLOAD))
+ assert result.message.buffers == [self.PAYLOAD]
+
+ @pytest.mark.skipif(
+ sys.version_info < (3, 12),
+ reason="__buffer__ dunder requires Python 3.12+",
+ )
+ def test_custom_buffer_protocol(self, comm):
+ """Custom buffer-protocol objects (like obstore.Bytes) survive the
+ roundtrip."""
+ result = self._roundtrip_buffer(comm, _CustomBytes(self.PAYLOAD))
+ assert result.message.buffers == [self.PAYLOAD]
+
+ def test_unsupported_type_raises(self):
+ from marimo._plugins.ui._impl.comm import _ensure_bytes
+
+ with pytest.raises(TypeError):
+ _ensure_bytes("not bytes")
+
+
def test_comm_lifecycle_item_dispose_idempotent(comm: MarimoComm):
"""Calling dispose twice should not error (comm.close is idempotent)."""
item = CommLifecycleItem(comm)