mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
asyncio: Prevent StopIteration from being thrown into a Future
Patch by Chris Angelico (issue #26221)
This commit is contained in:
parent
dce63234c5
commit
1bd030788d
2 changed files with 7 additions and 0 deletions
|
@ -341,6 +341,9 @@ class Future:
|
|||
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()
|
||||
|
|
|
@ -76,6 +76,10 @@ class FutureTests(test_utils.TestCase):
|
|||
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())
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue