[Python-Dev] 'with' __exit__ doesn't set sys.exc_info()

Mike Stall jmstall at exchange.microsoft.com
Tue Dec 18 19:11:01 CET 2007

Can somebody confirm the following behavior is expected: When a 'with' statement invokes __exit__(), it looks like sys.exc_info() is not actually set.

I know this may be a very pedantic detail,  but I'm working on IronPython and would like to make it consistent with CPython's behavior.

The PEP (http://www.python.org/dev/peps/pep-0343/ ) says that 'With' can be substituted as follows:

        mgr = (EXPR)
        exit = mgr.__exit__  # Not calling it yet
        value = mgr.__enter__()
        exc = True
                VAR = value  # Only if "as VAR" is present
                # The exceptional case is handled here
                exc = False
                if not exit(*sys.exc_info()):
                # The exception is swallowed if exit() returns true
            # The normal and non-local-goto cases are handled here
            if exc:
                exit(None, None, None)

and that "The details of the above translation are intended to prescribe the exact semantics.". This implies that sys.exc_info() would be set when exit is invoked.

I'm finding in practice that sys.exc_info() is not set when __exit__() is invoked in the exceptional case.   I attached a simple repro (repro.py) to demonstrate exactly what I mean.

Can somebody confirm this is the expected behavior?


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-dev/attachments/20071218/98926379/attachment.htm 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: repro.py
Type: application/octet-stream
Size: 1475 bytes
Desc: repro.py
Url : http://mail.python.org/pipermail/python-dev/attachments/20071218/98926379/attachment.obj 

More information about the Python-Dev mailing list