From 24411f8a8daace4ebf8abd41091b681160b4fb89 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sun, 6 Nov 2016 18:44:42 +0200 Subject: Issue #23996: Added _PyGen_SetStopIterationValue for safe raising StopIteration with value. More safely handle non-normalized exceptions in -_PyGen_FetchStopIterationValue. --- Lib/test/test_coroutines.py | 61 +++++++++++++++++++++++++++++++++++++++++++++ Lib/test/test_generators.py | 21 ++++++++++++++++ 2 files changed, 82 insertions(+) (limited to 'Lib') diff --git a/Lib/test/test_coroutines.py b/Lib/test/test_coroutines.py index e52654c14d..4a327b5ba9 100644 --- a/Lib/test/test_coroutines.py +++ b/Lib/test/test_coroutines.py @@ -710,6 +710,21 @@ class CoroutineTest(unittest.TestCase): coro.close() self.assertEqual(CHK, 1) + def test_coro_wrapper_send_tuple(self): + async def foo(): + return (10,) + + result = run_async__await__(foo()) + self.assertEqual(result, ([], (10,))) + + def test_coro_wrapper_send_stop_iterator(self): + async def foo(): + return StopIteration(10) + + result = run_async__await__(foo()) + self.assertIsInstance(result[1], StopIteration) + self.assertEqual(result[1].value, 10) + def test_cr_await(self): @types.coroutine def a(): @@ -1537,6 +1552,52 @@ class CoroutineTest(unittest.TestCase): warnings.simplefilter("error") run_async(foo()) + def test_for_tuple(self): + class Done(Exception): pass + + class AIter(tuple): + i = 0 + def __aiter__(self): + return self + async def __anext__(self): + if self.i >= len(self): + raise StopAsyncIteration + self.i += 1 + return self[self.i - 1] + + result = [] + async def foo(): + async for i in AIter([42]): + result.append(i) + raise Done + + with self.assertRaises(Done): + foo().send(None) + self.assertEqual(result, [42]) + + def test_for_stop_iteration(self): + class Done(Exception): pass + + class AIter(StopIteration): + i = 0 + def __aiter__(self): + return self + async def __anext__(self): + if self.i: + raise StopAsyncIteration + self.i += 1 + return self.value + + result = [] + async def foo(): + async for i in AIter(42): + result.append(i) + raise Done + + with self.assertRaises(Done): + foo().send(None) + self.assertEqual(result, [42]) + def test_copy(self): async def func(): pass coro = func() diff --git a/Lib/test/test_generators.py b/Lib/test/test_generators.py index 3f82462478..cd6a43d753 100644 --- a/Lib/test/test_generators.py +++ b/Lib/test/test_generators.py @@ -277,6 +277,27 @@ class ExceptionTest(unittest.TestCase): # hence no warning. next(g) + def test_return_tuple(self): + def g(): + return (yield 1) + + gen = g() + self.assertEqual(next(gen), 1) + with self.assertRaises(StopIteration) as cm: + gen.send((2,)) + self.assertEqual(cm.exception.value, (2,)) + + def test_return_stopiteration(self): + def g(): + return (yield 1) + + gen = g() + self.assertEqual(next(gen), 1) + with self.assertRaises(StopIteration) as cm: + gen.send(StopIteration(2)) + self.assertIsInstance(cm.exception.value, StopIteration) + self.assertEqual(cm.exception.value.value, 2) + class YieldFromTests(unittest.TestCase): def test_generator_gi_yieldfrom(self): -- cgit v1.2.1