asyncio: Future.set_exception(exc) should instantiate exc if it is a class.

This commit is contained in:
Victor Stinner 2014-01-30 16:01:54 -08:00
parent 91445fbeb0
commit 9572898282
2 changed files with 7 additions and 0 deletions

View File

@ -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()

View File

@ -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)