Expand description
CPython 3.11 string / float / round semantics (PORT-CONTRACT.md §“Python- compatibility primitives”, PORT-CONTRACT.d/07 §1.4–1.5).
Unicode behavior is table-driven from the packaged
assets/spec/pycompat-tables.json
(generated by rust/spec/extract_pycompat_tables.py against the oracle
interpreter), embedded at build time and parsed once into a static.
Float formatting (py_float_repr) reproduces CPython repr(float):
shortest round-trip digits reshaped Python-style (.0 for integral,
1e-05/1e+20 exponent shaping). py_round and the .1f/.0% format
helpers do correct decimal rounding (half-to-even) of the exact binary
value via an arbitrary-precision decimal expansion of the mantissa —
never multiply-by-10^n tricks.
Constants§
- SURROGATE_
SENTINEL_ BASE - First sentinel code point; add the raw byte value (0x80..=0xFF).
Functions§
- decode_
stdin_ surrogateescape - Decode stdin bytes the way the oracle’s
sys.stdin.read()does under the parity environment: UTF-8 withsurrogateescape— every undecodable byte becomes one sentinel char (CPython maps each bad byte individually, which matches walking Rust’sUtf8Errorwith per-byte emission). - encode_
stdout_ surrogateescape - Encode a rendered stdout payload, re-materializing sentinel chars as
their raw original bytes (the oracle’s stdout uses
surrogateescape, so a lone surrogate prints as the byte that produced it). Borrow when no translation is needed. - first_
nonempty_ line - The first non-empty
py_strip’d line ofs(str.splitlines()boundaries), or""when every line is blank. - is_
re_ digit - Python
re\dclass membership (Unicode default). - is_
re_ word - Python
re\wclass membership (Unicode default). - py_
abspath - Python
posixpath.abspath(path)—normpath(join(cwd, path)), lexical (never touches the filesystem beyond reading the cwd). - py_
casefold - Python
str.casefold()— full Unicode case folding (ß→ss). - py_
float_ repr - CPython
repr(float): shortest round-trip decimal digits (David Gay dtoa mode-0 semantics: among round-tripping candidates prefer the one nearest the exact binary value, ties to even), fixed notation when the decimal point position is in (-4, 16], otherwise exponent form with lowercasee, mandatory sign, and a minimum two-digit exponent. Integral in-range floats keep a.0suffix.inf/-inf/nan. - py_
format_ 1f - Python
f"{x:.1f}"— one fixed decimal, round-half-to-even on the exact binary value. - py_
format_ fixed - Python
f"{x:.<nd>f}"for an arbitrary decimal count (eval’s.3f,.6f, and.0freport/gate formats). - py_
format_ percent0 - Python
f"{x:.0%}"— multiplies by 100 in binary (a rounding f64 multiply, exactly as CPython does), formats with zero decimals half-to-even, appends%. - py_
is_ printable - Python
str.isprintable()for a single character. - py_
is_ space - Python
str.isspace()for a single character (includes\x1c–\x1fand NBSP; excludes U+FEFF and U+200B). - py_
lstrip - Python
str.lstrip()with no argument. - py_
normpath - Python
posixpath.normpath(path): collapse duplicate slashes and.components, resolve..lexically (kept when it would climb past a relative start; dropped at an absolute root), preserve an exactly-double leading slash. Empty input yields.. - py_
relpath - Python
os.path.relpath(path, start)on POSIX: both sides areabspath’d lexically, then the relative walk is derived from the component lists. Returns.when they coincide. - py_
repr_ str - Python
repr()for strings: single quotes unless the string contains a single quote and no double quote; escapes for backslash/quote/\t\n\r;\xXX/\uXXXX/\UXXXXXXXX(lowercase hex) for non-printable characters; printable non-ASCII stays literal. - py_
round - CPython
round(x, ndigits): correct decimal rounding, half-to-even, of the exact binary value (David Gay dtoa semantics). Non-finite and zero inputs return unchanged; a magnitude overflow (CPython raisesOverflowError) saturates to infinity here. - py_
rstrip - Python
str.rstrip()with no argument. - py_
splitlines - Python
str.splitlines()(keepends=False): splits on the exact Python boundary set (\n \r \v \f \x1c \x1d \x1e \x85 U+2028 U+2029), with a\r\npair consumed as a single boundary; no trailing empty element. - py_
strip - Python
str.strip()with no argument (strips the Python whitespace set). - quote_
plus urllib.parse.quote_plus(s, safe='')over the string’s UTF-8 bytes: the ALWAYS-SAFE set (ASCII alphanumerics plus_.-~) stays literal, a space becomes+, and every other byte becomes uppercase%XX.- quote_
plus_ urlencode urllib.parse.urlencode(mapping)over string pairs —quote_pluson each key and value, pairs joined with&in the given order.- read_
text_ universal Path(path).read_text(encoding="utf-8")— strict UTF-8 with universal newlines (\r\n/\r→\n);Noneon OSError/UnicodeDecodeError (callers substitute “” or a structuredunreadableerror).- sentinel_
surrogate - If
cis an active sentinel, the surrogate code point (0xDC80..=0xDCFF) the oracle would hold instead. - set_
surrogate_ sentinels_ active - Test hook: reset/force the sentinel flag.
- surrogate_
sentinels_ active - True once
decode_stdin_surrogateescapehas produced a sentinel.