@@ -2702,14 +2702,18 @@ def get_event_loop(self):
27022702 asyncio .set_event_loop_policy (Policy ())
27032703 loop = asyncio .new_event_loop ()
27042704
2705- with self .assertRaises (TestError ):
2706- asyncio .get_event_loop ()
2705+ with self .assertWarns (DeprecationWarning ) as cm :
2706+ with self .assertRaises (TestError ):
2707+ asyncio .get_event_loop ()
2708+ self .assertEqual (cm .warnings [0 ].filename , __file__ )
27072709 asyncio .set_event_loop (None )
2708- with self .assertRaises (TestError ):
2709- asyncio .get_event_loop ()
2710+ with self .assertWarns (DeprecationWarning ) as cm :
2711+ with self .assertRaises (TestError ):
2712+ asyncio .get_event_loop ()
2713+ self .assertEqual (cm .warnings [0 ].filename , __file__ )
27102714
27112715 with self .assertRaisesRegex (RuntimeError , 'no running' ):
2712- self . assertIs ( asyncio .get_running_loop (), None )
2716+ asyncio .get_running_loop ()
27132717 self .assertIs (asyncio ._get_running_loop (), None )
27142718
27152719 async def func ():
@@ -2720,20 +2724,73 @@ async def func():
27202724 loop .run_until_complete (func ())
27212725
27222726 asyncio .set_event_loop (loop )
2723- with self .assertRaises (TestError ):
2724- asyncio .get_event_loop ()
2727+ with self .assertWarns (DeprecationWarning ) as cm :
2728+ with self .assertRaises (TestError ):
2729+ asyncio .get_event_loop ()
2730+ self .assertEqual (cm .warnings [0 ].filename , __file__ )
27252731
27262732 asyncio .set_event_loop (None )
2727- with self .assertRaises (TestError ):
2728- asyncio .get_event_loop ()
2733+ with self .assertWarns (DeprecationWarning ) as cm :
2734+ with self .assertRaises (TestError ):
2735+ asyncio .get_event_loop ()
2736+ self .assertEqual (cm .warnings [0 ].filename , __file__ )
27292737
27302738 finally :
27312739 asyncio .set_event_loop_policy (old_policy )
27322740 if loop is not None :
27332741 loop .close ()
27342742
27352743 with self .assertRaisesRegex (RuntimeError , 'no running' ):
2736- self .assertIs (asyncio .get_running_loop (), None )
2744+ asyncio .get_running_loop ()
2745+
2746+ self .assertIs (asyncio ._get_running_loop (), None )
2747+
2748+ def test_get_event_loop_returns_running_loop2 (self ):
2749+ old_policy = asyncio .get_event_loop_policy ()
2750+ try :
2751+ asyncio .set_event_loop_policy (asyncio .DefaultEventLoopPolicy ())
2752+ loop = asyncio .new_event_loop ()
2753+ self .addCleanup (loop .close )
2754+
2755+ with self .assertWarns (DeprecationWarning ) as cm :
2756+ loop2 = asyncio .get_event_loop ()
2757+ self .addCleanup (loop2 .close )
2758+ self .assertEqual (cm .warnings [0 ].filename , __file__ )
2759+ asyncio .set_event_loop (None )
2760+ with self .assertWarns (DeprecationWarning ) as cm :
2761+ with self .assertRaisesRegex (RuntimeError , 'no current' ):
2762+ asyncio .get_event_loop ()
2763+ self .assertEqual (cm .warnings [0 ].filename , __file__ )
2764+
2765+ with self .assertRaisesRegex (RuntimeError , 'no running' ):
2766+ asyncio .get_running_loop ()
2767+ self .assertIs (asyncio ._get_running_loop (), None )
2768+
2769+ async def func ():
2770+ self .assertIs (asyncio .get_event_loop (), loop )
2771+ self .assertIs (asyncio .get_running_loop (), loop )
2772+ self .assertIs (asyncio ._get_running_loop (), loop )
2773+
2774+ loop .run_until_complete (func ())
2775+
2776+ asyncio .set_event_loop (loop )
2777+ with self .assertWarns (DeprecationWarning ) as cm :
2778+ self .assertIs (asyncio .get_event_loop (), loop )
2779+ self .assertEqual (cm .warnings [0 ].filename , __file__ )
2780+
2781+ asyncio .set_event_loop (None )
2782+ with self .assertWarns (DeprecationWarning ) as cm :
2783+ with self .assertRaisesRegex (RuntimeError , 'no current' ):
2784+ asyncio .get_event_loop ()
2785+ self .assertEqual (cm .warnings [0 ].filename , __file__ )
2786+
2787+ finally :
2788+ asyncio .set_event_loop_policy (old_policy )
2789+ if loop is not None :
2790+ loop .close ()
2791+
2792+ with self .assertRaisesRegex (RuntimeError , 'no running' ):
2793+ asyncio .get_running_loop ()
27372794
27382795 self .assertIs (asyncio ._get_running_loop (), None )
27392796
0 commit comments