]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-29587: Add another test for the gen.throw() fix. (GH-19859)
authorChris Jerdonek <chris.jerdonek@gmail.com>
Sun, 17 May 2020 04:14:48 +0000 (21:14 -0700)
committerGitHub <noreply@github.com>
Sun, 17 May 2020 04:14:48 +0000 (21:14 -0700)
Lib/test/test_generators.py

index 348ae15aa6532bbe71dc0ceed996f77eb2695034..87cc2dfc8c6796c5d5e5518c0c497356da4e15d3 100644 (file)
@@ -332,6 +332,28 @@ class GeneratorThrowTest(unittest.TestCase):
         context = cm.exception.__context__
         self.assertEqual((type(context), context.args), (KeyError, ('a',)))
 
+    def test_exception_context_with_yield_inside_generator(self):
+        # Check that the context is also available from inside the generator
+        # with yield, as opposed to outside.
+        def f():
+            try:
+                raise KeyError('a')
+            except Exception:
+                try:
+                    yield
+                except Exception as exc:
+                    self.assertEqual(type(exc), ValueError)
+                    context = exc.__context__
+                    self.assertEqual((type(context), context.args),
+                        (KeyError, ('a',)))
+                    yield 'b'
+
+        gen = f()
+        gen.send(None)
+        actual = gen.throw(ValueError)
+        # This ensures that the assertions inside were executed.
+        self.assertEqual(actual, 'b')
+
     def test_exception_context_with_yield_from(self):
         def f():
             yield