Datasets:
Download Python/val/3.13/async-generators/modern_02.py from SwashBuckler001/idiombench: direct link, hf CLI and curl.
- Browser
- Download file 13.3 kB
-
https://huggingface.co/datasets/SwashBuckler001/idiombench/resolve/main/Python/val/3.13/async-generators/modern_02.py
- Command line
-
hf download hf://datasets/SwashBuckler001/idiombench/Python/val/3.13/async-generators/modern_02.py
-
curl -L -o modern_02.py https://huggingface.co/datasets/SwashBuckler001/idiombench/resolve/main/Python/val/3.13/async-generators/modern_02.py
13.3 kB
| import inspect | |
| import types | |
| import unittest | |
| import contextlib | |
| from test.support.import_helper import import_module | |
| from test.support import gc_collect, requires_working_socket | |
| class AsyncGenTest(unittest.TestCase): | |
| def compare_generators(self, sync_gen, async_gen): | |
| def sync_iterate(g): | |
| res = [] | |
| while True: | |
| try: | |
| res.append(g.__next__()) | |
| except StopIteration: | |
| res.append('STOP') | |
| break | |
| except Exception as ex: | |
| 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 | |
| sync_gen_result = sync_iterate(sync_gen) | |
| async_gen_result = async_iterate(async_gen) | |
| self.assertEqual(sync_gen_result, async_gen_result) | |
| return async_gen_result | |
| def test_async_gen_iteration_01(self): | |
| async def gen(): | |
| await awaitable() | |
| a = yield 123 | |
| self.assertIs(a, None) | |
| await awaitable() | |
| yield 456 | |
| await awaitable() | |
| yield 789 | |
| self.assertEqual(to_list(gen()), [123, 456, 789]) | |
| def test_async_gen_iteration_02(self): | |
| async def gen(): | |
| await awaitable() | |
| yield 123 | |
| await awaitable() | |
| g = gen() | |
| ai = g.__aiter__() | |
| an = ai.__anext__() | |
| self.assertEqual(an.__next__(), ('result',)) | |
| try: | |
| an.__next__() | |
| except StopIteration as ex: | |
| self.assertEqual(ex.args[0], 123) | |
| else: | |
| self.fail('StopIteration was not raised') | |
| an = ai.__anext__() | |
| self.assertEqual(an.__next__(), ('result',)) | |
| try: | |
| an.__next__() | |
| except StopAsyncIteration as ex: | |
| self.assertFalse(ex.args) | |
| else: | |
| self.fail('StopAsyncIteration was not raised') | |
| def test_async_gen_exception_03(self): | |
| async def gen(): | |
| await awaitable() | |
| yield 123 | |
| await awaitable(throw=True) | |
| yield 456 | |
| with self.assertRaises(AwaitException): | |
| to_list(gen()) | |
| def test_async_gen_exception_04(self): | |
| async def gen(): | |
| await awaitable() | |
| yield 123 | |
| 1 / 0 | |
| g = gen() | |
| ai = g.__aiter__() | |
| an = ai.__anext__() | |
| self.assertEqual(an.__next__(), ('result',)) | |
| try: | |
| an.__next__() | |
| except StopIteration as ex: | |
| self.assertEqual(ex.args[0], 123) | |
| else: | |
| self.fail('StopIteration was not raised') | |
| with self.assertRaises(ZeroDivisionError): | |
| ai.__anext__().__next__() | |
| def test_async_gen_exception_05(self): | |
| async def gen(): | |
| yield 123 | |
| raise StopAsyncIteration | |
| with self.assertRaisesRegex(RuntimeError, | |
| 'async generator.*StopAsyncIteration'): | |
| to_list(gen()) | |
| def test_async_gen_exception_06(self): | |
| async def gen(): | |
| yield 123 | |
| raise StopIteration | |
| with self.assertRaisesRegex(RuntimeError, | |
| 'async generator.*StopIteration'): | |
| to_list(gen()) | |
| def test_async_gen_exception_07(self): | |
| def sync_gen(): | |
| try: | |
| yield 1 | |
| 1 / 0 | |
| finally: | |
| yield 2 | |
| yield 3 | |
| yield 100 | |
| async def async_gen(): | |
| try: | |
| yield 1 | |
| 1 / 0 | |
| finally: | |
| yield 2 | |
| yield 3 | |
| yield 100 | |
| self.compare_generators(sync_gen(), async_gen()) | |
| def test_async_gen_exception_08(self): | |
| def sync_gen(): | |
| try: | |
| yield 1 | |
| finally: | |
| yield 2 | |
| 1 / 0 | |
| yield 3 | |
| yield 100 | |
| async def async_gen(): | |
| try: | |
| yield 1 | |
| await awaitable() | |
| finally: | |
| await awaitable() | |
| yield 2 | |
| 1 / 0 | |
| yield 3 | |
| yield 100 | |
| self.compare_generators(sync_gen(), async_gen()) | |
| def test_async_gen_exception_09(self): | |
| def sync_gen(): | |
| try: | |
| yield 1 | |
| 1 / 0 | |
| finally: | |
| yield 2 | |
| yield 3 | |
| yield 100 | |
| async def async_gen(): | |
| try: | |
| await awaitable() | |
| yield 1 | |
| 1 / 0 | |
| finally: | |
| yield 2 | |
| await awaitable() | |
| yield 3 | |
| yield 100 | |
| self.compare_generators(sync_gen(), async_gen()) | |
| def test_async_gen_exception_10(self): | |
| async def gen(): | |
| yield 123 | |
| with self.assertRaisesRegex(TypeError, | |
| "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_exception_12(self): | |
| async def gen(): | |
| with self.assertWarnsRegex(RuntimeWarning, | |
| f"coroutine method 'asend' of '{gen.__qualname__}' " | |
| f"was never awaited"): | |
| await anext(me) | |
| yield 123 | |
| me = gen() | |
| ai = me.__aiter__() | |
| an = ai.__anext__() | |
| with self.assertRaisesRegex(RuntimeError, | |
| r'anext\(\): asynchronous generator is already running'): | |
| an.__next__() | |
| with self.assertRaisesRegex(RuntimeError, | |
| r"cannot reuse already awaited __anext__\(\)/asend\(\)"): | |
| an.send(None) | |
| def test_async_gen_asend_throw_concurrent_with_send(self): | |
| import types | |
| def _async_yield(v): | |
| return (yield v) | |
| class MyExc(Exception): | |
| pass | |
| async def agenfn(): | |
| while True: | |
| try: | |
| await _async_yield(None) | |
| except MyExc: | |
| pass | |
| return | |
| yield | |
| agen = agenfn() | |
| gen = agen.asend(None) | |
| gen.send(None) | |
| gen2 = agen.asend(None) | |
| with self.assertRaisesRegex(RuntimeError, | |
| r'anext\(\): asynchronous generator is already running'): | |
| gen2.throw(MyExc) | |
| with self.assertRaisesRegex(RuntimeError, | |
| r"cannot reuse already awaited __anext__\(\)/asend\(\)"): | |
| gen2.send(None) | |
| def test_async_gen_athrow_throw_concurrent_with_send(self): | |
| import types | |
| def _async_yield(v): | |
| return (yield v) | |
| class MyExc(Exception): | |
| pass | |
| async def agenfn(): | |
| while True: | |
| try: | |
| await _async_yield(None) | |
| except MyExc: | |
| pass | |
| return | |
| yield | |
| agen = agenfn() | |
| gen = agen.asend(None) | |
| gen.send(None) | |
| gen2 = agen.athrow(MyExc) | |
| with self.assertRaisesRegex(RuntimeError, | |
| r'athrow\(\): asynchronous generator is already running'): | |
| gen2.throw(MyExc) | |
| with self.assertRaisesRegex(RuntimeError, | |
| r"cannot reuse already awaited aclose\(\)/athrow\(\)"): | |
| gen2.send(None) | |
| def test_async_gen_asend_throw_concurrent_with_throw(self): | |
| import types | |
| def _async_yield(v): | |
| return (yield v) | |
| class MyExc(Exception): | |
| pass | |
| async def agenfn(): | |
| try: | |
| yield | |
| except MyExc: | |
| pass | |
| while True: | |
| try: | |
| await _async_yield(None) | |
| except MyExc: | |
| pass | |
| agen = agenfn() | |
| with self.assertRaises(StopIteration): | |
| agen.asend(None).send(None) | |
| gen = agen.athrow(MyExc) | |
| gen.throw(MyExc) | |
| gen2 = agen.asend(MyExc) | |
| with self.assertRaisesRegex(RuntimeError, | |
| r'anext\(\): asynchronous generator is already running'): | |
| gen2.throw(MyExc) | |
| with self.assertRaisesRegex(RuntimeError, | |
| r"cannot reuse already awaited __anext__\(\)/asend\(\)"): | |
| gen2.send(None) | |
| def test_async_gen_athrow_throw_concurrent_with_throw(self): | |
| import types | |
| def _async_yield(v): | |
| return (yield v) | |
| class MyExc(Exception): | |
| pass | |
| async def agenfn(): | |
| try: | |
| yield | |
| except MyExc: | |
| pass | |
| while True: | |
| try: | |
| await _async_yield(None) | |
| except MyExc: | |
| pass | |
| agen = agenfn() | |
| with self.assertRaises(StopIteration): | |
| agen.asend(None).send(None) | |
| gen = agen.athrow(MyExc) | |
| gen.throw(MyExc) | |
| gen2 = agen.athrow(None) | |
| with self.assertRaisesRegex(RuntimeError, | |
| r'athrow\(\): asynchronous generator is already running'): | |
| gen2.throw(MyExc) | |
| with self.assertRaisesRegex(RuntimeError, | |
| r"cannot reuse already awaited aclose\(\)/athrow\(\)"): | |
| gen2.send(None) | |
| def test_async_gen_3_arg_deprecation_warning(self): | |
| async def gen(): | |
| yield 123 | |
| with self.assertWarns(DeprecationWarning): | |
| x = gen().athrow(GeneratorExit, GeneratorExit(), None) | |
| with self.assertRaises(GeneratorExit): | |
| x.send(None) | |
| del x | |
| gc_collect() | |
| def test_async_gen_api_01(self): | |
| async def gen(): | |
| yield 123 | |
| g = gen() | |
| self.assertEqual(g.__name__, 'gen') | |
| g.__name__ = '123' | |
| self.assertEqual(g.__name__, '123') | |
| self.assertIn('.gen', g.__qualname__) | |
| g.__qualname__ = '123' | |
| self.assertEqual(g.__qualname__, '123') | |
| self.assertIsNone(g.ag_await) | |
| self.assertIsInstance(g.ag_frame, types.FrameType) | |
| self.assertFalse(g.ag_running) | |
| self.assertIsInstance(g.ag_code, types.CodeType) | |
| aclose = g.aclose() | |
| self.assertTrue(inspect.isawaitable(aclose)) | |
| aclose.close() | |
| def test_async_gen_asend_close_runtime_error(self): | |
| import types | |
| def _async_yield(v): | |
| return (yield v) | |
| async def agenfn(): | |
| try: | |
| await _async_yield(None) | |
| except GeneratorExit: | |
| await _async_yield(None) | |
| return | |
| yield | |
| agen = agenfn() | |
| gen = agen.asend(None) | |
| gen.send(None) | |
| with self.assertRaisesRegex(RuntimeError, "coroutine ignored GeneratorExit"): | |
| gen.close() | |
| def test_async_gen_athrow_close_runtime_error(self): | |
| import types | |
| def _async_yield(v): | |
| return (yield v) | |
| class MyExc(Exception): | |
| pass | |
| async def agenfn(): | |
| try: | |
| yield | |
| except MyExc: | |
| try: | |
| await _async_yield(None) | |
| except GeneratorExit: | |
| await _async_yield(None) | |
| agen = agenfn() | |
| with self.assertRaises(StopIteration): | |
| agen.asend(None).send(None) | |
| gen = agen.athrow(MyExc) | |
| gen.send(None) | |
| with self.assertRaisesRegex(RuntimeError, "coroutine ignored GeneratorExit"): | |
| gen.close() | |