[code.view]

[top] / python / PyMOTW / contextlib / contextlib_api_error.py

     #!/usr/bin/env python
     # encoding: utf-8
     #
     # Copyright (c) 2010 Doug Hellmann.  All rights reserved.
     #
     """Implementing the context manager API by hand.
     """
     #end_pymotw_header
     
     class Context(object):
     
         def __init__(self, handle_error):
             print '__init__(%s)' % handle_error
             self.handle_error = handle_error
     
         def __enter__(self):
             print '__enter__()'
             return self
         
         def __exit__(self, exc_type, exc_val, exc_tb):
             print '__exit__(%s, %s, %s)' % (exc_type, exc_val, exc_tb)
             return self.handle_error
             
     with Context(True):
         raise RuntimeError('error message handled')
     
     print
     
     with Context(False):
         raise RuntimeError('error message propagated')
     

[top] / python / PyMOTW / contextlib / contextlib_api_error.py

contact | logmethods.com