Skip to content

Commit 8243dfa

Browse files
author
Anselm Kruis
committed
Issue python#112: Prepare Stackless 3.5, fix StopIteration from generators
Fix the handling of StopIteration raised from generators. It was broken by commit 2771a0ac806b. https://bitbucket.org/stackless-dev/stackless/issues/112
1 parent fdcab56 commit 8243dfa

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

Stackless/core/stacklesseval.c

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,13 +1117,12 @@ gen_iternext_callback(PyFrameObject *f, int exc, PyObject *result)
11171117
}
11181118
Py_CLEAR(result);
11191119
}
1120-
else if (!result) {
1120+
else if (!result && PyErr_ExceptionMatches(PyExc_StopIteration)) {
11211121
/* Check for __future__ generator_stop and conditionally turn
11221122
* a leaking StopIteration into RuntimeError (with its cause
11231123
* set appropriately). */
1124-
if ((((PyCodeObject *)gen->gi_code)->co_flags &
1124+
if (((PyCodeObject *)gen->gi_code)->co_flags &
11251125
(CO_FUTURE_GENERATOR_STOP | CO_COROUTINE | CO_ITERABLE_COROUTINE))
1126-
&& PyErr_ExceptionMatches(PyExc_StopIteration))
11271126
{
11281127
PyObject *exc, *val, *val2, *tb;
11291128
PyErr_Fetch(&exc, &val, &tb);
@@ -1141,6 +1140,24 @@ gen_iternext_callback(PyFrameObject *f, int exc, PyObject *result)
11411140
Py_INCREF(val);
11421141
PyErr_Restore(exc, val2, tb);
11431142
}
1143+
else {
1144+
PyObject *exc, *val, *tb;
1145+
1146+
/* Pop the exception before issuing a warning. */
1147+
PyErr_Fetch(&exc, &val, &tb);
1148+
1149+
if (PyErr_WarnFormat(PyExc_PendingDeprecationWarning, 1,
1150+
"generator '%.50S' raised StopIteration",
1151+
gen->gi_qualname)) {
1152+
/* Warning was converted to an error. */
1153+
Py_XDECREF(exc);
1154+
Py_XDECREF(val);
1155+
Py_XDECREF(tb);
1156+
}
1157+
else {
1158+
PyErr_Restore(exc, val, tb);
1159+
}
1160+
}
11441161
}
11451162

11461163
gen->gi_running = 0;

0 commit comments

Comments
 (0)