Skip to content

Instantly share code, notes, and snippets.

@1st1

1st1/test.py Secret

Created September 2, 2021 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 1st1/eccc32991dc2798f3fa0b4050ae2461d to your computer and use it in GitHub Desktop.
Save 1st1/eccc32991dc2798f3fa0b4050ae2461d to your computer and use it in GitHub Desktop.
generators
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)
@1st1
Copy link
Author

1st1 commented Sep 2, 2021

+++ run <function thin_anext at 0x7fd1cd682840>
1. send 1
2. throw: got value 2


+++ run <function thick_anext at 0x7fd1cd6828e0>
1. send 1
2. throw: got error <class '__main__.MyError'> 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment