from __future__ import with_statement
import sys

# simple manager to use for 'with'
class M:
  def __enter__(self): pass
  def __exit__(self, t,v, tb):
    print 'Locals:', t,v
    print 'sys.exc_info(1):', sys.exc_info() # CPython prints None
    return True

def TestWith():
  print '--------------------'
  print 'Test WITH'
  with M():
    raise ValueError(15)
  
  print 'sys.exc_info(2):', sys.exc_info() # CPython prints None


# Test using the substitution described in the pep.
# http://www.python.org/peps/pep-0343.html
def Test24():
        print '--------------------'
        print 'Test Substitution'
        # ..... <begin with> .....
        mgr = (M()) # EXPR
        exit = mgr.__exit__  # Not calling it yet
        value = mgr.__enter__()
        exc = True
        try:
            try:
                #VAR = value  # Only if "as VAR" is present
                raise ValueError(15) #BLOCK
            except:
                # The exceptional case is handled here
                exc = False
                if not exit(*sys.exc_info()):
                    raise
                # The exception is swallowed if exit() returns true
        finally:
            # The normal and non-local-goto cases are handled here
            if exc:
                exit(None, None, None)  
        # ..... <end with> .....
        print 'sys.exc_info(2):', sys.exc_info() # CPython prints None

TestWith()
Test24()
