implement `tomllib`

Merged
SA
2mo ago
+2839-71

Summary by cubic

Implements the standard-library tomllib module with loads(), load() and TOMLDecodeError, ported from CPython's parser so the accepted grammar, namespace rules, and every error message and position match CPython 3.14.

  • TOMLDecodeError is a ValueError subclass carrying the same five attributes as json.JSONDecodeError and surfaces through the same payload in the JS and Python hosts; the Python binding rebuilds the attributes on hosts before 3.14 and falls back to ValueError when tomllib is absent. The wire sanitizer sizes msg from the exception's message — normally a strict prefix of it — so payloads naming long keys survive the trip, and accepts message-less payloads with msg capped at the document limit.
  • Parsing builds a Rust tree before heap conversion and polls the resource tracker per statement, per array element and per inline-table pair to bound memory use, preflighting array, table, namespace-flag and pending-flag growth like json.loads; inline tables and arrays nested beyond 100 levels raise RecursionError, and a pos one past the document is accepted for end-of-input escapes. Tree cleanup after a MemoryError is bounded by depth, and a backslash escape steps over a whole multibyte character, matching CPython's error position.
  • load() reads binary file objects through the same load-then-replay effect csv.reader uses for unread files; it raises TypeError when parse_float is passed.
  • Vendors a narrowed typeshed stub, reserves tomllib in the sandbox module lists, adds dual-run test cases against CPython, and documents the module's divergences in the limitations pages.