-
-
Save 1st1/eccc32991dc2798f3fa0b4050ae2461d to your computer and use it in GitHub Desktop.
generators
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import types | |
class MyError(Exception): pass | |
@types.coroutine | |
def _async_yield(v): | |
return (yield v) | |
async def test(): | |
try: | |
await _async_yield(1) | |
except MyError: | |
await _async_yield(2) | |
return | |
yield | |
def thin_anext(gen): | |
return gen.__anext__() | |
async def thick_anext(gen): | |
return await gen.__anext__() | |
def run(anext): | |
print('\n\n+++ run', anext) | |
gen = test() | |
aw = anext(gen).__await__() | |
print('1. send', aw.send(None)) | |
aw.close() | |
aw = anext(gen).__await__() | |
try: | |
val = aw.throw(MyError, MyError(), None) | |
print('2. throw: got value', val) | |
except MyError as e: | |
print('2. throw: got error', type(e), e) | |
run(thin_anext) | |
run(thick_anext) |
Author
1st1
commented
Sep 2, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment