Generators
Generator functions and expressions, yield, yield from, send() and StopIteration.value are supported.
See comprehensions.md for restrictions on generator expression targets and clauses.
throw()andclose()raiseAttributeError, e.g.'generator' object has no attribute 'close'.GeneratorExitis not an exception class in Monty either (see exceptions.md).gi_frame,gi_code,gi_running,gi_suspended,gi_yieldfrom,__name__and__qualname__raiseAttributeError.send()and__next__()work only as calls: readingg.sendwithout calling it raisesAttributeError, as the dunders of other built-in iterators do (see iter.md).repr()uses the function’s name, not its qualified name:<generator object inner at 0x...>where CPython gives<generator object outer.<locals>.inner at 0x...>.
- An unfinished generator never runs its pending
finallyblocks orwithexits. CPython closes a generator that is garbage collected, raisingGeneratorExitat the pausedyield. Monty releases its references without resuming it. - Consumers that advance an iterator in Rust cannot suspend.
*unpacking (includingasyncio.gather(*(f(x) for x in xs))),list(),tuple(),sum(),sorted(),str.join()and theitertoolsadaptors run the generator to completion without returning to the host, so a host call in its body raisesNotImplementedError: external function 'name' cannot suspend in this synchronous contextinside the generator, where it can be caught. Advance the generator withfor, a list, set or dict comprehension,next(),send()oryield fromto allow suspension. The same restriction applies to user__next__methods; see iter.md. yield fromcannot advance a delegate that would suspend outside a Python frame. A class whose__next__is a host function, or a file that has not been read yet, raisesNotImplementedError: yield from: this iterator cannot suspend to the hostinside the delegating generator. A__next__written in Python may call host functions.- The
RuntimeErrorfor aStopIterationescaping a generator (PEP 479) loses its exception chain when a consumer that advances in Rust drove the generator, as for any exception rebuilt after a nested synchronous call (see exceptions.md). Undernext()orfor, both__cause__and__context__are the originalStopIteration. StopIteration.valueis lost when the exception is rebuilt from a copy. AStopIterationthat escapes a nested synchronous call (asorted()key, a__repr__, a generator driven bylist()) reaches the outer handler with.valueset to its message, orNone, andargsto match:sorted([1], key=lambda x: next(g()))for agthat returns[1, 2]givese.value is Noneande.args == (). A string value survives as the message, but not its identity.- An uncaught
StopIterationshows its value only when it is a string. The message a host receives is the exception’s text, sonext(g())escaping the sandbox for agthat returns[1, 2]reportsStopIterationwith no message, where CPython printsStopIteration: [1, 2]. Inside the sandbox,str(e),repr(e)ande.argsmatch CPython. - A host function that raises
StopIteration(5)gives the sandboxe.value == '5', a string, because only the message crosses the host boundary.
- Async generator functions (
async defcontainingyield) raiseNotImplementedError. Async generator expressions (async fororawaitin the deferred body) also raiseNotImplementedError.yield frominsideasync defraises CPython’sSyntaxError: 'yield from' inside async function.
- A frame paused at a
yield fromstatement that fills its line shows carets under the statement, which CPython omits;x = yield from inner()matches CPython.