[ python-Bugs-1462485 ] StopIteration raised in body of 'with' statement suppressed

SourceForge.net noreply at sourceforge.net
Mon Apr 3 22:06:07 CEST 2006


Bugs item #1462485, was opened at 2006-04-01 00:17
Message generated for change (Settings changed) made by pje
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1462485&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Interpreter Core
Group: Python 2.5
>Status: Closed
>Resolution: Fixed
Priority: 7
Submitted By: Tim Delaney (tcdelaney)
>Assigned to: Phillip J. Eby (pje)
Summary: StopIteration raised in body of 'with' statement suppressed

Initial Comment:
Given:

@contextmanager
def gen():
    print '__enter__'

    try:
        yield
    finally:
        print '__exit__'

with gen():
    raise StopIteration('body')


running the above results in:

__enter__
__exit__


I would have expected instead

__enter__
__exit__
Traceback (most recent call last):
  File "test25.py", line 53, in <module>
    raise StopIteration('body')
StopIteration: body


Note that doing:

with gen():
    raise GeneratorExit('body')

does the expected thing:

__enter__
__exit__
Traceback (most recent call last):
  File "test25.py", line 53, in <module>
    raise GeneratorExit('body')
GeneratorExit: body

----------------------------------------------------------------------

>Comment By: Phillip J. Eby (pje)
Date: 2006-04-03 20:06

Message:
Logged In: YES 
user_id=56214

This looks good, although the second GeneratorExit test
looks redundant.  I went ahead and checked it in, however,
on the theory that more tests are better than fewer.  :)


----------------------------------------------------------------------

Comment By: Tim Delaney (tcdelaney)
Date: 2006-04-02 02:42

Message:
Logged In: YES 
user_id=603121

Attached diffs to test_with.py which adds tests for raising StopIteration (and GeneratorExit) from the body of a with-statement where the context manager is either a generator, or a class instance.

I think the correct behaviour is to return False from GeneratorContextManager.__exit__ if the thrown exception is a StopIteration, and the exact same instance is raised from self.gen.throw(). Diffs for this also attached.

----------------------------------------------------------------------

Comment By: Georg Brandl (gbrandl)
Date: 2006-04-01 07:02

Message:
Logged In: YES 
user_id=849994

Nick, I can't tell whether this is intentional without
rereading the specs. Can you?

----------------------------------------------------------------------

Comment By: Tim Delaney (tcdelaney)
Date: 2006-04-01 00:19

Message:
Logged In: YES 
user_id=603121

Attached code and results.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1462485&group_id=5470


More information about the Python-bugs-list mailing list