Skip to content

feat: MarshalBinary/UnmarshalBinary, NumPy-style Repr, notebook Display - #44

Merged
tannevaled merged 1 commit into
mainfrom
feat/marshal-display
Oct 9, 2026
Merged

tannevaled merged 1 commit into
mainfrom
feat/marshal-display

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

Two things a notebook needs from an array.

Keeping it. MarshalBinary/UnmarshalBinary (encoding.BinaryMarshaler): magic + version, dtype, shape as uvarints, then the elements row-major and little-endian, the same bytes on every architecture (s390x included; a test pins them). UnmarshalBinary treats its input as untrusted: the shape must account exactly for the data length (checked by division, since size×16 overflows for complex128 shapes validateShape allows) before anything is allocated. With this, gob keeps arrays and the go-notebook kernel (go-notebook/notebook#1) restores them after a rebuild instead of re-running the cell that made them.

Showing it. Repr prints as NumPy does: nested brackets, one width for all elements, 75-column lines, the first and last three items of each axis above 1000 elements, then dtype=/shape= suffixes. Display returns that as text/plain plus, for 1-D and 2-D, an HTML table summarised the same way. Element digits use the package's shortest exact form, so they can differ from NumPy's padded precision; the layout matches.

Tests: round trip for every dtype (views, 0-d, empty), pinned bytes, 13 malformed inputs, gob, layouts. Negative control: a big-endian encoder fails 5 tests. 100% coverage.

🤖 Generated with Claude Code

MarshalBinary writes dtype, shape and row-major little-endian elements,
the same bytes on every architecture; UnmarshalBinary checks the shape
against the data length (by division: size*16 overflows for complex128)
before allocating, so bytes from anywhere cannot make it allocate what
they do not carry. Arrays now survive gob, and the go-notebook kernel
keeps them instead of re-running the cell that made them.

Repr lays an array out as NumPy prints it: nested brackets, one width for
every element, 75-column lines, first/last three items per axis above
1000 elements, dtype and shape suffixes. Display returns it as text/plain
and, for 1-D and 2-D, an HTML table summarised the same way.

Tests: round trip of every dtype incl. views, 0-d and empty arrays;
pinned bytes; 13 malformed inputs; gob; repr layouts. An encoder switched
to big-endian fails 5 of them. 100% coverage under the CI gate.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@tannevaled
tannevaled merged commit d032aeb into main Oct 9, 2026
12 checks passed
@tannevaled
tannevaled deleted the feat/marshal-display branch October 9, 2026 19:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant