Let list(), tuple() and sum() suspend inside a user __next__

FE
8d ago
+1066-52

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 under check_time_every.
  • CallFrame.continuation, serialized as optional K on SerializedFrame / SerializedTaskFrame and skipped when empty. Released on every frame-discard path.
  • Only StopIteration is intercepted; any other exception keeps unwinding with the same exception object.
  • sum() keeps CPython's compensated float summation across suspensions: sum's Phase is stored in the continuation and shared through Phase::fold.
  • Rust callers that cannot yield (sorted(key=...), map(tuple, ...)) still work: finish_synchronously drives 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 K continuation and resume with wrong results; it rejects version 18 dumps instead. dump_v18.bin is upstream's.
  • limitations/iter.md and classes.md list 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_for and _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 suspended sum → for (#1384 catcher) → list loads, finishes, and is re-dumped at each later suspension.
  • tests/main.rs: external_function_in_next_raises_not_implemented now uses set(), which is still synchronous; #1384's for test is unchanged.
  • Local (macOS, merged with main at a96e6bd7): all 1638 test_cases, every monty test binary, make lint-rs / lint-py; memory-model-checks on the new cases, dump_compat and main.

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