@@ -58,6 +58,22 @@ def format_coroutine(qualname, state, src, source_traceback, generator=False):
5858 return 'coro=<%s() %s at %s>' % (qualname , state , src )
5959
6060
61+ def get_innermost_context (exc ):
62+ """
63+ Return information about the innermost exception context in the chain.
64+ """
65+ depth = 0
66+ while True :
67+ context = exc .__context__
68+ if context is None :
69+ break
70+
71+ exc = context
72+ depth += 1
73+
74+ return (type (exc ), exc .args , depth )
75+
76+
6177class Dummy :
6278
6379 def __repr__ (self ):
@@ -112,9 +128,10 @@ async def coro():
112128 self .assertEqual (t ._cancel_message , None )
113129
114130 t .cancel ('my message' )
131+ self .assertEqual (t ._cancel_message , 'my message' )
132+
115133 with self .assertRaises (asyncio .CancelledError ):
116134 self .loop .run_until_complete (t )
117- self .assertEqual (t ._cancel_message , 'my message' )
118135
119136 def test_task_cancel_message_setter (self ):
120137 async def coro ():
@@ -124,10 +141,8 @@ async def coro():
124141 t ._cancel_message = 'my new message'
125142 self .assertEqual (t ._cancel_message , 'my new message' )
126143
127- # Also check that the value is used for cancel().
128144 with self .assertRaises (asyncio .CancelledError ):
129145 self .loop .run_until_complete (t )
130- self .assertEqual (t ._cancel_message , 'my new message' )
131146
132147 def test_task_del_collect (self ):
133148 class Evil :
@@ -574,7 +589,11 @@ async def coro():
574589 with self .assertRaises (asyncio .CancelledError ) as cm :
575590 loop .run_until_complete (task )
576591 exc = cm .exception
577- self .assertEqual (exc .args , expected_args )
592+ self .assertEqual (exc .args , ('' ,))
593+
594+ actual = get_innermost_context (exc )
595+ self .assertEqual (actual ,
596+ (asyncio .CancelledError , expected_args , 2 ))
578597
579598 def test_cancel_with_message_then_future_exception (self ):
580599 # Test Future.exception() after calling cancel() with a message.
@@ -604,7 +623,11 @@ async def coro():
604623 with self .assertRaises (asyncio .CancelledError ) as cm :
605624 loop .run_until_complete (task )
606625 exc = cm .exception
607- self .assertEqual (exc .args , expected_args )
626+ self .assertEqual (exc .args , ('' ,))
627+
628+ actual = get_innermost_context (exc )
629+ self .assertEqual (actual ,
630+ (asyncio .CancelledError , expected_args , 2 ))
608631
609632 def test_cancel_with_message_before_starting_task (self ):
610633 loop = asyncio .new_event_loop ()
@@ -624,7 +647,11 @@ async def coro():
624647 with self .assertRaises (asyncio .CancelledError ) as cm :
625648 loop .run_until_complete (task )
626649 exc = cm .exception
627- self .assertEqual (exc .args , ('my message' ,))
650+ self .assertEqual (exc .args , ('' ,))
651+
652+ actual = get_innermost_context (exc )
653+ self .assertEqual (actual ,
654+ (asyncio .CancelledError , ('my message' ,), 2 ))
628655
629656 def test_cancel_yield (self ):
630657 with self .assertWarns (DeprecationWarning ):
@@ -2460,7 +2487,6 @@ def test_cancel_gather_2(self):
24602487 ]
24612488 for cancel_args , expected_args in cases :
24622489 with self .subTest (cancel_args = cancel_args ):
2463-
24642490 loop = asyncio .new_event_loop ()
24652491 self .addCleanup (loop .close )
24662492
@@ -2478,15 +2504,20 @@ async def main():
24782504 qwe = self .new_task (loop , test ())
24792505 await asyncio .sleep (0.2 )
24802506 qwe .cancel (* cancel_args )
2481- try :
2482- await qwe
2483- except asyncio .CancelledError as exc :
2484- self .assertEqual (exc .args , expected_args )
2485- else :
2486- self .fail ('gather did not propagate the cancellation '
2487- 'request' )
2488-
2489- loop .run_until_complete (main ())
2507+ await qwe
2508+
2509+ try :
2510+ loop .run_until_complete (main ())
2511+ except asyncio .CancelledError as exc :
2512+ self .assertEqual (exc .args , ('' ,))
2513+ exc_type , exc_args , depth = get_innermost_context (exc )
2514+ self .assertEqual ((exc_type , exc_args ),
2515+ (asyncio .CancelledError , expected_args ))
2516+ # The exact traceback seems to vary in CI.
2517+ self .assertIn (depth , (2 , 3 ))
2518+ else :
2519+ self .fail ('gather did not propagate the cancellation '
2520+ 'request' )
24902521
24912522 def test_exception_traceback (self ):
24922523 # See http://bugs.python.org/issue28843
0 commit comments