Skip to content

Commit 28ac42b

Browse files
authored
add two tests
2 parents 0a33be0 + f910b93 commit 28ac42b

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

Lib/test/test_asyncio/test_futures2.py

+20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# IsolatedAsyncioTestCase based tests
22
import asyncio
3+
import contextvars
34
import traceback
45
import unittest
56
from asyncio import tasks
@@ -27,6 +28,25 @@ async def raise_exc():
2728
else:
2829
self.fail('TypeError was not raised')
2930

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+
3050
@unittest.skipUnless(hasattr(tasks, '_CTask'),
3151
'requires the C _asyncio module')
3252
class CFutureTests(FutureTests, unittest.IsolatedAsyncioTestCase):

Lib/test/test_asyncio/test_tasks.py

+11
Original file line numberDiff line numberDiff line change
@@ -2362,6 +2362,17 @@ def test_get_coro(self):
23622362
finally:
23632363
loop.close()
23642364

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+
23652376

23662377
def add_subclass_tests(cls):
23672378
BaseTask = cls.Task

0 commit comments

Comments
 (0)