def run_as_contextmanager(ctx, fn, *arg, **kw):
 obj = ctx.__enter__()
 try:
  result = fn(obj, *arg, **kw)
  ctx.__exit__(None, None, None)
  return result
 except:
  exc_info = sys.exc_info()
  raise_ = ctx.__exit__(*exc_info)
  if (raise_ is None):
   raise
  else:
   return raise_

def run_as_contextmanager ( ctx , fn , * arg , ** kw ) : obj = ctx . __enter__ ( ) try : result = fn ( obj , * arg , ** kw ) ctx . __exit__ ( None , None , None ) return result except : exc_info = sys . exc_info ( ) raise_ = ctx . __exit__ ( * exc_info ) if ( raise_ is None ) : raise else : return raise_
run the given function under the given contextmanager .