Skip to content

Commit 0204726

Browse files
authored
bpo-29587: Update gen.throw() to chain exceptions (#19823)
Before this commit, if an exception was active inside a generator when calling gen.throw(), that exception was lost (i.e. there was no implicit exception chaining). This commit fixes that by setting exc.__context__ when calling gen.throw(exc).
1 parent f40bd46 commit 0204726

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

Lib/test/test_generators.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,23 @@ def g():
316316
self.assertEqual(cm.exception.value.value, 2)
317317

318318

319+
class GeneratorThrowTest(unittest.TestCase):
320+
321+
def test_exception_context_set(self):
322+
def f():
323+
try:
324+
raise KeyError('a')
325+
except Exception:
326+
yield
327+
328+
gen = f()
329+
gen.send(None)
330+
with self.assertRaises(ValueError) as cm:
331+
gen.throw(ValueError)
332+
context = cm.exception.__context__
333+
self.assertEqual((type(context), context.args), (KeyError, ('a',)))
334+
335+
319336
class YieldFromTests(unittest.TestCase):
320337
def test_generator_gi_yieldfrom(self):
321338
def a():
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Enable implicit exception chaining when calling :meth:`generator.throw`.

Objects/genobject.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,15 @@ _gen_throw(PyGenObject *gen, int close_on_genexit,
512512
}
513513

514514
PyErr_Restore(typ, val, tb);
515+
/* XXX Should we also handle the case where exc_type is true and
516+
exc_value is false? */
517+
if (gen->gi_exc_state.exc_type && gen->gi_exc_state.exc_value) {
518+
Py_INCREF(gen->gi_exc_state.exc_type);
519+
Py_INCREF(gen->gi_exc_state.exc_value);
520+
Py_XINCREF(gen->gi_exc_state.exc_traceback);
521+
_PyErr_ChainExceptions(gen->gi_exc_state.exc_type,
522+
gen->gi_exc_state.exc_value, gen->gi_exc_state.exc_traceback);
523+
}
515524
return gen_send_ex(gen, Py_None, 1, 0);
516525

517526
failed_throw:

0 commit comments

Comments
 (0)