]> granicus.if.org Git - python/commitdiff
asyncio: Prevent StopIteration from being thrown into a Future
authorYury Selivanov <yselivanov@sprymix.com>
Wed, 2 Mar 2016 16:03:28 +0000 (11:03 -0500)
committerYury Selivanov <yselivanov@sprymix.com>
Wed, 2 Mar 2016 16:03:28 +0000 (11:03 -0500)
Patch by Chris Angelico (issue #26221)

Lib/asyncio/futures.py
Lib/test/test_asyncio/test_futures.py

index 281fea337524caa4c2e5fbfc028af01ddf7ac264..ddb9cde188395ceb22537287c85998342f18c95c 100644 (file)
@@ -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()
index 55fdff3f8d5e27817e53d4fbc26e6a7503b66e40..358b190072338aa9aa1611df65de255993a9c790 100644 (file)
@@ -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())