Skip to content

Commit

Permalink
asyncio: Prevent StopIteration from being thrown into a Future
Browse files Browse the repository at this point in the history
Patch by Chris Angelico (issue #26221)
  • Loading branch information
1st1 committed Mar 2, 2016
1 parent dce6323 commit 1bd0307
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Lib/asyncio/futures.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,9 @@ def set_exception(self, exception):
raise InvalidStateError('{}: {!r}'.format(self._state, self))
if isinstance(exception, type):
exception = exception()
if type(exception) is StopIteration:
raise TypeError("StopIteration interacts badly with generators "
"and cannot be raised into a Future")
self._exception = exception
self._state = _FINISHED
self._schedule_callbacks()
Expand Down
4 changes: 4 additions & 0 deletions Lib/test/test_asyncio/test_futures.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ def test_exception(self):
f = asyncio.Future(loop=self.loop)
self.assertRaises(asyncio.InvalidStateError, f.exception)

# StopIteration cannot be raised into a Future - CPython issue26221
self.assertRaisesRegex(TypeError, "StopIteration .* cannot be raised",
f.set_exception, StopIteration)

f.set_exception(exc)
self.assertFalse(f.cancelled())
self.assertTrue(f.done())
Expand Down

0 comments on commit 1bd0307

Please sign in to comment.