Add the `io` module with `StringIO` and `BytesIO`

Merged
RE
8d ago
+4799-311
  • io.StringIO and io.BytesIO, native, matching CPython's messages and check ordering
  • io.SEEK_SET / SEEK_CUR / SEEK_END, DEFAULT_BUFFER_SIZE, open, open_code, text_encoding, UnsupportedOperation
  • BlockingIOError as a new builtin exception (also io.BlockingIOError)
  • io.TextIOWrapper, BufferedReader, BufferedWriter, BufferedRandom exported for isinstance checks
  • json.load/dump, csv readers/writers and print(file=...) accept the in-memory streams
  • open() accepts encoding='locale'
  • limitations/io.md

šŸ¤– Generated with Claude Code


Summary by cubic

Adds the io module with native StringIO and BytesIO in-memory streams that match CPython's error messages, argument-check ordering, and newline handling.

What changed

  • io is split like CPython: native _io holds the streams, file classes, open, open_code, text_encoding, DEFAULT_BUFFER_SIZE and the io exceptions; io is CPython's vendored io.py, patched to drop the ABCs and Reader/Writer protocols.
  • StringIO and BytesIO share newline translation with open() text files; both take CPython's newline argument and support re-running __init__ to reset the stream.
  • Stream methods register through attribute tables, so unbound calls like io.BytesIO.read(b) validate their receiver as CPython does.
  • print(file=...), json.load/dump, and the csv readers and writers read and write through the streams; csv.writerows writes a stream row by row, only open() files batch rows.
  • Writes ending past ssize_t raise CPython's OverflowError instead of panicking or wrapping; the wasm harness pins the 32-bit bounds of seek() and writes, read/readline sizes search only their bounds, and a closed BytesIO reports ValueError before preflighting a write payload.
  • Stream buffers preflight their growth against the sandbox memory limit at each doubling, including through print(file=) and readlines().
  • CPython's test_memoryio suite runs 57 of its 188 tests; the rest are skipped with reasons in skips.toml.
  • Typeshed stubs are narrowed to the same surface, so absent names like FileIO and io.IOBase fail type checking.

New module surface

  • Exports SEEK_SET/SEEK_CUR/SEEK_END, DEFAULT_BUFFER_SIZE, open, open_code, text_encoding, and UnsupportedOperation (shared with open() files).
  • BlockingIOError is a new builtin exception, also exported as io.BlockingIOError; _io is listed in sys.builtin_module_names.
  • TextIOWrapper, BufferedReader, BufferedWriter, and BufferedRandom are exported for isinstance checks only; constructing them raises TypeError. FileIO, BufferedRWPair, and IncrementalNewlineDecoder are absent.
  • open() now accepts encoding='locale', which resolves to UTF-8 in the sandbox.