File tree 2 files changed +31
-0
lines changed
2 files changed +31
-0
lines changed Original file line number Diff line number Diff line change 1
1
# IsolatedAsyncioTestCase based tests
2
2
import asyncio
3
+ import contextvars
3
4
import traceback
4
5
import unittest
5
6
from asyncio import tasks
@@ -27,6 +28,25 @@ async def raise_exc():
27
28
else :
28
29
self .fail ('TypeError was not raised' )
29
30
31
+ async def test_task_exc_handler_correct_context (self ):
32
+ # see https://github.com/python/cpython/issues/96704
33
+ name = contextvars .ContextVar ('name' , default = 'foo' )
34
+ exc_handler_called = False
35
+ def exc_handler (* args ):
36
+ self .assertEqual (name .get (), 'bar' )
37
+ nonlocal exc_handler_called
38
+ exc_handler_called = True
39
+
40
+ async def task ():
41
+ name .set ('bar' )
42
+ 1 / 0
43
+
44
+ loop = asyncio .get_running_loop ()
45
+ loop .set_exception_handler (exc_handler )
46
+ self .cls (task ())
47
+ await asyncio .sleep (0 )
48
+ self .assertTrue (exc_handler_called )
49
+
30
50
@unittest .skipUnless (hasattr (tasks , '_CTask' ),
31
51
'requires the C _asyncio module' )
32
52
class CFutureTests (FutureTests , unittest .IsolatedAsyncioTestCase ):
Original file line number Diff line number Diff line change @@ -2362,6 +2362,17 @@ def test_get_coro(self):
2362
2362
finally :
2363
2363
loop .close ()
2364
2364
2365
+ def test_get_context (self ):
2366
+ loop = asyncio .new_event_loop ()
2367
+ coro = coroutine_function ()
2368
+ context = contextvars .copy_context ()
2369
+ try :
2370
+ task = self .new_task (loop , coro , context = context )
2371
+ loop .run_until_complete (task )
2372
+ self .assertIs (task .get_context (), context )
2373
+ finally :
2374
+ loop .close ()
2375
+
2365
2376
2366
2377
def add_subclass_tests (cls ):
2367
2378
BaseTask = cls .Task
You can’t perform that action at this time.
0 commit comments