]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-101892: Fix `SystemError` when a callable iterator call exhausts the iterator...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sat, 4 Mar 2023 14:46:17 +0000 (06:46 -0800)
committerGitHub <noreply@github.com>
Sat, 4 Mar 2023 14:46:17 +0000 (06:46 -0800)
(cherry picked from commit 705487c6557c3d8866622b4d32528bf7fc2e4204)

Co-authored-by: Raj <51259329+workingpayload@users.noreply.github.com>
Co-authored-by: Oleg Iarygin <oleg@arhadthedev.net>
Lib/test/test_iter.py
Misc/NEWS.d/next/Library/2023-02-14-09-08-48.gh-issue-101892.FMos8l.rst [new file with mode: 0644]
Objects/iterobject.c

index 6ab9b7a72303091ea0abe8f63f2ca752d9abecaa..30aedb0db3bb3dbf4e5cf9aba5fd1a05af179764 100644 (file)
@@ -348,6 +348,31 @@ class TestCase(unittest.TestCase):
             return i
         self.check_iterator(iter(spam, 20), list(range(10)), pickle=False)
 
+    def test_iter_function_concealing_reentrant_exhaustion(self):
+        # gh-101892: Test two-argument iter() with a function that
+        # exhausts its associated iterator but forgets to either return
+        # a sentinel value or raise StopIteration.
+        HAS_MORE = 1
+        NO_MORE = 2
+
+        def exhaust(iterator):
+            """Exhaust an iterator without raising StopIteration."""
+            list(iterator)
+
+        def spam():
+            # Touching the iterator with exhaust() below will call
+            # spam() once again so protect against recursion.
+            if spam.is_recursive_call:
+                return NO_MORE
+            spam.is_recursive_call = True
+            exhaust(spam.iterator)
+            return HAS_MORE
+
+        spam.is_recursive_call = False
+        spam.iterator = iter(spam, NO_MORE)
+        with self.assertRaises(StopIteration):
+            next(spam.iterator)
+
     # Test exception propagation through function iterator
     def test_exception_function(self):
         def spam(state=[0]):
diff --git a/Misc/NEWS.d/next/Library/2023-02-14-09-08-48.gh-issue-101892.FMos8l.rst b/Misc/NEWS.d/next/Library/2023-02-14-09-08-48.gh-issue-101892.FMos8l.rst
new file mode 100644 (file)
index 0000000..d586779
--- /dev/null
@@ -0,0 +1,3 @@
+Callable iterators no longer raise :class:`SystemError` when the
+callable object exhausts the iterator but forgets to either return a
+sentinel value or raise :class:`StopIteration`.
index ae09d2c0570efa287cd9ab0ef88c26d0db9e9ca9..822f9e293e27cf4ccfd307736574fa111245767b 100644 (file)
@@ -222,7 +222,7 @@ calliter_iternext(calliterobject *it)
     }
 
     result = _PyObject_CallNoArgs(it->it_callable);
-    if (result != NULL{
+    if (result != NULL && it->it_sentinel != NULL){
         int ok;
 
         ok = PyObject_RichCompareBool(it->it_sentinel, result, Py_EQ);
@@ -230,7 +230,6 @@ calliter_iternext(calliterobject *it)
             return result; /* Common case, fast path */
         }
 
-        Py_DECREF(result);
         if (ok > 0) {
             Py_CLEAR(it->it_callable);
             Py_CLEAR(it->it_sentinel);
@@ -241,6 +240,7 @@ calliter_iternext(calliterobject *it)
         Py_CLEAR(it->it_callable);
         Py_CLEAR(it->it_sentinel);
     }
+    Py_XDECREF(result);
     return NULL;
 }