mirror of
https://github.com/python/cpython.git
synced 2025-10-10 00:43:41 +00:00
bpo-33786: Fix asynchronous generators to handle GeneratorExit in athrow() (GH-7467)
This commit is contained in:
parent
378c53cc31
commit
52698c7ad9
5 changed files with 87 additions and 9 deletions
|
@ -108,6 +108,31 @@ class AsyncGenTest(unittest.TestCase):
|
|||
res.append(str(type(ex)))
|
||||
return res
|
||||
|
||||
def async_iterate(g):
|
||||
res = []
|
||||
while True:
|
||||
an = g.__anext__()
|
||||
try:
|
||||
while True:
|
||||
try:
|
||||
an.__next__()
|
||||
except StopIteration as ex:
|
||||
if ex.args:
|
||||
res.append(ex.args[0])
|
||||
break
|
||||
else:
|
||||
res.append('EMPTY StopIteration')
|
||||
break
|
||||
except StopAsyncIteration:
|
||||
raise
|
||||
except Exception as ex:
|
||||
res.append(str(type(ex)))
|
||||
break
|
||||
except StopAsyncIteration:
|
||||
res.append('STOP')
|
||||
break
|
||||
return res
|
||||
|
||||
def async_iterate(g):
|
||||
res = []
|
||||
while True:
|
||||
|
@ -297,6 +322,37 @@ class AsyncGenTest(unittest.TestCase):
|
|||
"non-None value .* async generator"):
|
||||
gen().__anext__().send(100)
|
||||
|
||||
def test_async_gen_exception_11(self):
|
||||
def sync_gen():
|
||||
yield 10
|
||||
yield 20
|
||||
|
||||
def sync_gen_wrapper():
|
||||
yield 1
|
||||
sg = sync_gen()
|
||||
sg.send(None)
|
||||
try:
|
||||
sg.throw(GeneratorExit())
|
||||
except GeneratorExit:
|
||||
yield 2
|
||||
yield 3
|
||||
|
||||
async def async_gen():
|
||||
yield 10
|
||||
yield 20
|
||||
|
||||
async def async_gen_wrapper():
|
||||
yield 1
|
||||
asg = async_gen()
|
||||
await asg.asend(None)
|
||||
try:
|
||||
await asg.athrow(GeneratorExit())
|
||||
except GeneratorExit:
|
||||
yield 2
|
||||
yield 3
|
||||
|
||||
self.compare_generators(sync_gen_wrapper(), async_gen_wrapper())
|
||||
|
||||
def test_async_gen_api_01(self):
|
||||
async def gen():
|
||||
yield 123
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue