[New-bugs-announce] [issue15568] yield from missed StopIteration raised from iterator instead of getting value

Dino Viehland report at bugs.python.org
Tue Aug 7 02:16:17 CEST 2012


New submission from Dino Viehland:

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)

list(f(C()))
list(f(g()))


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);".

----------
components: Interpreter Core
messages: 167591
nosy: dino.viehland
priority: normal
severity: normal
status: open
title: yield from missed StopIteration raised from iterator instead of getting value
type: behavior
versions: Python 3.3

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue15568>
_______________________________________


More information about the New-bugs-announce mailing list