Title: yield from missed StopIteration raised from iterator instead of getting value
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.3
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: alex, dino.viehland, meador.inge, python-dev
Priority: normal Keywords:

Created on 2012-08-07 00:16 by dino.viehland, last changed 2012-08-07 00:53 by python-dev. This issue is now closed.

Messages (2)
msg167591 - (view) Author: Dino Viehland (dino.viehland) * (Python committer) Date: 2012-08-07 00:16
When implementing an iterable object by hand, and raising StopIteration with a value, the value is not provided as the result of the yield from expression.  This differs from the behavior in the "Formal Semantics" section.  Here's an example of how it differs from working with a normal generator:

class C:
    def __iter__(self): return self
    def __next__(self):
            raise StopIteration(100)

def g():
    if False:
        yield 100
    return 100

def f(val):
    x = yield from val
    print('x', x)


This makes it impossible to wrap a generator in a non-generator and get the same behavior.  The issue seems to be that the yield from implementation calls PyIter_Next which clears the StopIteration exception rather than say directly doing something like "(*iter->ob_type->tp_iternext)(iter);".
msg167593 - (view) Author: Roundup Robot (python-dev) Date: 2012-08-07 00:53
New changeset ee55f8e4fb50 by Benjamin Peterson in branch 'default':
fix yield from return value on custom iterators (closes #15568)
Date User Action Args
2012-08-07 00:53:25python-devsetstatus: open -> closed

nosy: + python-dev
messages: + msg167593

resolution: fixed
stage: resolved
2012-08-07 00:20:22meador.ingesetnosy: + meador.inge
2012-08-07 00:17:30alexsetnosy: + alex
2012-08-07 00:16:14dino.viehlandcreate