Don't raise an exception on normal return from generator. (GH-19473)
diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-04-11-13-07-49.bpo-4022.Ctpn_F.rst b/Misc/NEWS.d/next/Core and Builtins/2020-04-11-13-07-49.bpo-4022.Ctpn_F.rst
new file mode 100644
index 0000000..a13a8e8
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2020-04-11-13-07-49.bpo-4022.Ctpn_F.rst
@@ -0,0 +1 @@
+Improve performance of generators by not raising internal StopIteration.
diff --git a/Objects/genobject.c b/Objects/genobject.c
index 09efbab..1393f42 100644
--- a/Objects/genobject.c
+++ b/Objects/genobject.c
@@ -231,7 +231,8 @@
if (PyAsyncGen_CheckExact(gen)) {
PyErr_SetNone(PyExc_StopAsyncIteration);
}
- else {
+ else if (arg) {
+ /* Set exception if not called by gen_iternext() */
PyErr_SetNone(PyExc_StopIteration);
}
}