Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Silent StopIteration exc when raised from generator inside of another generator #60814

Closed
StepanWagner mannequin opened this issue Dec 4, 2012 · 5 comments
Closed

Silent StopIteration exc when raised from generator inside of another generator #60814

StepanWagner mannequin opened this issue Dec 4, 2012 · 5 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@StepanWagner
Copy link
Mannequin

StepanWagner mannequin commented Dec 4, 2012

BPO 16610
Nosy @bitdancer

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2012-12-04.20:14:36.212>
created_at = <Date 2012-12-04.19:46:00.182>
labels = ['interpreter-core', 'type-bug', 'invalid']
title = 'Silent StopIteration exc when raised from generator inside of another generator'
updated_at = <Date 2012-12-04.21:01:37.833>
user = 'https://bugs.python.org/StepanWagner'

bugs.python.org fields:

activity = <Date 2012-12-04.21:01:37.833>
actor = 'Stepan.Wagner'
assignee = 'none'
closed = True
closed_date = <Date 2012-12-04.20:14:36.212>
closer = 'r.david.murray'
components = ['Interpreter Core']
creation = <Date 2012-12-04.19:46:00.182>
creator = 'Stepan.Wagner'
dependencies = []
files = []
hgrepos = []
issue_num = 16610
keywords = []
message_count = 5.0
messages = ['176939', '176947', '176951', '176956', '176959']
nosy_count = 2.0
nosy_names = ['r.david.murray', 'Stepan.Wagner']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue16610'
versions = ['Python 2.6', 'Python 3.1']

@StepanWagner
Copy link
Mannequin Author

StepanWagner mannequin commented Dec 4, 2012

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

@StepanWagner StepanWagner mannequin added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Dec 4, 2012
@bitdancer
Copy link
Member

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.

@StepanWagner
Copy link
Mannequin Author

StepanWagner mannequin commented Dec 4, 2012

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\>


@bitdancer
Copy link
Member

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.

@StepanWagner
Copy link
Mannequin Author

StepanWagner mannequin commented Dec 4, 2012

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\>


@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant