@@ -461,56 +461,6 @@ def handler(loop, context):
461461 self .mock_pattern ('Unhandled error in exception handler' ),
462462 exc_info = mock .ANY )
463463
464- def test_default_exc_handler_broken (self ):
465- logger = logging .getLogger ('asyncio' )
466- _context = None
467-
468- class Loop (uvloop .Loop ):
469-
470- _selector = mock .Mock ()
471- _process_events = mock .Mock ()
472-
473- def default_exception_handler (self , context ):
474- nonlocal _context
475- _context = context
476- # Simulates custom buggy "default_exception_handler"
477- raise ValueError ('spam' )
478-
479- loop = Loop ()
480- self .addCleanup (loop .close )
481- asyncio .set_event_loop (loop )
482-
483- def run_loop ():
484- def zero_error ():
485- loop .stop ()
486- 1 / 0
487- loop .call_soon (zero_error )
488- loop .run_forever ()
489-
490- with mock .patch .object (logger , 'error' ) as log :
491- run_loop ()
492- log .assert_called_with (
493- 'Exception in default exception handler' ,
494- exc_info = True )
495-
496- def custom_handler (loop , context ):
497- raise ValueError ('ham' )
498-
499- _context = None
500- loop .set_exception_handler (custom_handler )
501- with mock .patch .object (logger , 'error' ) as log :
502- run_loop ()
503- log .assert_called_with (
504- self .mock_pattern ('Exception in default exception.*'
505- 'while handling.*in custom' ),
506- exc_info = True )
507-
508- # Check that original context was passed to default
509- # exception handler.
510- self .assertIn ('context' , _context )
511- self .assertIs (type (_context ['context' ]['exception' ]),
512- ZeroDivisionError )
513-
514464 def test_set_task_factory_invalid (self ):
515465 with self .assertRaisesRegex (
516466 TypeError ,
@@ -663,7 +613,7 @@ def test_loop_create_future(self):
663613 fut .cancel ()
664614
665615 def test_loop_call_soon_handle_cancelled (self ):
666- cb = lambda : False
616+ cb = lambda : False # NoQA
667617 handle = self .loop .call_soon (cb )
668618 self .assertFalse (handle .cancelled ())
669619 handle .cancel ()
@@ -675,7 +625,7 @@ def test_loop_call_soon_handle_cancelled(self):
675625 self .assertFalse (handle .cancelled ())
676626
677627 def test_loop_call_later_handle_cancelled (self ):
678- cb = lambda : False
628+ cb = lambda : False # NoQA
679629 handle = self .loop .call_later (0.01 , cb )
680630 self .assertFalse (handle .cancelled ())
681631 handle .cancel ()
@@ -692,6 +642,58 @@ def test_loop_std_files_cloexec(self):
692642 flags = fcntl .fcntl (fd , fcntl .F_GETFD )
693643 self .assertFalse (flags & fcntl .FD_CLOEXEC )
694644
645+ def test_default_exc_handler_broken (self ):
646+ logger = logging .getLogger ('asyncio' )
647+ _context = None
648+
649+ class Loop (uvloop .Loop ):
650+
651+ _selector = mock .Mock ()
652+ _process_events = mock .Mock ()
653+
654+ def default_exception_handler (self , context ):
655+ nonlocal _context
656+ _context = context
657+ # Simulates custom buggy "default_exception_handler"
658+ raise ValueError ('spam' )
659+
660+ loop = Loop ()
661+ self .addCleanup (loop .close )
662+ self .addCleanup (lambda : asyncio .set_event_loop (None ))
663+
664+ asyncio .set_event_loop (loop )
665+
666+ def run_loop ():
667+ def zero_error ():
668+ loop .stop ()
669+ 1 / 0
670+ loop .call_soon (zero_error )
671+ loop .run_forever ()
672+
673+ with mock .patch .object (logger , 'error' ) as log :
674+ run_loop ()
675+ log .assert_called_with (
676+ 'Exception in default exception handler' ,
677+ exc_info = True )
678+
679+ def custom_handler (loop , context ):
680+ raise ValueError ('ham' )
681+
682+ _context = None
683+ loop .set_exception_handler (custom_handler )
684+ with mock .patch .object (logger , 'error' ) as log :
685+ run_loop ()
686+ log .assert_called_with (
687+ self .mock_pattern ('Exception in default exception.*'
688+ 'while handling.*in custom' ),
689+ exc_info = True )
690+
691+ # Check that original context was passed to default
692+ # exception handler.
693+ self .assertIn ('context' , _context )
694+ self .assertIs (type (_context ['context' ]['exception' ]),
695+ ZeroDivisionError )
696+
695697
696698class TestBaseAIO (_TestBase , AIOTestCase ):
697699 pass
0 commit comments