Add the `random` module and an `os.urandom` host call
Merged
SA
3mo ago+4193+4193+0123456789012345678901234567890123456789-142-142-012345678901234567890123456789
Follow ups:
- replace
HostClockwithAuthOsCallsor whatever that includesos.urandom -
SessionState { globals, cwd, random }as described below
Summary by cubic
Adds a CPython-compatible random module and an os.urandom host call. Seeded generators match CPython 3.14; unseeded draws request host entropy rather than failing as unsupported.
Host entropy
- The first draw from an unseeded generator requests one 2496-byte state vector; seeded generators never call the host.
os.urandom(size)rejects negative sizes, chargessizebefore yielding, and requires an exact-size bytes response; the proto size is unsigned, so a negative count can't be expressed.- The default Python handler caps requests at 1 MiB with
max_urandom_bytes; zero rejects nonempty requests without allocating. - Rejection loops poll deadlines, and
sample,choices, andrandbytespreflight allocations and saturate size calculations.
Random state
- Implements MT19937, distributions, independent
random.Randominstances, and CPython-compatible seeding and state handling. - The module-level generator persists across REPL feeds and dumps (dump version 10).
random.Randominstances and the class are returned through the public host API as repr strings rather than public Monty types.