Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4737b92

Browse files
ZackerySpytzasvetlov
authored andcommitted
bpo-24638: Improve the error message in asyncio.ensure_future() (#12848)
1 parent ceb842e commit 4737b92

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

Lib/asyncio/tasks.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,8 @@ def ensure_future(coro_or_future, *, loop=None):
628628
return task
629629
elif futures.isfuture(coro_or_future):
630630
if loop is not None and loop is not futures._get_loop(coro_or_future):
631-
raise ValueError('loop argument must agree with Future')
631+
raise ValueError('The future belongs to a different loop than '
632+
'the one specified as the loop argument')
632633
return coro_or_future
633634
elif inspect.isawaitable(coro_or_future):
634635
return ensure_future(_wrap_awaitable(coro_or_future), loop=loop)

Lib/test/test_asyncio/test_tasks.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,15 @@ def test_ensure_future_neither(self):
236236
with self.assertRaises(TypeError):
237237
asyncio.ensure_future('ok')
238238

239+
def test_ensure_future_error_msg(self):
240+
loop = asyncio.new_event_loop()
241+
f = self.new_future(self.loop)
242+
with self.assertRaisesRegex(ValueError, 'The future belongs to a '
243+
'different loop than the one specified as '
244+
'the loop argument'):
245+
asyncio.ensure_future(f, loop=loop)
246+
loop.close()
247+
239248
def test_get_stack(self):
240249
T = None
241250

0 commit comments

Comments
 (0)