@@ -967,6 +967,31 @@ gen_iternext_callback(PyFrameObject *f, int exc, PyObject *result)
967
967
}
968
968
Py_CLEAR (result );
969
969
}
970
+ else if (!result ) {
971
+ /* Check for __future__ generator_stop and conditionally turn
972
+ * a leaking StopIteration into RuntimeError (with its cause
973
+ * set appropriately). */
974
+ if ((((PyCodeObject * )gen -> gi_code )-> co_flags &
975
+ CO_FUTURE_GENERATOR_STOP )
976
+ && PyErr_ExceptionMatches (PyExc_StopIteration ))
977
+ {
978
+ PyObject * exc , * val , * val2 , * tb ;
979
+ PyErr_Fetch (& exc , & val , & tb );
980
+ PyErr_NormalizeException (& exc , & val , & tb );
981
+ if (tb != NULL )
982
+ PyException_SetTraceback (val , tb );
983
+ Py_DECREF (exc );
984
+ Py_XDECREF (tb );
985
+ PyErr_SetString (PyExc_RuntimeError ,
986
+ "generator raised StopIteration" );
987
+ PyErr_Fetch (& exc , & val2 , & tb );
988
+ PyErr_NormalizeException (& exc , & val2 , & tb );
989
+ PyException_SetCause (val2 , val );
990
+ PyException_SetContext (val2 , val );
991
+ Py_INCREF (val );
992
+ PyErr_Restore (exc , val2 , tb );
993
+ }
994
+ }
970
995
971
996
/* We hold references to things in the cframe, if we release it
972
997
before we clear the references, they get incorrectly and
0 commit comments