Let list(), tuple() and sum() suspend inside a user __next__
Summary
Step 1 of the proposal in #1374, rebased onto #1384.
#1384 made for loops and comprehensions suspend inside a user __next__, and left synchronous consumers such as list() raising NotImplementedError. This PR makes list(), tuple() and sum() over a user iterator suspend too:
class It:
...
def __next__(self):
...
return add_ints(self.n, 100) # external function
list(It()) # was NotImplementedError: ... cannot suspend in this synchronous context
sum(It()) # same
Instead of running __next__ in a nested vm.run(), the builtin parks a serializable NativeCont on the __next__ frame and returns to the main loop. When that frame returns or raises, ReturnValue / exception unwinding hand the outcome to the continuation, which asks for the next item or finishes and pushes the result. A suspension inside __next__ therefore leaves only the main loop on the Rust stack, and the session can be dumped.
bytecode/vm/native_cont.rs(new):NativeCont::{Collect, Sum}and the trampoline. Frameless items (builtin or host__next__) loop in Rust undercheck_time_every.CallFrame.continuation, serialized as optionalKonSerializedFrame/SerializedTaskFrameand skipped when empty. Released on every frame-discard path.- Only
StopIterationis intercepted; any other exception keeps unwinding with the same exception object. sum()keeps CPython's compensated float summation across suspensions:sum'sPhaseis stored in the continuation and shared throughPhase::fold.- Rust callers that cannot yield (
sorted(key=...),map(tuple, ...)) still work:finish_synchronouslydrives a parked continuation in place. - Needs dump version 18, which #1313 already introduced after 1.7.0, so this PR makes no bump of its own. A 1.7.0 reader (version 17) would ignore a frame's
Kcontinuation and resume with wrong results; it rejects version 18 dumps instead.dump_v18.binis upstream's. limitations/iter.mdandclasses.mdlist exactly which consumers suspend. I checked each case against CPython.
Design question for maintainers. The interpreter would now have two continuation mechanisms: #1384's catcher frame for ForIter, and NativeCont for builtins that return a value. I kept NativeCont because those builtins carry Rust state across items (items, a compensated total), which a catcher frame does not hold. If you'd rather have one mechanism, or move these consumers to bytecode as #1041 plans, I'm happy to rework it. That choice is yours.
Tests
- New differential cases:
ext_call__native_continuation.py(list/tuple/sum, errors and exception identity,finally, nested continuations, compensated float/complex sums, synchronous boundaries), plus_async,_tb_forand_tb_list. They pass on CPython and Monty, and the harness dumps and reloads at every host call. dump_compat::native_continuations_fixture_loads_and_resumes: a fixture suspendedsumāfor(#1384 catcher) ālistloads, finishes, and is re-dumped at each later suspension.tests/main.rs:external_function_in_next_raises_not_implementednow usesset(), which is still synchronous; #1384'sfortest is unchanged.- Local (macOS, merged with main at a96e6bd7): all 1638 test_cases, every
montytest binary,make lint-rs/lint-py;memory-model-checkson the new cases,dump_compatandmain.
Not run locally: heap_reader_compile_fail (its snapshots differ only in diagnostic wording on my nightly toolchain), the Python, JS and WASM suites, and the full memory-model-checks run.
Prior discussion
Proposal: #1374, which has no maintainer reply yet. Related: #1041 (moving iterator builtins to bytecode), #1384, #496.
- I have read the contribution guide.
- I understand and can explain the proposed changes, including any generated with AI assistance.
š¤ Generated with Claude Code