]> granicus.if.org Git - python/commitdiff
fix yield from return value on custom iterators (closes #15568)
authorBenjamin Peterson <benjamin@python.org>
Tue, 7 Aug 2012 00:53:09 +0000 (17:53 -0700)
committerBenjamin Peterson <benjamin@python.org>
Tue, 7 Aug 2012 00:53:09 +0000 (17:53 -0700)
Lib/test/test_pep380.py
Misc/NEWS
Python/ceval.c

index 658bcb99c1f87469d10848e1ea116397c9e81f30..9569e10c9e566cb76ce88d843263da54f64fb878 100644 (file)
@@ -940,6 +940,20 @@ class TestPEP380Operation(unittest.TestCase):
         for stack in spam(eggs(gen())):
             self.assertTrue('spam' in stack and 'eggs' in stack)
 
+    def test_custom_iterator_return(self):
+        # See issue #15568
+        class MyIter:
+            def __iter__(self):
+                return self
+            def __next__(self):
+                raise StopIteration(42)
+        def gen():
+            nonlocal ret
+            ret = yield from MyIter()
+        ret = None
+        list(gen())
+        self.assertEqual(ret, 42)
+
 
 def test_main():
     from test import support
index 6f7b0fcc985eed2ca406a84b1e03bffc89fa8acf..d062df42e2e89d21c29071d4975074897a9502df 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ What's New in Python 3.3.0 Beta 2?
 Core and Builtins
 -----------------
 
+- Issue #15568: Fix the return value of "yield from" when StopIteration is
+  raised by a custom iterator.
+
 - Issue #13119: sys.stdout and sys.stderr are now using "\r\n" newline on
   Windows, as Python 2.
 
index 7e9318bc0b38c82c5804d746e300d63cbca92d43..82bfcc612856b99caf378198e0f6e6d13e519e3f 100644 (file)
@@ -1843,7 +1843,7 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
             } else {
                 _Py_IDENTIFIER(send);
                 if (u == Py_None)
-                    retval = PyIter_Next(x);
+                    retval = Py_TYPE(x)->tp_iternext(x);
                 else
                     retval = _PyObject_CallMethodId(x, &PyId_send, "O", u);
             }