ADR 0044: slicing, with CPython's adjustment and shared slots¶
Status: Final
Type: Feature
Created: 2026-08-04
Related-ADRs: 0043 (s[i], the code-point machinery), 0027 (which deferred slicing as the first producing list operation), 0030 (raising from generated code). Provenance: arbitration A130.
Context¶
The slice lands whole: full CPython semantics, arbitrary step included. Refusing step was considered and rejected: [::-1] is the language's own reversal spelling, an idiom real programs write.
Decision¶
One adjustment: __pp_slice_norm is CPython's PySlice_AdjustIndices exactly, because clamping is the semantics. s[5:100] answers without raising, negative indices wrap once, and absent bounds default by the sign of the step. step == 0 raises ValueError("slice step cannot be zero") from generated code (ADR 0030's arrangement), with the runtime's own check kept as a compiler-bug backstop.
Two producers:
__pp_str_sliceworks in code points: an ASCII fast path when the byte length equals the code-point length; otherwise a cursor visits the selected code points in output order, forward for a positive step and backward for a negative one, since UTF-8 walks both ways. Two passes, exact allocation, no scratch memory, and therefore no OOM path.__pp_list_slicecopies slots and retains each per the list's own element kind, so a shared element stays alive whichever list dies first; this is the one thing a slice adds to the memory model.
Both are producers (STR_OWNING / LIST_OWNING). The frontend threads absent-bound flags down because only the runtime knows the step's sign uniformly.