diff -r ff983ee10b3e Lib/test/test_generators.py --- a/Lib/test/test_generators.py Thu May 21 12:03:21 2015 -0400 +++ b/Lib/test/test_generators.py Thu May 21 12:23:28 2015 -0400 @@ -1,6 +1,7 @@ import gc import sys import unittest +import warnings import weakref from test import support @@ -217,6 +218,24 @@ self.assertEqual(next(g), "done") self.assertEqual(sys.exc_info(), (None, None, None)) + def test_stopiteration_warning(self): + # See also PEP 479. + + def gen(): + raise StopIteration + yield + + with self.assertRaises(StopIteration), warnings.catch_warnings(): + warnings.simplefilter('ignore') + next(gen()) + + with self.assertRaisesRegex(PendingDeprecationWarning, + "generator .* raised StopIteration"), \ + warnings.catch_warnings(): + + warnings.simplefilter('error') + next(gen()) + tutorial_tests = """ Let's try a simple generator: diff -r ff983ee10b3e Objects/genobject.c --- a/Objects/genobject.c Thu May 21 12:03:21 2015 -0400 +++ b/Objects/genobject.c Thu May 21 12:23:28 2015 -0400 @@ -143,13 +143,12 @@ } Py_CLEAR(result); } - else if (!result) { + else if (!result && PyErr_ExceptionMatches(PyExc_StopIteration)) { /* Check for __future__ generator_stop and conditionally turn * a leaking StopIteration into RuntimeError (with its cause * set appropriately). */ - if ((((PyCodeObject *)gen->gi_code)->co_flags & + if (((PyCodeObject *)gen->gi_code)->co_flags & (CO_FUTURE_GENERATOR_STOP | CO_COROUTINE | CO_ITERABLE_COROUTINE)) - && PyErr_ExceptionMatches(PyExc_StopIteration)) { PyObject *exc, *val, *val2, *tb; PyErr_Fetch(&exc, &val, &tb); @@ -167,6 +166,24 @@ PyException_SetContext(val2, val); PyErr_Restore(exc, val2, tb); } + else { + PyObject *exc, *val, *tb; + + /* Pop the exception before issuing a warning. */ + PyErr_Fetch(&exc, &val, &tb); + + if (PyErr_WarnFormat(PyExc_PendingDeprecationWarning, 1, + "generator '%.50S' raised StopIteration", + gen->gi_qualname)) { + /* Warning was converted to an error. */ + Py_XDECREF(exc); + Py_XDECREF(val); + Py_XDECREF(tb); + } + else { + PyErr_Restore(exc, val, tb); + } + } } if (!result || f->f_stacktop == NULL) {