@@ -124,9 +124,11 @@ async def coro():
124124 t .cancel ('my message' )
125125 self .assertEqual (t ._cancel_message , 'my message' )
126126
127- with self .assertRaises (asyncio .CancelledError ):
127+ with self .assertRaises (asyncio .CancelledError ) as cm :
128128 self .loop .run_until_complete (t )
129129
130+ self .assertEqual ('my message' , cm .exception .args [0 ])
131+
130132 def test_task_cancel_message_setter (self ):
131133 async def coro ():
132134 pass
@@ -135,9 +137,11 @@ async def coro():
135137 t ._cancel_message = 'my new message'
136138 self .assertEqual (t ._cancel_message , 'my new message' )
137139
138- with self .assertRaises (asyncio .CancelledError ):
140+ with self .assertRaises (asyncio .CancelledError ) as cm :
139141 self .loop .run_until_complete (t )
140142
143+ self .assertEqual ('my new message' , cm .exception .args [0 ])
144+
141145 def test_task_del_collect (self ):
142146 class Evil :
143147 def __del__ (self ):
@@ -590,11 +594,11 @@ async def coro():
590594 with self .assertRaises (asyncio .CancelledError ) as cm :
591595 loop .run_until_complete (task )
592596 exc = cm .exception
593- self .assertEqual (exc .args , () )
597+ self .assertEqual (exc .args , expected_args )
594598
595599 actual = get_innermost_context (exc )
596600 self .assertEqual (actual ,
597- (asyncio .CancelledError , expected_args , 2 ))
601+ (asyncio .CancelledError , expected_args , 0 ))
598602
599603 def test_cancel_with_message_then_future_exception (self ):
600604 # Test Future.exception() after calling cancel() with a message.
@@ -624,11 +628,39 @@ async def coro():
624628 with self .assertRaises (asyncio .CancelledError ) as cm :
625629 loop .run_until_complete (task )
626630 exc = cm .exception
627- self .assertEqual (exc .args , () )
631+ self .assertEqual (exc .args , expected_args )
628632
629633 actual = get_innermost_context (exc )
630634 self .assertEqual (actual ,
631- (asyncio .CancelledError , expected_args , 2 ))
635+ (asyncio .CancelledError , expected_args , 0 ))
636+
637+ def test_cancellation_exception_context (self ):
638+ loop = asyncio .new_event_loop ()
639+ self .set_event_loop (loop )
640+ fut = loop .create_future ()
641+
642+ async def sleep ():
643+ fut .set_result (None )
644+ await asyncio .sleep (10 )
645+
646+ async def coro ():
647+ inner_task = self .new_task (loop , sleep ())
648+ await fut
649+ loop .call_soon (inner_task .cancel , 'msg' )
650+ try :
651+ await inner_task
652+ except asyncio .CancelledError as ex :
653+ raise ValueError ("cancelled" ) from ex
654+
655+ task = self .new_task (loop , coro ())
656+ with self .assertRaises (ValueError ) as cm :
657+ loop .run_until_complete (task )
658+ exc = cm .exception
659+ self .assertEqual (exc .args , ('cancelled' ,))
660+
661+ actual = get_innermost_context (exc )
662+ self .assertEqual (actual ,
663+ (asyncio .CancelledError , ('msg' ,), 1 ))
632664
633665 def test_cancel_with_message_before_starting_task (self ):
634666 loop = asyncio .new_event_loop ()
@@ -648,11 +680,11 @@ async def coro():
648680 with self .assertRaises (asyncio .CancelledError ) as cm :
649681 loop .run_until_complete (task )
650682 exc = cm .exception
651- self .assertEqual (exc .args , ())
683+ self .assertEqual (exc .args , ('my message' , ))
652684
653685 actual = get_innermost_context (exc )
654686 self .assertEqual (actual ,
655- (asyncio .CancelledError , ('my message' ,), 2 ))
687+ (asyncio .CancelledError , ('my message' ,), 0 ))
656688
657689 def test_cancel_yield (self ):
658690 async def task ():
@@ -2296,15 +2328,17 @@ async def main():
22962328 try :
22972329 loop .run_until_complete (main ())
22982330 except asyncio .CancelledError as exc :
2299- self .assertEqual (exc .args , () )
2300- exc_type , exc_args , depth = get_innermost_context (exc )
2301- self .assertEqual (( exc_type , exc_args ),
2302- ( asyncio . CancelledError , expected_args ))
2303- # The exact traceback seems to vary in CI.
2304- self . assertIn ( depth , ( 2 , 3 ) )
2331+ self .assertEqual (exc .args , expected_args )
2332+ actual = get_innermost_context (exc )
2333+ self .assertEqual (
2334+ actual ,
2335+ ( asyncio . CancelledError , expected_args , 0 ),
2336+ )
23052337 else :
2306- self .fail ('gather did not propagate the cancellation '
2307- 'request' )
2338+ self .fail (
2339+ 'gather() does not propagate CancelledError '
2340+ 'raised by inner task to the gather() caller.'
2341+ )
23082342
23092343 def test_exception_traceback (self ):
23102344 # See http://bugs.python.org/issue28843
0 commit comments