class CManager(object):

    def __enter__(self):
        print('  __enter__')
return self

def __exit__(self, exctype, value, tb):
    print("  __exit__; excptype: '%s'; value: '%s'" % (exctype, value))
if exctype is None:
    return
if sys.exc_info()[1] is not (value or exctype()):
    raise
How to use a python context manager inside a generator