Implement the `csv` module
Implements the csv module: reader, writer, DictReader, DictWriter, Error, the QUOTE_* constants, the three built-in dialects, get_dialect, list_dialects and a read-only field_size_limit. Stacked on #1001, which it needs for open() files to be iterable.
The parser and writer are ports of CPython's _csv.c state machine and join_append_data, so quoting, escaping, skipinitialspace, strict, the QUOTE_* conversions and the error messages match CPython 3.14. DictReader and DictWriter follow csv.py.
How a file gets read. A reader cannot load an unread open() file from __next__, so csv.reader(f) and csv.DictReader(f) yield the host read with a new LoadThenCall effect and the construction is replayed once the buffer is in place. Every consumer then works: list(csv.reader(f)), next(reader), for row in csv.DictReader(f). The effect is general (it stores a callable plus its ArgValues), so it is also the mechanism the TODO on unloaded_iteration_error describes for list(f) and friends; this PR wires it only for csv.
Writers. writerow returns whatever the target's write() returns, yielding the host write for a file. writerows on a file sends all rows in one host write (WritePosition gained a result_none flag so it still answers None); on any other target write() runs synchronously per row.
Not implemented, documented in limitations/csv.md: register_dialect / unregister_dialect (no module state), Sniffer, changing field_size_limit. The built-in dialects are _csv.Dialect objects rather than classes.
Each commit is one layer: the exception type, the VM effect, the module, tests, type stubs, docs. Note the heap/mod.rs change is two one-line match arms for the new HeapData::Csv payload (GC trace and dec-ref), the same shape as Itertools.
Also touched: _csv.Error crosses the Python and JS bindings as csv.Error; the Type fingerprint in dump_format moved because five variants were appended (additive, no DUMP_VERSION bump).
The four CPython-side case failures in str__format.py, str__percent_format.py, fstring__all.py and bytes__percent_format.py ('{0:.2147482623g}' precision edge cases) also fail when the unchanged test files are run directly under this machine's CPython 3.14.7, so they are unrelated to this branch.
🤖 Generated with Claude Code
https://claude.ai/code/session_01VMK9rG1LtuF1pugGYcHdHC
Summary by cubic
Implements the csv module: reader, writer, DictReader, DictWriter, the QUOTE_* constants, the three built-in dialects, get_dialect, list_dialects, Error and a read-only field_size_limit. The parser and writer port CPython's _csv.c state machine, so quoting, escaping, QUOTE_* conversions, underscore digit separators and error messages match CPython 3.14; DictReader/DictWriter follow csv.py. Adds csv.Error (_csv.Error), a direct Exception subclass, as a new exception type across the Python and JS bindings.
Reading files and matching CPython edges
reader(f)andDictReader(f)on an unreadopen()file yield the host read through a newLoadThenCalleffect and replay construction once the buffer is in place, solist(...),next(...)andforloops all work; replays ending in an uncaught error get the per-turn resource-limit re-check.writerowson a file sends all rows in one host write; other targets stream rows, polling the clock through a retained iterator view.- The writer also polls the clock per 64 KiB chunk of a plain field, so a long ordinary field cannot outrun the time limit.
get_dialect(name=...)andfield_size_limit(new_limit=...)go throughFromArgs, so keyword forms and arity errors match CPython.float()accepts underscore digit separators under CPython's rule, shared with theQUOTE_NONNUMERICreader.register_dialect,unregister_dialectandSnifferare absent,field_size_limitis read-only, and the built-in dialects are_csv.Dialectobjects rather than classes; the gaps are documented indocs/limitations/csv.md, and hand-written type stubs reject the absent surface up front.
Performance work
- The parser is about a third faster and the writer about a quarter faster: in-place line parsing, one field buffer reused across fields, exact-size allocations, and bulk copies for list/tuple rows and runs of ordinary characters.
DictReadertakes the field vector straight from the reader and sizes rows by the previous row's key count;DictWriterreadsfieldnames,restvalandextrasactionper row and shares the dialect through anArc.- Adds read and write benchmarks and limits variants in
crates/monty-bench.