Vendor CPython's textwrap

Merged
R3
9d ago
+2608-25

Stacked on #1298: that PR's branch lives in a fork, so the base here is `main` and the diff also shows its commits. Only the last two commits are new:

  • `Add str.translate, str.maketrans and re.VERBOSE`
  • `Vendor CPython's textwrap`

What

`import textwrap` runs CPython 3.14's own `Lib/textwrap.py`.

  • `str.translate` / `str.maketrans` (new `types/str_translate.rs`), matching CPython's behaviour and error messages. The table is looked up with generic subscription, and the result is built with `StringBuilder`.
  • `re.VERBOSE` / `re.X` exposed, and included in the pattern repr.
  • Patch `0003-textwrap-no-del`: Monty does not parse `del` yet (#1032), and cannot read a builtin method without calling it (`patsearch = pattern.search`).
  • Registered in the type checker, JS sandbox module list and typeshed; docs and `limitations/` updated.

Divergences (documented)

  • `TextWrapper.word_punct`, `letter`, `nowhitespace` and `whitespace` stay as class attributes.
  • A `translate` mapping to a surrogate raises `ValueError`, since Monty strings cannot hold lone surrogates.
  • `bytes.translate` / `bytes.maketrans` are not implemented.

Tests

New `str__translate` and `textwrap__module` cases and `re.VERBOSE` asserts in `re__basic`, run on Monty and CPython. `cargo nextest -p monty -p monty-type-checking -p monty-types`, `make lint-rs`, `make check-stdlib`, `make check-typeshed` and `branch_diff.py --check` pass.


Summary by cubic

Vendors CPython's own pure-Python textwrap.py and its upstream test suite, so import textwrap works without a Rust port, and adds the str.translate/str.maketrans methods and re.VERBOSE/re.X that they need. The module is embedded in the binary and runs like a file module: compiled per session, cached by name, with no OS call.

Infrastructure

  • crates/monty/stdlib/vendor.toml pins CPython v3.14.10; make vendor-stdlib downloads the files and LICENSE, applies patches/*.patch, and generates the embedded source table.
  • make check-stdlib runs in CI and fails if vendored files drift from the pinned release.
  • make stdlib-candidates reports which stdlib modules the current binary can load and the first blocker of each.
  • The 68 upstream test_textwrap.py tests run on Monty and CPython; the shared unittest patch drops with_traceback(), which Monty exceptions lack, and each class lists its test methods since Monty has no dir().
  • The vendored CPython test patches are skipped in codespell, since their context lines quote upstream test code.

Divergences

  • One patch works around del not parsing (#1032) — the class-body del is dropped and del chunks[-1] becomes .pop() — and bound builtin methods not being readable without a call; dedent's error names the type via __name__ since Monty types have no __qualname__.
  • str.translate builds its output through a single memory-checked path, so both mapped values and unmapped misses stay within the resource limits, and validates its arguments before copying the receiver, so an invalid call on a string too large to copy raises TypeError rather than MemoryError.
  • A translate mapping to a surrogate code point raises ValueError, since Monty strings cannot hold lone surrogates; bytes.translate/bytes.maketrans are not implemented.