Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b37df51

Browse files
committed
fix yield from return value on custom iterators (closes #15568)
1 parent a0abb44 commit b37df51

3 files changed

Lines changed: 18 additions & 1 deletion

File tree

Lib/test/test_pep380.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,20 @@ def eggs(g):
940940
for stack in spam(eggs(gen())):
941941
self.assertTrue('spam' in stack and 'eggs' in stack)
942942

943+
def test_custom_iterator_return(self):
944+
# See issue #15568
945+
class MyIter:
946+
def __iter__(self):
947+
return self
948+
def __next__(self):
949+
raise StopIteration(42)
950+
def gen():
951+
nonlocal ret
952+
ret = yield from MyIter()
953+
ret = None
954+
list(gen())
955+
self.assertEqual(ret, 42)
956+
943957

944958
def test_main():
945959
from test import support

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ What's New in Python 3.3.0 Beta 2?
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #15568: Fix the return value of "yield from" when StopIteration is
14+
raised by a custom iterator.
15+
1316
- Issue #13119: sys.stdout and sys.stderr are now using "\r\n" newline on
1417
Windows, as Python 2.
1518

Python/ceval.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1843,7 +1843,7 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
18431843
} else {
18441844
_Py_IDENTIFIER(send);
18451845
if (u == Py_None)
1846-
retval = PyIter_Next(x);
1846+
retval = Py_TYPE(x)->tp_iternext(x);
18471847
else
18481848
retval = _PyObject_CallMethodId(x, &PyId_send, "O", u);
18491849
}

0 commit comments

Comments
 (0)