summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorChris Jerdonek <chris.jerdonek@gmail.com>2020-05-13 23:18:27 (GMT)
committerGitHub <noreply@github.com>2020-05-13 23:18:27 (GMT)
commit75cd8e48c62c97fdb9d9a94fd2335be06084471d (patch)
treebcd719b2442c302d0fb5100fb57894b6517ce090 /Lib
parentd6fb53fe42d83a10f1372dd92ffaa6a01d2feffb (diff)
downloadcpython-75cd8e48c62c97fdb9d9a94fd2335be06084471d.zip
cpython-75cd8e48c62c97fdb9d9a94fd2335be06084471d.tar.gz
cpython-75cd8e48c62c97fdb9d9a94fd2335be06084471d.tar.bz2
bpo-29587: Make gen.throw() chain exceptions with yield from (GH-19858)
The previous commits on bpo-29587 got exception chaining working with gen.throw() in the `yield` case. This patch also gets the `yield from` case working. As a consequence, implicit exception chaining now also works in the asyncio scenario of awaiting on a task when an exception is already active. Tests are included for both the asyncio case and the pure generator-only case.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_asyncio/test_tasks.py27
-rw-r--r--Lib/test/test_generators.py19
2 files changed, 45 insertions, 1 deletions
diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py
index 68f3b8c..6eb6b46 100644
--- a/Lib/test/test_asyncio/test_tasks.py
+++ b/Lib/test/test_asyncio/test_tasks.py
@@ -466,6 +466,33 @@ class BaseTaskTests:
t = outer()
self.assertEqual(self.loop.run_until_complete(t), 1042)
+ def test_exception_chaining_after_await(self):
+ # Test that when awaiting on a task when an exception is already
+ # active, if the task raises an exception it will be chained
+ # with the original.
+ loop = asyncio.new_event_loop()
+ self.set_event_loop(loop)
+
+ async def raise_error():
+ raise ValueError
+
+ async def run():
+ try:
+ raise KeyError(3)
+ except Exception as exc:
+ task = self.new_task(loop, raise_error())
+ try:
+ await task
+ except Exception as exc:
+ self.assertEqual(type(exc), ValueError)
+ chained = exc.__context__
+ self.assertEqual((type(chained), chained.args),
+ (KeyError, (3,)))
+
+ task = self.new_task(loop, run())
+ loop.run_until_complete(task)
+ loop.close()
+
def test_cancel(self):
def gen():
diff --git a/Lib/test/test_generators.py b/Lib/test/test_generators.py
index e047801..1081107 100644
--- a/Lib/test/test_generators.py
+++ b/Lib/test/test_generators.py
@@ -318,7 +318,7 @@ class ExceptionTest(unittest.TestCase):
class GeneratorThrowTest(unittest.TestCase):
- def test_exception_context_set(self):
+ def test_exception_context_with_yield(self):
def f():
try:
raise KeyError('a')
@@ -332,6 +332,23 @@ class GeneratorThrowTest(unittest.TestCase):
context = cm.exception.__context__
self.assertEqual((type(context), context.args), (KeyError, ('a',)))
+ def test_exception_context_with_yield_from(self):
+ def f():
+ yield
+
+ def g():
+ try:
+ raise KeyError('a')
+ except Exception:
+ yield from f()
+
+ gen = g()
+ gen.send(None)
+ with self.assertRaises(ValueError) as cm:
+ gen.throw(ValueError)
+ context = cm.exception.__context__
+ self.assertEqual((type(context), context.args), (KeyError, ('a',)))
+
def test_throw_after_none_exc_type(self):
def g():
try: