Add ascii(obj, /) builtin function

Merged
GA
4d ago
+152-40

Summary

Implements the ascii(obj, /) builtin function (tracked in #1275 and #1319):

  • Returns the Value from value.py_repr(vm)? directly without extra allocation when the representation is already all-ASCII.
  • Escapes non-ASCII code points into \xNN, \uNNNN, or \UNNNNNNNN via StringBuilder and check_time_every, sharing escape_non_ascii with the !a format conversion in crates/monty/src/bytecode/vm/format.rs.
  • Exposes ascii(obj: object, /) -> str in crates/monty-typeshed and updates docs/limitations/builtins.md.

Tests

  • Added crates/monty/test_cases/builtin__ascii.py covering ASCII strings, primitives, \xNN/\uNNNN/\UNNNNNNNN code point boundaries, containers, custom __repr__ implementations, and argument/return-type error messages.

  • Verified with cargo run -p monty-datatest -- builtin__ascii, cargo test -p monty-type-checking, cargo test -p monty --test dump_compat, make lint-rs, make lint-py, and make lint-md.

  • I have read the contribution guide.

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


Summary by cubic

Implements the ascii(obj, /) builtin function (tracked in #1275 and #1319). Like repr(), it returns a printable representation of an object, but escapes non-ASCII code points into \xNN, \uNNNN, or \UNNNNNNNN sequences, returning all-ASCII results directly without extra allocation. Escaping is extracted into escape_non_ascii and shared with the !a format conversion in format.rs, removing duplicated code; !a now also skips escaping for ASCII-only text. Adds the typeshed stub, updates the builtins limitations doc, and records the new builtin in the identity-encoding snapshot.