]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
asyncio: Fix _format_coroutine for coroutine-like objects w/o __name__
authorYury Selivanov <yury@magic.io>
Wed, 9 Nov 2016 00:16:01 +0000 (19:16 -0500)
committerYury Selivanov <yury@magic.io>
Wed, 9 Nov 2016 00:16:01 +0000 (19:16 -0500)
Some built-in coroutine-like objects might not have __name__ or
__qualname__. A good example of such are 'asend', 'aclose' and
'athrow' coroutine methods of asynchronous generators.

Lib/asyncio/coroutines.py
Lib/test/test_asyncio/test_events.py

index 1db7030205dc26d3c8e0f90848c35140da4ea42f..f46197dea6a391e2dc0273e6381cb72d9e913b68 100644 (file)
@@ -262,8 +262,12 @@ def _format_coroutine(coro):
     assert iscoroutine(coro)
 
     if not hasattr(coro, 'cr_code') and not hasattr(coro, 'gi_code'):
-        # Most likely a Cython coroutine.
-        coro_name = getattr(coro, '__qualname__', coro.__name__)
+        # Most likely a built-in type or a Cython coroutine.
+
+        # Built-in types might not have __qualname__ or __name__.
+        coro_name = getattr(
+            coro, '__qualname__',
+            getattr(coro, '__name__', type(coro).__name__))
         coro_name = '{}()'.format(coro_name)
 
         running = False
index d6489d5f8d13f1e29299e3ea395a572d65c33506..5b32332ff4b8ee67d4646acfae93900aa6fd7a8d 100644 (file)
@@ -2384,8 +2384,6 @@ class HandleTests(test_utils.TestCase):
         # (such as ones compiled with Cython).
 
         class Coro:
-            __name__ = 'AAA'
-
             def send(self, v):
                 pass
 
@@ -2399,6 +2397,7 @@ class HandleTests(test_utils.TestCase):
                 pass
 
         coro = Coro()
+        coro.__name__ = 'AAA'
         self.assertTrue(asyncio.iscoroutine(coro))
         self.assertEqual(coroutines._format_coroutine(coro), 'AAA()')
 
@@ -2408,6 +2407,11 @@ class HandleTests(test_utils.TestCase):
         coro.cr_running = True
         self.assertEqual(coroutines._format_coroutine(coro), 'BBB() running')
 
+        coro = Coro()
+        # Some coroutines might not have '__name__', such as
+        # built-in async_gen.asend().
+        self.assertEqual(coroutines._format_coroutine(coro), 'Coro()')
+
 
 class TimerTests(unittest.TestCase):