summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorNicolas Tessore <n.tessore@ucl.ac.uk>2023-05-23 20:51:56 (GMT)
committerGitHub <noreply@github.com>2023-05-23 20:51:56 (GMT)
commitd56c933992c86986bd58eb3880aed0ed1b0cadc9 (patch)
treed5c4630de7661b61442891cc296584e24ae6dbab /Objects
parent50fce89d123b25e53fa8a0303a169e8887154a0e (diff)
downloadcpython-d56c933992c86986bd58eb3880aed0ed1b0cadc9.zip
cpython-d56c933992c86986bd58eb3880aed0ed1b0cadc9.tar.gz
cpython-d56c933992c86986bd58eb3880aed0ed1b0cadc9.tar.bz2
gh-104770: Let generator.close() return value (#104771)
Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
Diffstat (limited to 'Objects')
-rw-r--r--Objects/genobject.c11
1 files changed, 8 insertions, 3 deletions
diff --git a/Objects/genobject.c b/Objects/genobject.c
index 9252c65..1abfc83 100644
--- a/Objects/genobject.c
+++ b/Objects/genobject.c
@@ -408,11 +408,16 @@ gen_close(PyGenObject *gen, PyObject *args)
PyErr_SetString(PyExc_RuntimeError, msg);
return NULL;
}
- if (PyErr_ExceptionMatches(PyExc_StopIteration)
- || PyErr_ExceptionMatches(PyExc_GeneratorExit)) {
- PyErr_Clear(); /* ignore these errors */
+ assert(PyErr_Occurred());
+ if (PyErr_ExceptionMatches(PyExc_GeneratorExit)) {
+ PyErr_Clear(); /* ignore this error */
Py_RETURN_NONE;
}
+ /* if the generator returned a value while closing, StopIteration was
+ * raised in gen_send_ex() above; retrieve and return the value here */
+ if (_PyGen_FetchStopIterationValue(&retval) == 0) {
+ return retval;
+ }
return NULL;
}