Skip to content
This repository was archived by the owner on Nov 23, 2017. It is now read-only.

Commit a3bb643

Browse files
committed
Fix _format_coroutine for coroutine-like objects without __name__
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.
1 parent 8e8416f commit a3bb643

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

asyncio/coroutines.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,12 @@ def _format_coroutine(coro):
262262
assert iscoroutine(coro)
263263

264264
if not hasattr(coro, 'cr_code') and not hasattr(coro, 'gi_code'):
265-
# Most likely a Cython coroutine.
266-
coro_name = getattr(coro, '__qualname__', coro.__name__)
265+
# Most likely a built-in type or a Cython coroutine.
266+
267+
# Built-in types might not have __qualname__ or __name__.
268+
coro_name = getattr(
269+
coro, '__qualname__',
270+
getattr(coro, '__name__', type(coro).__name__))
267271
coro_name = '{}()'.format(coro_name)
268272

269273
running = False

tests/test_events.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -2384,8 +2384,6 @@ def test_coroutine_like_object_debug_formatting(self):
23842384
# (such as ones compiled with Cython).
23852385

23862386
class Coro:
2387-
__name__ = 'AAA'
2388-
23892387
def send(self, v):
23902388
pass
23912389

@@ -2399,6 +2397,7 @@ def __await__(self):
23992397
pass
24002398

24012399
coro = Coro()
2400+
coro.__name__ = 'AAA'
24022401
self.assertTrue(asyncio.iscoroutine(coro))
24032402
self.assertEqual(coroutines._format_coroutine(coro), 'AAA()')
24042403

@@ -2408,6 +2407,11 @@ def __await__(self):
24082407
coro.cr_running = True
24092408
self.assertEqual(coroutines._format_coroutine(coro), 'BBB() running')
24102409

2410+
coro = Coro()
2411+
# Some coroutines might not have '__name__', such as
2412+
# built-in async_gen.asend().
2413+
self.assertEqual(coroutines._format_coroutine(coro), 'Coro()')
2414+
24112415

24122416
class TimerTests(unittest.TestCase):
24132417

0 commit comments

Comments
 (0)