Implement memoryview buffers, casts, and multidimensional layouts
Adds memoryview on top of the bytearray implementation in #1208.
Depends on #1208. This draft targets main because GitHub cannot use the fork's bytearray branch as a base in this repository.
Until #1229 and #1208 merge, the Files changed tab also includes those prerequisite commits.
Review the memoryview changes separately.
Views retain their source without copying, support shared writes, strided slices, numeric casts, and zero-dimensional and multidimensional shapes.
Bytearray resizing is blocked until all exports are released or freed.
Release state, aliases, iterators, cached hashes, shapes, and strides survive snapshots, including suspended host calls.
Buffer consumers accept the supported views, and BufferError crosses the Python and JavaScript boundaries.
This PR also contains the subsequent bytearray compatibility fixes, buffer typing changes, and resource checks from the original combined PR.
Binary writes use buffer coercion while filesystem paths keep strict path coercion.
Hex formatting uses StringBuilder to preflight output allocation.
Typeshed transform regressions run through make test-type-checking and CI's existing make check-typeshed step.
Validation coverage includes:
- Monty/CPython fixture comparisons, slice checks, seeded model-based transitions, and bytearray error cases.
- Numeric formats, multidimensional layouts, callback reentrancy, release semantics, GC, and snapshots.
- Bounded-worker memory regressions, exception bridges, and buffer typing regressions.
Remaining limitations:
- Custom buffer providers (
__buffer__) and indirect buffers are unsupported. - Native formats use a fixed little-endian LP64 layout on every host.
l,L,n,N, andPare always eight bytes, which differs from CPython on Windows or 32-bit hosts where applicable. Native byte order differs on big-endian hosts. - Subscript deletion and suspendable user iteration remain unsupported.
- The type checker cannot distinguish writable and readonly views; mutation of readonly views is rejected at runtime.
- Host conversion copies bytearrays as immutable bytes; memoryviews cross as representation strings.
Part of the buffer prerequisites for #1201.