@@ -933,12 +933,14 @@ async def async_time_bomb():
933
933
async def test_from_thread_check_cancelled ():
934
934
q = stdlib_queue .Queue ()
935
935
936
- async def child (cancellable ):
936
+ async def child (cancellable , scope ):
937
+ with scope :
937
938
record .append ("start" )
938
939
try :
939
940
return await to_thread_run_sync (f , cancellable = cancellable )
940
941
except _core .Cancelled :
941
942
record .append ("cancel" )
943
+ raise
942
944
finally :
943
945
record .append ("exit" )
944
946
@@ -956,7 +958,7 @@ def f():
956
958
record = []
957
959
ev = threading .Event ()
958
960
async with _core .open_nursery () as nursery :
959
- nursery .start_soon (child , False )
961
+ nursery .start_soon (child , False , _core . CancelScope () )
960
962
await wait_all_tasks_blocked ()
961
963
assert record [0 ] == "start"
962
964
assert q .get (timeout = 1 ) == "Not Cancelled"
@@ -968,14 +970,15 @@ def f():
968
970
# the appropriate cancel scope
969
971
record = []
970
972
ev = threading .Event ()
973
+ scope = _core .CancelScope () # Nursery cancel scope gives false positives
971
974
async with _core .open_nursery () as nursery :
972
- nursery .start_soon (child , False )
975
+ nursery .start_soon (child , False , scope )
973
976
await wait_all_tasks_blocked ()
974
977
assert record [0 ] == "start"
975
978
assert q .get (timeout = 1 ) == "Not Cancelled"
976
- nursery . cancel_scope .cancel ()
979
+ scope .cancel ()
977
980
ev .set ()
978
- assert nursery . cancel_scope .cancelled_caught
981
+ assert scope .cancelled_caught
979
982
assert "cancel" in record
980
983
assert record [- 1 ] == "exit"
981
984
@@ -992,13 +995,14 @@ def f(): # noqa: F811
992
995
993
996
record = []
994
997
ev = threading .Event ()
998
+ scope = _core .CancelScope ()
995
999
async with _core .open_nursery () as nursery :
996
- nursery .start_soon (child , True )
1000
+ nursery .start_soon (child , True , scope )
997
1001
await wait_all_tasks_blocked ()
998
1002
assert record [0 ] == "start"
999
- nursery . cancel_scope .cancel ()
1003
+ scope .cancel ()
1000
1004
ev .set ()
1001
- assert nursery . cancel_scope .cancelled_caught
1005
+ assert scope .cancelled_caught
1002
1006
assert "cancel" in record
1003
1007
assert record [- 1 ] == "exit"
1004
1008
assert q .get (timeout = 1 ) == "Cancelled"
0 commit comments