NULL and no exception set from tp_iternext means StopIteration
diff --git a/Python/ceval.c b/Python/ceval.c
index 134d1ee..98219b0 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -1839,7 +1839,8 @@
             if (!retval) {
                 /* iter may be exhausted */
                 Py_CLEAR(x);
-                if (!PyErr_ExceptionMatches(PyExc_StopIteration)) {
+                if (PyErr_Occurred() &&
+                    !PyErr_ExceptionMatches(PyExc_StopIteration)) {
                     /* some other exception */
                     break;
                 }