From: Victor Stinner Date: Fri, 31 Jan 2014 00:01:54 +0000 (-0800) Subject: asyncio: Future.set_exception(exc) should instantiate exc if it is a class. X-Git-Tag: v3.4.0rc1~172 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=95728982825e1c7247d40e8cfcae48b46377baa6;p=python asyncio: Future.set_exception(exc) should instantiate exc if it is a class. --- diff --git a/Lib/asyncio/futures.py b/Lib/asyncio/futures.py index 9ee13e3e2b..d09f423ce4 100644 --- a/Lib/asyncio/futures.py +++ b/Lib/asyncio/futures.py @@ -301,6 +301,8 @@ class Future: """ if self._state != _PENDING: raise InvalidStateError('{}: {!r}'.format(self._state, self)) + if isinstance(exception, type): + exception = exception() self._exception = exception self._state = _FINISHED self._schedule_callbacks() diff --git a/Lib/test/test_asyncio/test_futures.py b/Lib/test/test_asyncio/test_futures.py index d3a741254f..8a6976b17d 100644 --- a/Lib/test/test_asyncio/test_futures.py +++ b/Lib/test/test_asyncio/test_futures.py @@ -79,6 +79,11 @@ class FutureTests(unittest.TestCase): self.assertRaises(asyncio.InvalidStateError, f.set_exception, None) self.assertFalse(f.cancel()) + def test_exception_class(self): + f = asyncio.Future(loop=self.loop) + f.set_exception(RuntimeError) + self.assertIsInstance(f.exception(), RuntimeError) + def test_yield_from_twice(self): f = asyncio.Future(loop=self.loop)