Skip to content

Commit b377e49

Browse files
committed
bpo-29587: set exc.__context__ when calling gen.throw() (GH-19811)
Before this commit, if an exception was active inside a generator when calling gen.throw(), then that exception was lost (i.e. there was no implicit exception chaining). This commit fixes that.
1 parent 70ed7a8 commit b377e49

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed
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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,12 @@ _gen_throw(PyGenObject *gen, int close_on_genexit,
512512
}
513513

514514
PyErr_Restore(typ, val, tb);
515+
if (gen->gi_exc_state.exc_type) {
516+
Py_INCREF(gen->gi_exc_state.exc_type);
517+
Py_XINCREF(gen->gi_exc_state.exc_value);
518+
Py_XINCREF(gen->gi_exc_state.exc_traceback);
519+
_PyErr_ChainExceptions(gen->gi_exc_state.exc_type, gen->gi_exc_state.exc_value, gen->gi_exc_state.exc_traceback);
520+
}
515521
return gen_send_ex(gen, Py_None, 1, 0);
516522

517523
failed_throw:

0 commit comments

Comments
 (0)