Skip to content

Commit 85acf32

Browse files
committed
all_tasks and current_task are now in asyncio proper
1 parent abf33dd commit 85acf32

2 files changed

Lines changed: 28 additions & 18 deletions

File tree

tests/conftest.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@
99
import trio_asyncio
1010
import inspect
1111

12+
if not hasattr(asyncio,'current_task'):
13+
def current_task(loop=None):
14+
return asyncio.Task.current.task(loop)
15+
asyncio.current_task = current_task
16+
17+
if not hasattr(asyncio,'all_tasks'):
18+
def all_tasks(loop=None):
19+
return asyncio.Task.all_tasks.task(loop)
20+
asyncio.all_tasks = all_tasks
21+
1222
@pytest.fixture
1323
async def loop():
1424
async with trio_asyncio.open_loop() as loop:

tests/python/test_tasks.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,51 +1451,51 @@ def coro():
14511451
def test_current_task(self):
14521452
Task = self.__class__.Task
14531453

1454-
self.assertIsNone(Task.current_task(loop=self.loop))
1454+
self.assertIsNone(asyncio.current_task(loop=self.loop))
14551455

14561456
@asyncio.coroutine
14571457
def coro(loop):
1458-
self.assertTrue(Task.current_task(loop=loop) is task)
1458+
self.assertTrue(asyncio.current_task(loop=loop) is task)
14591459

14601460
# See http://bugs.python.org/issue29271 for details:
14611461
asyncio.set_event_loop(loop)
14621462
try:
1463-
self.assertIs(Task.current_task(None), task)
1464-
self.assertIs(Task.current_task(), task)
1463+
self.assertIs(asyncio.current_task(None), task)
1464+
self.assertIs(asyncio.current_task(), task)
14651465
finally:
14661466
asyncio.set_event_loop(None)
14671467

14681468
task = self.new_task(self.loop, coro(self.loop))
14691469
self.loop.run_until_complete(task)
1470-
self.assertIsNone(Task.current_task(loop=self.loop))
1470+
self.assertIsNone(asyncio.current_task(loop=self.loop))
14711471

14721472
def test_current_task_with_interleaving_tasks(self):
14731473
Task = self.__class__.Task
14741474

1475-
self.assertIsNone(Task.current_task(loop=self.loop))
1475+
self.assertIsNone(asyncio.current_task(loop=self.loop))
14761476

14771477
fut1 = self.new_future(self.loop)
14781478
fut2 = self.new_future(self.loop)
14791479

14801480
@asyncio.coroutine
14811481
def coro1(loop):
1482-
self.assertTrue(Task.current_task(loop=loop) is task1)
1482+
self.assertTrue(asyncio.current_task(loop=loop) is task1)
14831483
yield from fut1
1484-
self.assertTrue(Task.current_task(loop=loop) is task1)
1484+
self.assertTrue(asyncio.current_task(loop=loop) is task1)
14851485
fut2.set_result(True)
14861486

14871487
@asyncio.coroutine
14881488
def coro2(loop):
1489-
self.assertTrue(Task.current_task(loop=loop) is task2)
1489+
self.assertTrue(asyncio.current_task(loop=loop) is task2)
14901490
fut1.set_result(True)
14911491
yield from fut2
1492-
self.assertTrue(Task.current_task(loop=loop) is task2)
1492+
self.assertTrue(asyncio.current_task(loop=loop) is task2)
14931493

14941494
task1 = self.new_task(self.loop, coro1(self.loop))
14951495
task2 = self.new_task(self.loop, coro2(self.loop))
14961496

14971497
self.loop.run_until_complete(asyncio.wait((task1, task2), loop=self.loop))
1498-
self.assertIsNone(Task.current_task(loop=self.loop))
1498+
self.assertIsNone(asyncio.current_task(loop=self.loop))
14991499

15001500
# Some thorough tests for cancellation propagation through
15011501
# coroutines, tasks and wait().
@@ -1815,13 +1815,13 @@ def kill_me(loop):
18151815
coro = kill_me(self.loop)
18161816
task = asyncio.ensure_future(coro, loop=self.loop)
18171817

1818-
self.assertEqual(Task.all_tasks(loop=self.loop), {task})
1818+
self.assertEqual(asyncio.all_tasks(loop=self.loop), {task})
18191819

18201820
# See http://bugs.python.org/issue29271 for details:
18211821
asyncio.set_event_loop(self.loop)
18221822
try:
1823-
self.assertEqual(Task.all_tasks(), {task})
1824-
self.assertEqual(Task.all_tasks(None), {task})
1823+
self.assertEqual(asyncio.all_tasks(), {task})
1824+
self.assertEqual(asyncio.all_tasks(None), {task})
18251825
finally:
18261826
asyncio.set_event_loop(None)
18271827

@@ -1838,7 +1838,7 @@ def kill_me(loop):
18381838
# no more reference to kill_me() task: the task is destroyed by the GC
18391839
support.gc_collect()
18401840

1841-
self.assertEqual(Task.all_tasks(loop=self.loop), set())
1841+
self.assertEqual(asyncio.all_tasks(loop=self.loop), set())
18421842

18431843
mock_handler.assert_called_with(
18441844
self.loop, {
@@ -2033,7 +2033,7 @@ def coro():
20332033
message = m_log.error.call_args[0][0]
20342034
self.assertIn('Task was destroyed but it is pending', message)
20352035

2036-
self.assertEqual(self.Task.all_tasks(self.loop), set())
2036+
self.assertEqual(asyncio.all_tasks(self.loop), set())
20372037

20382038

20392039
def add_subclass_tests(cls):
@@ -2463,7 +2463,7 @@ def add(self, a, b, fail=False, cancel=False):
24632463
if fail:
24642464
raise RuntimeError("Fail!")
24652465
if cancel:
2466-
asyncio.tasks.Task.current_task(self.loop).cancel()
2466+
asyncio.current_task(self.loop).cancel()
24672467
yield
24682468
return a + b
24692469

@@ -2508,7 +2508,7 @@ def test_run_coroutine_threadsafe_with_timeout(self):
25082508
self.loop.run_until_complete(future)
25092509
test_utils.run_briefly(self.loop)
25102510
# Check that there's no pending task (add has been cancelled)
2511-
for task in asyncio.Task.all_tasks(self.loop):
2511+
for task in asyncio.all_tasks(self.loop):
25122512
self.assertTrue(task.done())
25132513

25142514
def test_run_coroutine_threadsafe_task_cancelled(self):

0 commit comments

Comments
 (0)