ADR 0059: The standard library, compiled where CPython is reproduced exactly¶
Status: Final
Type: Feature
Created: 2026-10-09
Related-ADRs: 0006 (the explicit boundary, from python import ...), 0007 (raises from generated code), 0005 (the builtins it sits beside), 0061 (files and sys.stdin), 0071 (collections.defaultdict). Provenance: A145 (owner decision), A144 (what a typed program is), A166 (modules written in POST), the Shed Skin census (notes/measurements.md, 2026-10-09).
Context¶
The first census on real programs found the standard library in front of all 54 programs: time.time() in 48, random in 13, math in most of the numeric ones. Typed code writes import time and from math import sqrt. postpile refused both: math.sqrt(x) as an attribute call, with advice to import postpile.math instead, and from math import sqrt as a CPython-boundary call it did not lower.
postpile.math was never the answer for such a program. It is the kernels' library and has IEEE semantics by design: floor there returns a float and keeps -0.0, while a value outside a function's domain gives a NaN. CPython's math returns an int from floor, raises ValueError outside a domain and OverflowError past a range. A program that writes import math is owed those semantics, because the interpreted run is the oracle.
Decision¶
A standard-library module is compiled where CPython's behaviour can be reproduced exactly, and reached through the explicit from python import ... boundary everywhere else (A145). A name the compiler does not hold is refused with that spelling: `math.hypot` is not compiled by postpile; reach it through the CPython boundary with `from python import math`.
How a function qualifies¶
- Its results are CPython's, bit for bit. A function CPython implements with a dedicated algorithm, which libm could round differently in the last bit, is not compiled (
math.hypot). A module with a generator is compiled only if it reproduces CPython's generator and algorithms exactly, so a seeded program prints the same numbers compiled as interpreted (random). Runtime float arithmetic that must match CPython's rounding is written so the C compiler cannot fuse it into an FMA. - Its raises are CPython's: the same exception class and the same message, built in generated code, with arguments checked in CPython's order. The messages are those of the interpreter running the suite. A unit test holds each declared message against that interpreter, so a message that changes upstream fails there before it fails a program's output.
- Its evaluation order and observable calls are CPython's. Where a function calls back into the program (a
__lt__in a heap or a search), it makes CPython's sequence of calls. Where CPython reads the inputs before producing anything, the compiled form takes the same snapshot. - Its result does not depend on the host. A form whose meaning varies with the platform (a
structformat with native size and alignment) is refused. - Process-wide state stays the host's. A function that reads or changes state CPython keeps per process (the
randomgenerator,sys.argv,sys.exit) is compiled into an executable only: a library or an extension module runs inside a host process whose module is CPython's, shared by all callers. A compiled module's copy would not see a Python caller's changes. The build refuses such a module, naming the boundary spelling. A shared state is held under a lock, so draws from concurrent pygo tasks are some interleaving of one stream. - A divergence is named. Where reproducing CPython exactly is out of reach in a corner no ordinary program meets, the corner is written down or refused; it is never approximated silently.
heapqdoes not check, as_heapqdoes, that a comparison changed the heap's size, so only a__lt__that mutates the heap it is ordering sees a difference (noRuntimeError).sys.setrecursionlimitchecks the argument and changes nothing, since a compiled call has the C stack and no such limit; any recursive program meets that divergence.
A function that does not qualify is refused with the reason. The rest of the module is reached through the boundary. A function named without a call (f = math.sqrt) is refused: a function of the standard library is not a function value (0070).
How a module is provided¶
- A table of C calls. A module compiled in the runtime is described by an authored table: each name, the C symbol, and the error CPython raises. A call that can raise takes part in the raisers analysis, so a domain error raised in a callee reaches the caller's handler.
- POST source, specialized per type (A166). A module of functions generic over an element type (
heapq,bisect) is written in POST and shipped with postpile, each function over a type parameter and compiled once per element type a program calls it with: monomorphization by source copy, with no type parameter in the IR, the backends or the runtime. A refusal inside an instance is reported at the program's call. The algorithms are those of CPython's C accelerators, comparison for comparison. - A rewrite into a construct postpile already has, when one shape of a module's API is what programs use: a
collections.dequelocal used only throughappend,pop,popleftand iteration is a list with an O(1)popleft, anditertools.productin aforis one counted loop. Any other use of the value is refused, since it would expose the stand-in. - A fold, for constants whose value is CPython's (
math.pi,string's constants, read from CPython when postpile loads).
The spellings: import math, import time as t, from math import sqrt, pi, from time import time as clock and from math import *. A local, or a function of the module, named like the module or the function is that local or function, per Python scoping.
The compatibility page lists what each module compiles today and what it refuses.
Consequences¶
- A message built at run time (the argument's repr) is held by the exception record and released with it when a handler is done with it (0007); the suite checks the counts.
- Each module is added when a measured program asks for it, so the compiled surface follows the census.
postpile.math's compiled functions return IEEE values where the interpreted copy, delegating to CPython'smath, raises. That disagreement predates this ADR and is recorded separately (notes/accept-then-wrong.md§46).
Alternatives considered¶
import xreaches CPython implicitly: all modules at once, but a binary then links libpython and pays the boundary per call. The source would also no longer show where the boundary is, which pygonotes/11§2 rules out.mathas an alias ofpostpile.math: one table instead of two, with the wrong semantics for any program that writesimport math.- The explicit boundary only: typed code rewritten at every import, for modules whose behaviour is a few lines of C.