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: Silent StopIteration exc when raised from generator inside of another generator
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.1, Python 2.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Stepan.Wagner, r.david.murray
Priority: normal Keywords:

Created on 2012-12-04 19:46 by Stepan.Wagner, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (5)
msg176939 - (view) Author: Stepan Wagner (Stepan.Wagner) Date: 2012-12-04 19:46
def emptygen():
    # Or other more meaningful generator
    raise StopIteration
    yield

def wrap(gen):
    next(gen)
    print("This should be printed or StopIteration raised.")
    while True:
        try:
            yield next(gen)
        except StopIteration as exc:
            return

items = wrap(emptygen())
for item in items:
    print(item)

print("End.")
msg176947 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2012-12-04 20:14
I don't see the bug here.

Your for loop calls wrap.  Wrap calls emptygen.  Emptygen raises a StopIteration exception.  That exception is of course propagated upward (it isn't caught by wrap), and the loop stops.
msg176951 - (view) Author: Stepan Wagner (Stepan.Wagner) Date: 2012-12-04 20:28
OK, thanks for explanation.

The behaviour is still strange, because when I delete try...except clause
from wrap, the StopIteration exc from emptygen terminates the program with
traceback.

On Tue, Dec 4, 2012 at 9:14 PM, R. David Murray <report@bugs.python.org>wrote:

>
> R. David Murray added the comment:
>
> I don't see the bug here.
>
> Your for loop calls wrap.  Wrap calls emptygen.  Emptygen raises a
> StopIteration exception.  That exception is of course propagated upward (it
> isn't caught by wrap), and the loop stops.
>
> ----------
> nosy: +r.david.murray
> resolution:  -> invalid
> stage:  -> committed/rejected
> status: open -> closed
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue16610>
> _______________________________________
>
msg176956 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2012-12-04 20:46
The only way I was able to replicate that result was by removing the entire try/except block, including the yield.  In that case, wrap is no longer a generator, so the exception is raised before you enter the for loop.
msg176959 - (view) Author: Stepan Wagner (Stepan.Wagner) Date: 2012-12-04 21:01
Thank you, I wasn't paying attention enough. It works as you describe.

On Tue, Dec 4, 2012 at 9:46 PM, R. David Murray <report@bugs.python.org>wrote:

>
> R. David Murray added the comment:
>
> The only way I was able to replicate that result was by removing the
> entire try/except block, including the yield.  In that case, wrap is no
> longer a generator, so the exception is raised before you enter the for
> loop.
>
> ----------
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue16610>
> _______________________________________
>
History
Date User Action Args
2022-04-11 14:57:39adminsetgithub: 60814
2012-12-04 21:01:37Stepan.Wagnersetmessages: + msg176959
2012-12-04 20:46:38r.david.murraysetmessages: + msg176956
2012-12-04 20:28:16Stepan.Wagnersetmessages: + msg176951
2012-12-04 20:14:36r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg176947

resolution: not a bug
stage: resolved
2012-12-04 19:46:00Stepan.Wagnercreate