Skip to content

Commit

Permalink
bpo-24638: Improve the error message in asyncio.ensure_future() (pyth…
Browse files Browse the repository at this point in the history
  • Loading branch information
ZackerySpytz authored and asvetlov committed May 3, 2019
1 parent ceb842e commit 4737b92
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Lib/asyncio/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,8 @@ def ensure_future(coro_or_future, *, loop=None):
return task
elif futures.isfuture(coro_or_future):
if loop is not None and loop is not futures._get_loop(coro_or_future):
raise ValueError('loop argument must agree with Future')
raise ValueError('The future belongs to a different loop than '
'the one specified as the loop argument')
return coro_or_future
elif inspect.isawaitable(coro_or_future):
return ensure_future(_wrap_awaitable(coro_or_future), loop=loop)
Expand Down
9 changes: 9 additions & 0 deletions Lib/test/test_asyncio/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,15 @@ def test_ensure_future_neither(self):
with self.assertRaises(TypeError):
asyncio.ensure_future('ok')

def test_ensure_future_error_msg(self):
loop = asyncio.new_event_loop()
f = self.new_future(self.loop)
with self.assertRaisesRegex(ValueError, 'The future belongs to a '
'different loop than the one specified as '
'the loop argument'):
asyncio.ensure_future(f, loop=loop)
loop.close()

def test_get_stack(self):
T = None

Expand Down

0 comments on commit 4737b92

Please sign in to comment.