diff options
author | Yury Selivanov <yselivanov@sprymix.com> | 2015-07-03 13:11:54 -0400 |
---|---|---|
committer | Yury Selivanov <yselivanov@sprymix.com> | 2015-07-03 13:11:54 -0400 |
commit | d48fb485d9d6597b1048f6131a28cb14979349af (patch) | |
tree | c5937eb56fc234ac626c58f7bf7e925aef09ffe0 /Lib/test | |
parent | 11e6d79eca724697f096ba465046c8ea4f8cf86d (diff) | |
parent | fdbeb2b4b67e1e44c96127a06cf1bdf878f4f7ca (diff) | |
download | cpython-git-d48fb485d9d6597b1048f6131a28cb14979349af.tar.gz |
Merge 3.5 (Issue #24400)
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_collections.py | 12 | ||||
-rw-r--r-- | Lib/test/test_inspect.py | 23 | ||||
-rw-r--r-- | Lib/test/test_types.py | 16 |
3 files changed, 44 insertions, 7 deletions
diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py index 698805d4db..07420c6907 100644 --- a/Lib/test/test_collections.py +++ b/Lib/test/test_collections.py @@ -511,8 +511,10 @@ class TestOneTrickPonyABCs(ABCTestCase): self.assertTrue(issubclass(type(x), Awaitable)) c = coro() - self.assertIsInstance(c, Awaitable) - c.close() # awoid RuntimeWarning that coro() was not awaited + # Iterable coroutines (generators with CO_ITERABLE_COROUTINE + # flag don't have '__await__' method, hence can't be instances + # of Awaitable. Use inspect.isawaitable to detect them. + self.assertNotIsInstance(c, Awaitable) c = new_coro() self.assertIsInstance(c, Awaitable) @@ -559,8 +561,10 @@ class TestOneTrickPonyABCs(ABCTestCase): self.assertTrue(issubclass(type(x), Awaitable)) c = coro() - self.assertIsInstance(c, Coroutine) - c.close() # awoid RuntimeWarning that coro() was not awaited + # Iterable coroutines (generators with CO_ITERABLE_COROUTINE + # flag don't have '__await__' method, hence can't be instances + # of Coroutine. Use inspect.isawaitable to detect them. + self.assertNotIsInstance(c, Coroutine) c = new_coro() self.assertIsInstance(c, Coroutine) diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py index ab22c7d677..a02f2e1b60 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -151,6 +151,29 @@ class TestPredicates(IsTestBase): coro.close(); gen_coro.close() # silence warnings + def test_isawaitable(self): + def gen(): yield + self.assertFalse(inspect.isawaitable(gen())) + + coro = coroutine_function_example(1) + gen_coro = gen_coroutine_function_example(1) + + self.assertTrue(inspect.isawaitable(coro)) + self.assertTrue(inspect.isawaitable(gen_coro)) + + class Future: + def __await__(): + pass + self.assertTrue(inspect.isawaitable(Future())) + self.assertFalse(inspect.isawaitable(Future)) + + class NotFuture: pass + not_fut = NotFuture() + not_fut.__await__ = lambda: None + self.assertFalse(inspect.isawaitable(not_fut)) + + coro.close(); gen_coro.close() # silence warnings + def test_isroutine(self): self.assertTrue(inspect.isroutine(mod.spam)) self.assertTrue(inspect.isroutine([].count)) diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py index ba8a1b93eb..738588e9ef 100644 --- a/Lib/test/test_types.py +++ b/Lib/test/test_types.py @@ -1447,6 +1447,19 @@ class CoroutineTests(unittest.TestCase): with self.assertRaisesRegex(Exception, 'ham'): wrapper.throw(Exception, Exception('ham')) + def test_returning_itercoro(self): + @types.coroutine + def gen(): + yield + + gencoro = gen() + + @types.coroutine + def foo(): + return gencoro + + self.assertIs(foo(), gencoro) + def test_genfunc(self): def gen(): yield self.assertIs(types.coroutine(gen), gen) @@ -1457,9 +1470,6 @@ class CoroutineTests(unittest.TestCase): g = gen() self.assertTrue(g.gi_code.co_flags & inspect.CO_ITERABLE_COROUTINE) self.assertFalse(g.gi_code.co_flags & inspect.CO_COROUTINE) - self.assertIsInstance(g, collections.abc.Coroutine) - self.assertIsInstance(g, collections.abc.Awaitable) - g.close() # silence warning self.assertIs(types.coroutine(gen), gen) |