Add ascii(obj, /) builtin function
Summary
Implements the ascii(obj, /) builtin function (tracked in #1275 and #1319):
- Returns the
Valuefromvalue.py_repr(vm)?directly without extra allocation when the representation is already all-ASCII. - Escapes non-ASCII code points into
\xNN,\uNNNN, or\UNNNNNNNNviaStringBuilderandcheck_time_every, sharingescape_non_asciiwith the!aformat conversion incrates/monty/src/bytecode/vm/format.rs. - Exposes
ascii(obj: object, /) -> strincrates/monty-typeshedand updatesdocs/limitations/builtins.md.
Tests
-
Added
crates/monty/test_cases/builtin__ascii.pycovering ASCII strings, primitives,\xNN/\uNNNN/\UNNNNNNNNcode 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, andmake 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.