Add sys.intern

JA
8d ago
+328-61

Summary

Add sys.intern(string, /) using Monty’s session string table. Equal strings passed to sys.intern share a string ID with
each other and with compiled literals of the same text.

Monty retains new entries for the lifetime of the session. The existing worker memory budget accounts for the entries.

Tests

The import__sys.py fixture exercises identity for runtime strings and literals, including Unicode text, and checks
CPython’s errors for invalid argument types and call shapes. The type-checking fixture checks the stub’s return type.

All 1,632 fixture cases passed with CPython 3.14.10. The production build check, type-checking suite, dump compatibility
tests, Clippy, and Ruff passed.

  • I have read the contribution guide.
  • I understand and can explain the proposed changes, including any generated with AI assistance.

Summary by cubic

Adds sys.intern(string, /) to Monty's production sys module. Equal strings passed to it share identity while the runtime intern stays alive, so repeated calls return the same object.

  • Runtime interns live in a weak index keyed by a private per-heap hash seed and are released when the string dies, including through collected cycles; a literal compiled later keeps its own identity until sys.intern is called on it.
  • Existing compiled literals are reused, short text returns an inline string that can differ in identity from a literal, and allocation is preflighted against the worker memory budget.
  • Raises TypeError matching CPython for non-string arguments, wrong arity, and keyword arguments.
  • Sys module functions are now available in production builds; setrecursionlimit stays behind test-hooks.
  • Updates type stubs, type-checking, runtime allocation and subprocess memory-budget tests, dump-compat lifetime tests, and the sys limitations doc, which now spells out the identity rules and the fixed string and bytes hash seeds behind sys.flags.hash_randomization.