Commit 0693ae14 authored by Andrew M. Kuchling's avatar Andrew M. Kuchling

[Bug #1616726] Fix description of generator.close(); if you raise some random...

[Bug #1616726] Fix description of generator.close(); if you raise some random exception, the exception is raised and doesn't trigger a RuntimeError
parent ff5c229e
...@@ -556,13 +556,14 @@ generators: ...@@ -556,13 +556,14 @@ generators:
where the generator's execution is paused. where the generator's execution is paused.
\item \method{close()} raises a new \exception{GeneratorExit} \item \method{close()} raises a new \exception{GeneratorExit}
exception inside the generator to terminate the iteration. exception inside the generator to terminate the iteration. On
On receiving this receiving this exception, the generator's code must either raise
exception, the generator's code must either raise \exception{GeneratorExit} or \exception{StopIteration}. Catching
\exception{GeneratorExit} or \exception{StopIteration}; catching the the \exception{GeneratorExit} exception and returning a value is
exception and doing anything else is illegal and will trigger illegal and will trigger a \exception{RuntimeError}; if the function
a \exception{RuntimeError}. \method{close()} will also be called by raises some other exception, that exception is propagated to the
Python's garbage collector when the generator is garbage-collected. caller. \method{close()} will also be called by Python's garbage
collector when the generator is garbage-collected.
If you need to run cleanup code when a \exception{GeneratorExit} occurs, If you need to run cleanup code when a \exception{GeneratorExit} occurs,
I suggest using a \code{try: ... finally:} suite instead of I suggest using a \code{try: ... finally:} suite instead of
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment