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.
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
# (such as ones compiled with Cython).
class Coro:
- __name__ = 'AAA'
-
def send(self, v):
pass
pass
coro = Coro()
+ coro.__name__ = 'AAA'
self.assertTrue(asyncio.iscoroutine(coro))
self.assertEqual(coroutines._format_coroutine(coro), 'AAA()')
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):