diff options
author | Yury Selivanov <yselivanov@sprymix.com> | 2015-05-22 15:16:47 (GMT) |
---|---|---|
committer | Yury Selivanov <yselivanov@sprymix.com> | 2015-05-22 15:16:47 (GMT) |
commit | 683333955a05a31da5f0b2ca1b3bffb9962fb4b2 (patch) | |
tree | 236016d773133c19e57c14fc69144e46a586549d /Objects/genobject.c | |
parent | e79ec70801e410de9c3110ffe78f98e08114ae16 (diff) | |
download | cpython-683333955a05a31da5f0b2ca1b3bffb9962fb4b2.zip cpython-683333955a05a31da5f0b2ca1b3bffb9962fb4b2.tar.gz cpython-683333955a05a31da5f0b2ca1b3bffb9962fb4b2.tar.bz2 |
Issue 24237: Raise PendingDeprecationWarning per PEP 479
Raise PendingDeprecationWarning when generator raises StopIteration
and no __future__ import is used. Fix offenders in the stdlib
and tests.
See also issue 22906.
Thanks to Nick Coghlan and Berker Peksag for reviews.
Diffstat (limited to 'Objects/genobject.c')
-rw-r--r-- | Objects/genobject.c | 23 |
1 files changed, 20 insertions, 3 deletions
diff --git a/Objects/genobject.c b/Objects/genobject.c index 555e4fd..8e5624d 100644 --- a/Objects/genobject.c +++ b/Objects/genobject.c @@ -143,13 +143,12 @@ gen_send_ex(PyGenObject *gen, PyObject *arg, int exc) } 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 @@ gen_send_ex(PyGenObject *gen, PyObject *arg, int exc) 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) { |