diff options
author | Guido van Rossum <guido@python.org> | 2015-05-03 01:38:24 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2015-05-03 01:38:24 (GMT) |
commit | 0a9933ebf3704540a5f31225b26acb990e1de4bf (patch) | |
tree | aabc7b609ae15cc12bf49cd4451122d2c661c471 /Lib/test/test_asyncio/test_tasks.py | |
parent | 4590c3d944230aff3d1e6810d3113e790922359c (diff) | |
download | cpython-0a9933ebf3704540a5f31225b26acb990e1de4bf.zip cpython-0a9933ebf3704540a5f31225b26acb990e1de4bf.tar.gz cpython-0a9933ebf3704540a5f31225b26acb990e1de4bf.tar.bz2 |
Asyncio issue 222 / PR 231 (Victor Stinner) -- fix @coroutine functions without __name__.
Diffstat (limited to 'Lib/test/test_asyncio/test_tasks.py')
-rw-r--r-- | Lib/test/test_asyncio/test_tasks.py | 65 |
1 files changed, 44 insertions, 21 deletions
diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py index 06447d7..ab61462 100644 --- a/Lib/test/test_asyncio/test_tasks.py +++ b/Lib/test/test_asyncio/test_tasks.py @@ -1,5 +1,7 @@ """Tests for tasks.py.""" +import contextlib +import functools import os import re import sys @@ -28,6 +30,19 @@ def coroutine_function(): pass +@contextlib.contextmanager +def set_coroutine_debug(enabled): + coroutines = asyncio.coroutines + + old_debug = coroutines._DEBUG + try: + coroutines._DEBUG = enabled + yield + finally: + coroutines._DEBUG = old_debug + + + def format_coroutine(qualname, state, src, source_traceback, generator=False): if generator: state = '%s' % state @@ -279,6 +294,29 @@ class TaskTests(test_utils.TestCase): fut.set_result(None) self.loop.run_until_complete(task) + def test_task_repr_partial_corowrapper(self): + # Issue #222: repr(CoroWrapper) must not fail in debug mode if the + # coroutine is a partial function + with set_coroutine_debug(True): + self.loop.set_debug(True) + + @asyncio.coroutine + def func(x, y): + yield from asyncio.sleep(0) + + partial_func = asyncio.coroutine(functools.partial(func, 1)) + task = self.loop.create_task(partial_func(2)) + + # make warnings quiet + task._log_destroy_pending = False + self.addCleanup(task._coro.close) + + coro_repr = repr(task._coro) + expected = ('<CoroWrapper TaskTests.test_task_repr_partial_corowrapper' + '.<locals>.func(1)() running, ') + self.assertTrue(coro_repr.startswith(expected), + coro_repr) + def test_task_basics(self): @asyncio.coroutine def outer(): @@ -1555,25 +1593,16 @@ class TaskTests(test_utils.TestCase): # The frame should have changed. self.assertIsNone(gen.gi_frame) - # Save debug flag. - old_debug = asyncio.coroutines._DEBUG - try: - # Test with debug flag cleared. - asyncio.coroutines._DEBUG = False + # Test with debug flag cleared. + with set_coroutine_debug(False): check() - # Test with debug flag set. - asyncio.coroutines._DEBUG = True + # Test with debug flag set. + with set_coroutine_debug(True): check() - finally: - # Restore original debug flag. - asyncio.coroutines._DEBUG = old_debug - def test_yield_from_corowrapper(self): - old_debug = asyncio.coroutines._DEBUG - asyncio.coroutines._DEBUG = True - try: + with set_coroutine_debug(True): @asyncio.coroutine def t1(): return (yield from t2()) @@ -1591,8 +1620,6 @@ class TaskTests(test_utils.TestCase): task = asyncio.Task(t1(), loop=self.loop) val = self.loop.run_until_complete(task) self.assertEqual(val, (1, 2, 3)) - finally: - asyncio.coroutines._DEBUG = old_debug def test_yield_from_corowrapper_send(self): def foo(): @@ -1663,14 +1690,10 @@ class TaskTests(test_utils.TestCase): @mock.patch('asyncio.coroutines.logger') def test_coroutine_never_yielded(self, m_log): - debug = asyncio.coroutines._DEBUG - try: - asyncio.coroutines._DEBUG = True + with set_coroutine_debug(True): @asyncio.coroutine def coro_noop(): pass - finally: - asyncio.coroutines._DEBUG = debug tb_filename = __file__ tb_lineno = sys._getframe().f_lineno + 2 |