You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Let generator.close() return StopIteration.value #104770
The StopIteration is handled by close(), but the return value is currently discarded, and close() always returns None.
The proposed change is to let close() return the value of a StopIteration it encounters after a graceful generator exit.
Every other case, including errors thrown between except GeneratorExit and return, is already handled by close() and would remain unchanged. This includes repeated calls to close(): Only the first such call might cause a generator to exit gracefully, after which it cannot raise StopIteration any longer.
The change enables more natural use of generators as "pipelines" that process input data until ended. As a trivial example of the functionality, consider this computer of averages:
The generator processes data in an infinite loop. Once the controlling process terminates processing, the generator performs post-processing, and returns a final result. Without the return value of close(), there is no intrinsic way of obtaining such a post-processed result.
Activity
gh-104770: Let generator.close() return value (#104771)