This repository was archived by the owner on Nov 23, 2017. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +50
-0
lines changed Expand file tree Collapse file tree 2 files changed +50
-0
lines changed Original file line number Diff line number Diff line change @@ -261,6 +261,25 @@ def iscoroutine(obj):
261
261
def _format_coroutine (coro ):
262
262
assert iscoroutine (coro )
263
263
264
+ 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__ )
267
+ coro_name = '{}()' .format (coro_name )
268
+
269
+ running = False
270
+ try :
271
+ running = coro .cr_running
272
+ except AttributeError :
273
+ try :
274
+ running = coro .gi_running
275
+ except AttributeError :
276
+ pass
277
+
278
+ if running :
279
+ return '{} running' .format (coro_name )
280
+ else :
281
+ return coro_name
282
+
264
283
coro_name = None
265
284
if isinstance (coro , CoroWrapper ):
266
285
func = coro .func
Original file line number Diff line number Diff line change 25
25
import tty
26
26
27
27
import asyncio
28
+ from asyncio import coroutines
28
29
from asyncio import proactor_events
29
30
from asyncio import selector_events
30
31
from asyncio import sslproto
@@ -2380,6 +2381,36 @@ def check_source_traceback(h):
2380
2381
h = loop .call_later (0 , noop )
2381
2382
check_source_traceback (h )
2382
2383
2384
+ def test_coroutine_like_object_debug_formatting (self ):
2385
+ # Test that asyncio can format coroutines that are instances of
2386
+ # collections.abc.Coroutine, but lack cr_core or gi_code attributes
2387
+ # (such as ones compiled with Cython).
2388
+
2389
+ class Coro :
2390
+ __name__ = 'AAA'
2391
+
2392
+ def send (self , v ):
2393
+ pass
2394
+
2395
+ def throw (self , * exc ):
2396
+ pass
2397
+
2398
+ def close (self ):
2399
+ pass
2400
+
2401
+ def __await__ (self ):
2402
+ pass
2403
+
2404
+ coro = Coro ()
2405
+ self .assertTrue (asyncio .iscoroutine (coro ))
2406
+ self .assertEqual (coroutines ._format_coroutine (coro ), 'AAA()' )
2407
+
2408
+ coro .__qualname__ = 'BBB'
2409
+ self .assertEqual (coroutines ._format_coroutine (coro ), 'BBB()' )
2410
+
2411
+ coro .cr_running = True
2412
+ self .assertEqual (coroutines ._format_coroutine (coro ), 'BBB() running' )
2413
+
2383
2414
2384
2415
class TimerTests (unittest .TestCase ):
2385
2416
You can’t perform that action at this time.
0 commit comments