@@ -546,7 +546,7 @@ async def task():
546546 try :
547547 await asyncio .sleep (10 )
548548 except asyncio .CancelledError :
549- asyncio .current_task ().uncancel ()
549+ self .current_task ().uncancel ()
550550 await asyncio .sleep (10 )
551551
552552 try :
@@ -598,7 +598,7 @@ def test_uncancel_structured_blocks(self):
598598 loop = asyncio .new_event_loop ()
599599
600600 async def make_request_with_timeout (* , sleep : float , timeout : float ):
601- task = asyncio .current_task ()
601+ task = self .current_task ()
602602 loop = task .get_loop ()
603603
604604 timed_out = False
@@ -1987,41 +1987,41 @@ async def coro():
19871987 self .assertIsNone (t2 .result ())
19881988
19891989 def test_current_task (self ):
1990- self .assertIsNone (asyncio .current_task (loop = self .loop ))
1990+ self .assertIsNone (self .current_task (loop = self .loop ))
19911991
19921992 async def coro (loop ):
1993- self .assertIs (asyncio .current_task (), task )
1993+ self .assertIs (self .current_task (), task )
19941994
1995- self .assertIs (asyncio .current_task (None ), task )
1996- self .assertIs (asyncio .current_task (), task )
1995+ self .assertIs (self .current_task (None ), task )
1996+ self .assertIs (self .current_task (), task )
19971997
19981998 task = self .new_task (self .loop , coro (self .loop ))
19991999 self .loop .run_until_complete (task )
2000- self .assertIsNone (asyncio .current_task (loop = self .loop ))
2000+ self .assertIsNone (self .current_task (loop = self .loop ))
20012001
20022002 def test_current_task_with_interleaving_tasks (self ):
2003- self .assertIsNone (asyncio .current_task (loop = self .loop ))
2003+ self .assertIsNone (self .current_task (loop = self .loop ))
20042004
20052005 fut1 = self .new_future (self .loop )
20062006 fut2 = self .new_future (self .loop )
20072007
20082008 async def coro1 (loop ):
2009- self .assertTrue (asyncio .current_task () is task1 )
2009+ self .assertTrue (self .current_task () is task1 )
20102010 await fut1
2011- self .assertTrue (asyncio .current_task () is task1 )
2011+ self .assertTrue (self .current_task () is task1 )
20122012 fut2 .set_result (True )
20132013
20142014 async def coro2 (loop ):
2015- self .assertTrue (asyncio .current_task () is task2 )
2015+ self .assertTrue (self .current_task () is task2 )
20162016 fut1 .set_result (True )
20172017 await fut2
2018- self .assertTrue (asyncio .current_task () is task2 )
2018+ self .assertTrue (self .current_task () is task2 )
20192019
20202020 task1 = self .new_task (self .loop , coro1 (self .loop ))
20212021 task2 = self .new_task (self .loop , coro2 (self .loop ))
20222022
20232023 self .loop .run_until_complete (asyncio .wait ((task1 , task2 )))
2024- self .assertIsNone (asyncio .current_task (loop = self .loop ))
2024+ self .assertIsNone (self .current_task (loop = self .loop ))
20252025
20262026 # Some thorough tests for cancellation propagation through
20272027 # coroutines, tasks and wait().
@@ -2833,6 +2833,7 @@ class CTask_CFuture_Tests(BaseTaskTests, SetMethodsTest,
28332833 Task = getattr (tasks , '_CTask' , None )
28342834 Future = getattr (futures , '_CFuture' , None )
28352835 all_tasks = getattr (tasks , '_c_all_tasks' , None )
2836+ current_task = staticmethod (getattr (tasks , '_c_current_task' , None ))
28362837
28372838 @support .refcount_test
28382839 def test_refleaks_in_task___init__ (self ):
@@ -2865,6 +2866,7 @@ class CTask_CFuture_SubclassTests(BaseTaskTests, test_utils.TestCase):
28652866 Task = getattr (tasks , '_CTask' , None )
28662867 Future = getattr (futures , '_CFuture' , None )
28672868 all_tasks = getattr (tasks , '_c_all_tasks' , None )
2869+ current_task = staticmethod (getattr (tasks , '_c_current_task' , None ))
28682870
28692871
28702872@unittest .skipUnless (hasattr (tasks , '_CTask' ),
@@ -2875,6 +2877,7 @@ class CTaskSubclass_PyFuture_Tests(BaseTaskTests, test_utils.TestCase):
28752877 Task = getattr (tasks , '_CTask' , None )
28762878 Future = futures ._PyFuture
28772879 all_tasks = getattr (tasks , '_c_all_tasks' , None )
2880+ current_task = staticmethod (getattr (tasks , '_c_current_task' , None ))
28782881
28792882
28802883@unittest .skipUnless (hasattr (futures , '_CFuture' ),
@@ -2885,6 +2888,7 @@ class PyTask_CFutureSubclass_Tests(BaseTaskTests, test_utils.TestCase):
28852888 Future = getattr (futures , '_CFuture' , None )
28862889 Task = tasks ._PyTask
28872890 all_tasks = staticmethod (tasks ._py_all_tasks )
2891+ current_task = staticmethod (tasks ._py_current_task )
28882892
28892893
28902894@unittest .skipUnless (hasattr (tasks , '_CTask' ),
@@ -2894,6 +2898,7 @@ class CTask_PyFuture_Tests(BaseTaskTests, test_utils.TestCase):
28942898 Task = getattr (tasks , '_CTask' , None )
28952899 Future = futures ._PyFuture
28962900 all_tasks = getattr (tasks , '_c_all_tasks' , None )
2901+ current_task = staticmethod (getattr (tasks , '_c_current_task' , None ))
28972902
28982903
28992904@unittest .skipUnless (hasattr (futures , '_CFuture' ),
@@ -2903,6 +2908,7 @@ class PyTask_CFuture_Tests(BaseTaskTests, test_utils.TestCase):
29032908 Task = tasks ._PyTask
29042909 Future = getattr (futures , '_CFuture' , None )
29052910 all_tasks = staticmethod (tasks ._py_all_tasks )
2911+ current_task = staticmethod (tasks ._py_current_task )
29062912
29072913
29082914class PyTask_PyFuture_Tests (BaseTaskTests , SetMethodsTest ,
@@ -2911,13 +2917,15 @@ class PyTask_PyFuture_Tests(BaseTaskTests, SetMethodsTest,
29112917 Task = tasks ._PyTask
29122918 Future = futures ._PyFuture
29132919 all_tasks = staticmethod (tasks ._py_all_tasks )
2920+ current_task = staticmethod (tasks ._py_current_task )
29142921
29152922
29162923@add_subclass_tests
29172924class PyTask_PyFuture_SubclassTests (BaseTaskTests , test_utils .TestCase ):
29182925 Task = tasks ._PyTask
29192926 Future = futures ._PyFuture
29202927 all_tasks = staticmethod (tasks ._py_all_tasks )
2928+ current_task = staticmethod (tasks ._py_current_task )
29212929
29222930@unittest .skipUnless (hasattr (tasks , '_CTask' ),
29232931 'requires the C _asyncio module' )
@@ -3004,44 +3012,60 @@ def done(self):
30043012 def test__enter_task (self ):
30053013 task = mock .Mock ()
30063014 loop = mock .Mock ()
3007- self .assertIsNone (asyncio .current_task (loop ))
3015+ # _enter_task is called by Task.__step while the loop
3016+ # is running, so set the loop as the running loop
3017+ # for a more realistic test.
3018+ asyncio ._set_running_loop (loop )
3019+ self .assertIsNone (self .current_task (loop ))
30083020 self ._enter_task (loop , task )
3009- self .assertIs (asyncio .current_task (loop ), task )
3021+ self .assertIs (self .current_task (loop ), task )
30103022 self ._leave_task (loop , task )
3023+ asyncio ._set_running_loop (None )
30113024
30123025 def test__enter_task_failure (self ):
30133026 task1 = mock .Mock ()
30143027 task2 = mock .Mock ()
30153028 loop = mock .Mock ()
3029+ asyncio ._set_running_loop (loop )
30163030 self ._enter_task (loop , task1 )
30173031 with self .assertRaises (RuntimeError ):
30183032 self ._enter_task (loop , task2 )
3019- self .assertIs (asyncio .current_task (loop ), task1 )
3033+ self .assertIs (self .current_task (loop ), task1 )
30203034 self ._leave_task (loop , task1 )
3035+ asyncio ._set_running_loop (None )
30213036
30223037 def test__leave_task (self ):
30233038 task = mock .Mock ()
30243039 loop = mock .Mock ()
3040+ asyncio ._set_running_loop (loop )
30253041 self ._enter_task (loop , task )
30263042 self ._leave_task (loop , task )
3027- self .assertIsNone (asyncio .current_task (loop ))
3043+ self .assertIsNone (self .current_task (loop ))
3044+ asyncio ._set_running_loop (None )
30283045
30293046 def test__leave_task_failure1 (self ):
30303047 task1 = mock .Mock ()
30313048 task2 = mock .Mock ()
30323049 loop = mock .Mock ()
3050+ # _leave_task is called by Task.__step while the loop
3051+ # is running, so set the loop as the running loop
3052+ # for a more realistic test.
3053+ asyncio ._set_running_loop (loop )
30333054 self ._enter_task (loop , task1 )
30343055 with self .assertRaises (RuntimeError ):
30353056 self ._leave_task (loop , task2 )
3036- self .assertIs (asyncio .current_task (loop ), task1 )
3057+ self .assertIs (self .current_task (loop ), task1 )
30373058 self ._leave_task (loop , task1 )
3059+ asyncio ._set_running_loop (None )
30383060
30393061 def test__leave_task_failure2 (self ):
30403062 task = mock .Mock ()
30413063 loop = mock .Mock ()
3064+ asyncio ._set_running_loop (loop )
30423065 with self .assertRaises (RuntimeError ):
30433066 self ._leave_task (loop , task )
3044- self .assertIsNone (asyncio .current_task (loop ))
3067+ self .assertIsNone (self .current_task (loop ))
3068+ asyncio ._set_running_loop (None )
30453069
30463070 def test__unregister_task (self ):
30473071 task = mock .Mock ()
@@ -3064,6 +3088,7 @@ class PyIntrospectionTests(test_utils.TestCase, BaseTaskIntrospectionTests):
30643088 _enter_task = staticmethod (tasks ._py_enter_task )
30653089 _leave_task = staticmethod (tasks ._py_leave_task )
30663090 all_tasks = staticmethod (tasks ._py_all_tasks )
3091+ current_task = staticmethod (tasks ._py_current_task )
30673092
30683093
30693094@unittest .skipUnless (hasattr (tasks , '_c_register_task' ),
@@ -3075,6 +3100,7 @@ class CIntrospectionTests(test_utils.TestCase, BaseTaskIntrospectionTests):
30753100 _enter_task = staticmethod (tasks ._c_enter_task )
30763101 _leave_task = staticmethod (tasks ._c_leave_task )
30773102 all_tasks = staticmethod (tasks ._c_all_tasks )
3103+ current_task = staticmethod (tasks ._c_current_task )
30783104 else :
30793105 _register_task = _unregister_task = _enter_task = _leave_task = None
30803106
0 commit comments