Add the `io` module with `StringIO` and `BytesIO`
Merged
RE
8d ago+4799+4799+0123456789012345678901234567890123456789-311-311-012345678901234567890123456789
io.StringIOandio.BytesIO, native, matching CPython's messages and check orderingio.SEEK_SET/SEEK_CUR/SEEK_END,DEFAULT_BUFFER_SIZE,open,open_code,text_encoding,UnsupportedOperationBlockingIOErroras a new builtin exception (alsoio.BlockingIOError)io.TextIOWrapper,BufferedReader,BufferedWriter,BufferedRandomexported forisinstancechecksjson.load/dump,csvreaders/writers andprint(file=...)accept the in-memory streamsopen()acceptsencoding='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
iois split like CPython: native_ioholds the streams, file classes,open,open_code,text_encoding,DEFAULT_BUFFER_SIZEand the io exceptions;iois CPython's vendoredio.py, patched to drop the ABCs andReader/Writerprotocols.StringIOandBytesIOshare newline translation withopen()text files; both take CPython'snewlineargument 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 thecsvreaders and writers read and write through the streams;csv.writerowswrites a stream row by row, onlyopen()files batch rows.- Writes ending past
ssize_traise CPython'sOverflowErrorinstead of panicking or wrapping; the wasm harness pins the 32-bit bounds ofseek()and writes,read/readlinesizes search only their bounds, and a closedBytesIOreportsValueErrorbefore preflighting a write payload. - Stream buffers preflight their growth against the sandbox memory limit at each doubling, including through
print(file=)andreadlines(). - CPython's
test_memoryiosuite runs 57 of its 188 tests; the rest are skipped with reasons inskips.toml. - Typeshed stubs are narrowed to the same surface, so absent names like
FileIOandio.IOBasefail type checking.
New module surface
- Exports
SEEK_SET/SEEK_CUR/SEEK_END,DEFAULT_BUFFER_SIZE,open,open_code,text_encoding, andUnsupportedOperation(shared withopen()files). BlockingIOErroris a new builtin exception, also exported asio.BlockingIOError;_iois listed insys.builtin_module_names.TextIOWrapper,BufferedReader,BufferedWriter, andBufferedRandomare exported forisinstancechecks only; constructing them raisesTypeError.FileIO,BufferedRWPair, andIncrementalNewlineDecoderare absent.open()now acceptsencoding='locale', which resolves to UTF-8 in the sandbox.