@@ -2804,6 +2804,7 @@ class CIntrospectionTests(test_utils.TestCase, BaseTaskIntrospectionTests):
28042804
28052805
28062806class BaseCurrentLoopTests :
2807+ current_task = None
28072808
28082809 def setUp (self ):
28092810 super ().setUp ()
@@ -2814,33 +2815,39 @@ def new_task(self, coro):
28142815 raise NotImplementedError
28152816
28162817 def test_current_task_no_running_loop (self ):
2817- self .assertIsNone (asyncio .current_task (loop = self .loop ))
2818+ self .assertIsNone (self .current_task (loop = self .loop ))
28182819
28192820 def test_current_task_no_running_loop_implicit (self ):
28202821 with self .assertRaisesRegex (RuntimeError , 'no running event loop' ):
2821- asyncio .current_task ()
2822+ self .current_task ()
28222823
28232824 def test_current_task_with_implicit_loop (self ):
28242825 async def coro ():
2825- self .assertIs (asyncio .current_task (loop = self .loop ), task )
2826+ self .assertIs (self .current_task (loop = self .loop ), task )
28262827
2827- self .assertIs (asyncio .current_task (None ), task )
2828- self .assertIs (asyncio .current_task (), task )
2828+ self .assertIs (self .current_task (None ), task )
2829+ self .assertIs (self .current_task (), task )
28292830
28302831 task = self .new_task (coro ())
28312832 self .loop .run_until_complete (task )
2832- self .assertIsNone (asyncio .current_task (loop = self .loop ))
2833+ self .assertIsNone (self .current_task (loop = self .loop ))
28332834
28342835
28352836class PyCurrentLoopTests (BaseCurrentLoopTests , test_utils .TestCase ):
2837+ current_task = staticmethod (tasks ._py_current_task )
28362838
28372839 def new_task (self , coro ):
28382840 return tasks ._PyTask (coro , loop = self .loop )
28392841
28402842
2841- @unittest .skipUnless (hasattr (tasks , '_CTask' ),
2843+ @unittest .skipUnless (hasattr (tasks , '_CTask' ) and
2844+ hasattr (tasks , '_c_current_task' ),
28422845 'requires the C _asyncio module' )
28432846class CCurrentLoopTests (BaseCurrentLoopTests , test_utils .TestCase ):
2847+ if hasattr (tasks , '_c_current_task' ):
2848+ current_task = staticmethod (tasks ._c_current_task )
2849+ else :
2850+ current_task = None
28442851
28452852 def new_task (self , coro ):
28462853 return getattr (tasks , '_CTask' )(coro , loop = self .loop )
0 commit comments