@@ -546,7 +546,7 @@ async def task():
546
546
try :
547
547
await asyncio .sleep (10 )
548
548
except asyncio .CancelledError :
549
- asyncio .current_task ().uncancel ()
549
+ self .current_task ().uncancel ()
550
550
await asyncio .sleep (10 )
551
551
552
552
try :
@@ -598,7 +598,7 @@ def test_uncancel_structured_blocks(self):
598
598
loop = asyncio .new_event_loop ()
599
599
600
600
async def make_request_with_timeout (* , sleep : float , timeout : float ):
601
- task = asyncio .current_task ()
601
+ task = self .current_task ()
602
602
loop = task .get_loop ()
603
603
604
604
timed_out = False
@@ -1987,41 +1987,41 @@ async def coro():
1987
1987
self .assertIsNone (t2 .result ())
1988
1988
1989
1989
def test_current_task (self ):
1990
- self .assertIsNone (asyncio .current_task (loop = self .loop ))
1990
+ self .assertIsNone (self .current_task (loop = self .loop ))
1991
1991
1992
1992
async def coro (loop ):
1993
- self .assertIs (asyncio .current_task (), task )
1993
+ self .assertIs (self .current_task (), task )
1994
1994
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 )
1997
1997
1998
1998
task = self .new_task (self .loop , coro (self .loop ))
1999
1999
self .loop .run_until_complete (task )
2000
- self .assertIsNone (asyncio .current_task (loop = self .loop ))
2000
+ self .assertIsNone (self .current_task (loop = self .loop ))
2001
2001
2002
2002
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 ))
2004
2004
2005
2005
fut1 = self .new_future (self .loop )
2006
2006
fut2 = self .new_future (self .loop )
2007
2007
2008
2008
async def coro1 (loop ):
2009
- self .assertTrue (asyncio .current_task () is task1 )
2009
+ self .assertTrue (self .current_task () is task1 )
2010
2010
await fut1
2011
- self .assertTrue (asyncio .current_task () is task1 )
2011
+ self .assertTrue (self .current_task () is task1 )
2012
2012
fut2 .set_result (True )
2013
2013
2014
2014
async def coro2 (loop ):
2015
- self .assertTrue (asyncio .current_task () is task2 )
2015
+ self .assertTrue (self .current_task () is task2 )
2016
2016
fut1 .set_result (True )
2017
2017
await fut2
2018
- self .assertTrue (asyncio .current_task () is task2 )
2018
+ self .assertTrue (self .current_task () is task2 )
2019
2019
2020
2020
task1 = self .new_task (self .loop , coro1 (self .loop ))
2021
2021
task2 = self .new_task (self .loop , coro2 (self .loop ))
2022
2022
2023
2023
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 ))
2025
2025
2026
2026
# Some thorough tests for cancellation propagation through
2027
2027
# coroutines, tasks and wait().
@@ -2833,6 +2833,7 @@ class CTask_CFuture_Tests(BaseTaskTests, SetMethodsTest,
2833
2833
Task = getattr (tasks , '_CTask' , None )
2834
2834
Future = getattr (futures , '_CFuture' , None )
2835
2835
all_tasks = getattr (tasks , '_c_all_tasks' , None )
2836
+ current_task = staticmethod (getattr (tasks , '_c_current_task' , None ))
2836
2837
2837
2838
@support .refcount_test
2838
2839
def test_refleaks_in_task___init__ (self ):
@@ -2865,6 +2866,7 @@ class CTask_CFuture_SubclassTests(BaseTaskTests, test_utils.TestCase):
2865
2866
Task = getattr (tasks , '_CTask' , None )
2866
2867
Future = getattr (futures , '_CFuture' , None )
2867
2868
all_tasks = getattr (tasks , '_c_all_tasks' , None )
2869
+ current_task = staticmethod (getattr (tasks , '_c_current_task' , None ))
2868
2870
2869
2871
2870
2872
@unittest .skipUnless (hasattr (tasks , '_CTask' ),
@@ -2875,6 +2877,7 @@ class CTaskSubclass_PyFuture_Tests(BaseTaskTests, test_utils.TestCase):
2875
2877
Task = getattr (tasks , '_CTask' , None )
2876
2878
Future = futures ._PyFuture
2877
2879
all_tasks = getattr (tasks , '_c_all_tasks' , None )
2880
+ current_task = staticmethod (getattr (tasks , '_c_current_task' , None ))
2878
2881
2879
2882
2880
2883
@unittest .skipUnless (hasattr (futures , '_CFuture' ),
@@ -2885,6 +2888,7 @@ class PyTask_CFutureSubclass_Tests(BaseTaskTests, test_utils.TestCase):
2885
2888
Future = getattr (futures , '_CFuture' , None )
2886
2889
Task = tasks ._PyTask
2887
2890
all_tasks = staticmethod (tasks ._py_all_tasks )
2891
+ current_task = staticmethod (tasks ._py_current_task )
2888
2892
2889
2893
2890
2894
@unittest .skipUnless (hasattr (tasks , '_CTask' ),
@@ -2894,6 +2898,7 @@ class CTask_PyFuture_Tests(BaseTaskTests, test_utils.TestCase):
2894
2898
Task = getattr (tasks , '_CTask' , None )
2895
2899
Future = futures ._PyFuture
2896
2900
all_tasks = getattr (tasks , '_c_all_tasks' , None )
2901
+ current_task = staticmethod (getattr (tasks , '_c_current_task' , None ))
2897
2902
2898
2903
2899
2904
@unittest .skipUnless (hasattr (futures , '_CFuture' ),
@@ -2903,6 +2908,7 @@ class PyTask_CFuture_Tests(BaseTaskTests, test_utils.TestCase):
2903
2908
Task = tasks ._PyTask
2904
2909
Future = getattr (futures , '_CFuture' , None )
2905
2910
all_tasks = staticmethod (tasks ._py_all_tasks )
2911
+ current_task = staticmethod (tasks ._py_current_task )
2906
2912
2907
2913
2908
2914
class PyTask_PyFuture_Tests (BaseTaskTests , SetMethodsTest ,
@@ -2911,13 +2917,15 @@ class PyTask_PyFuture_Tests(BaseTaskTests, SetMethodsTest,
2911
2917
Task = tasks ._PyTask
2912
2918
Future = futures ._PyFuture
2913
2919
all_tasks = staticmethod (tasks ._py_all_tasks )
2920
+ current_task = staticmethod (tasks ._py_current_task )
2914
2921
2915
2922
2916
2923
@add_subclass_tests
2917
2924
class PyTask_PyFuture_SubclassTests (BaseTaskTests , test_utils .TestCase ):
2918
2925
Task = tasks ._PyTask
2919
2926
Future = futures ._PyFuture
2920
2927
all_tasks = staticmethod (tasks ._py_all_tasks )
2928
+ current_task = staticmethod (tasks ._py_current_task )
2921
2929
2922
2930
@unittest .skipUnless (hasattr (tasks , '_CTask' ),
2923
2931
'requires the C _asyncio module' )
@@ -3004,44 +3012,60 @@ def done(self):
3004
3012
def test__enter_task (self ):
3005
3013
task = mock .Mock ()
3006
3014
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 ))
3008
3020
self ._enter_task (loop , task )
3009
- self .assertIs (asyncio .current_task (loop ), task )
3021
+ self .assertIs (self .current_task (loop ), task )
3010
3022
self ._leave_task (loop , task )
3023
+ asyncio ._set_running_loop (None )
3011
3024
3012
3025
def test__enter_task_failure (self ):
3013
3026
task1 = mock .Mock ()
3014
3027
task2 = mock .Mock ()
3015
3028
loop = mock .Mock ()
3029
+ asyncio ._set_running_loop (loop )
3016
3030
self ._enter_task (loop , task1 )
3017
3031
with self .assertRaises (RuntimeError ):
3018
3032
self ._enter_task (loop , task2 )
3019
- self .assertIs (asyncio .current_task (loop ), task1 )
3033
+ self .assertIs (self .current_task (loop ), task1 )
3020
3034
self ._leave_task (loop , task1 )
3035
+ asyncio ._set_running_loop (None )
3021
3036
3022
3037
def test__leave_task (self ):
3023
3038
task = mock .Mock ()
3024
3039
loop = mock .Mock ()
3040
+ asyncio ._set_running_loop (loop )
3025
3041
self ._enter_task (loop , task )
3026
3042
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 )
3028
3045
3029
3046
def test__leave_task_failure1 (self ):
3030
3047
task1 = mock .Mock ()
3031
3048
task2 = mock .Mock ()
3032
3049
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 )
3033
3054
self ._enter_task (loop , task1 )
3034
3055
with self .assertRaises (RuntimeError ):
3035
3056
self ._leave_task (loop , task2 )
3036
- self .assertIs (asyncio .current_task (loop ), task1 )
3057
+ self .assertIs (self .current_task (loop ), task1 )
3037
3058
self ._leave_task (loop , task1 )
3059
+ asyncio ._set_running_loop (None )
3038
3060
3039
3061
def test__leave_task_failure2 (self ):
3040
3062
task = mock .Mock ()
3041
3063
loop = mock .Mock ()
3064
+ asyncio ._set_running_loop (loop )
3042
3065
with self .assertRaises (RuntimeError ):
3043
3066
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 )
3045
3069
3046
3070
def test__unregister_task (self ):
3047
3071
task = mock .Mock ()
@@ -3064,6 +3088,7 @@ class PyIntrospectionTests(test_utils.TestCase, BaseTaskIntrospectionTests):
3064
3088
_enter_task = staticmethod (tasks ._py_enter_task )
3065
3089
_leave_task = staticmethod (tasks ._py_leave_task )
3066
3090
all_tasks = staticmethod (tasks ._py_all_tasks )
3091
+ current_task = staticmethod (tasks ._py_current_task )
3067
3092
3068
3093
3069
3094
@unittest .skipUnless (hasattr (tasks , '_c_register_task' ),
@@ -3075,6 +3100,7 @@ class CIntrospectionTests(test_utils.TestCase, BaseTaskIntrospectionTests):
3075
3100
_enter_task = staticmethod (tasks ._c_enter_task )
3076
3101
_leave_task = staticmethod (tasks ._c_leave_task )
3077
3102
all_tasks = staticmethod (tasks ._c_all_tasks )
3103
+ current_task = staticmethod (tasks ._c_current_task )
3078
3104
else :
3079
3105
_register_task = _unregister_task = _enter_task = _leave_task = None
3080
3106
0 commit comments