bpo-33786: Fix asynchronous generators to handle GeneratorExit in athrow() by 1st1 · Pull Request #7467 · python/cpython (original) (raw)
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I understand, this will make .aclose() raise StopIteration in the usual case (when generator doesn't handle the GeneratorExit).
Keep in mind, that raising StopIteration
from any of a***()
methods just means that they are returning. They are all iterators.
What about .athrow(), how will it behave?
The idea here:
athrow()
andaclose()
share one implementation.aclose()
is basically anathrow(GeneratorExit)
.- When
aclose()
is called and the generator propagatesGeneratorExit
, it means that everything is fine and theaclose()
call was successful. We raiseStopIteration
to makeaclose()
awaitable return. - When
athrow()
is called we don't want to trap any exceptions at all. So we raiseStopIteration
only if the asynchronous generator swallowed the thrown error and yielded (asynchronously).
This is a bit complex, I know. I explained how asynchronous generators work in detail here: https://www.python.org/dev/peps/pep-0525/#implementation-details