This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: yield from missed StopIteration raised from iterator instead of getting value
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.3
process
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 2022-04-11 14:57 by admin. 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)

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);".
msg167593 - (view) Author: Roundup Robot (python-dev) (Python triager) 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)
http://hg.python.org/cpython/rev/ee55f8e4fb50
History
Date User Action Args
2022-04-11 14:57:33adminsetgithub: 59773
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