]> granicus.if.org Git - python/commitdiff
bpo-24638: Improve the error message in asyncio.ensure_future() (#12848)
authorZackery Spytz <zspytz@gmail.com>
Fri, 3 May 2019 15:35:26 +0000 (09:35 -0600)
committerAndrew Svetlov <andrew.svetlov@gmail.com>
Fri, 3 May 2019 15:35:25 +0000 (11:35 -0400)
Lib/asyncio/tasks.py
Lib/test/test_asyncio/test_tasks.py

index 007a459857d2081af706a2bed3115e76ca5fdd7a..b007b74344edb5b44be6e1435596964676f73879 100644 (file)
@@ -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)
index 1cdff528def49d5d5152b1f50d54b7f7e0d5b7a1..c4f6d703549ca264ccb6aaac8f9e95f54195d53a 100644 (file)
@@ -236,6 +236,15 @@ class BaseTaskTests:
         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