Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7403e91

Browse files
committed
Issue #23996: Avoid a crash when a delegated generator raises an unnormalized StopIteration exception. Patch by Stefan Behnel.
1 parent 8c99a6d commit 7403e91

2 files changed

Lines changed: 25 additions & 5 deletions

File tree

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ Release date: tba
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #23996: Avoid a crash when a delegated generator raises an
14+
unnormalized StopIteration exception. Patch by Stefan Behnel.
15+
1316
- Issue #24022: Fix tokenizer crash when processing undecodable source code.
1417

1518
- Issue #23309: Avoid a deadlock at shutdown if a daemon thread is aborted

Objects/genobject.c

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -396,13 +396,30 @@ _PyGen_FetchStopIterationValue(PyObject **pvalue) {
396396

397397
if (PyErr_ExceptionMatches(PyExc_StopIteration)) {
398398
PyErr_Fetch(&et, &ev, &tb);
399-
Py_XDECREF(et);
400-
Py_XDECREF(tb);
401399
if (ev) {
402-
value = ((PyStopIterationObject *)ev)->value;
403-
Py_INCREF(value);
404-
Py_DECREF(ev);
400+
/* exception will usually be normalised already */
401+
if (Py_TYPE(ev) == (PyTypeObject *) et
402+
|| PyObject_IsInstance(ev, PyExc_StopIteration)) {
403+
value = ((PyStopIterationObject *)ev)->value;
404+
Py_INCREF(value);
405+
Py_DECREF(ev);
406+
} else if (et == PyExc_StopIteration) {
407+
/* avoid normalisation and take ev as value */
408+
value = ev;
409+
} else {
410+
/* normalisation required */
411+
PyErr_NormalizeException(&et, &ev, &tb);
412+
if (!PyObject_IsInstance(ev, PyExc_StopIteration)) {
413+
PyErr_Restore(et, ev, tb);
414+
return -1;
415+
}
416+
value = ((PyStopIterationObject *)ev)->value;
417+
Py_INCREF(value);
418+
Py_DECREF(ev);
419+
}
405420
}
421+
Py_XDECREF(et);
422+
Py_XDECREF(tb);
406423
} else if (PyErr_Occurred()) {
407424
return -1;
408425
}

0 commit comments

Comments
 (0)