Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def contextmanager(foo):
- class cls:
- def __init__(self):
- self.it = iter(foo())
- # next(self.it)
- def __enter__(self):
- next(self.it)
- def __exit__(self, exc_type, exc_val, exc_tb):
- # print('__exit__1')
- # next(self.it)
- if exc_type and exc_type is StopIteration:
- return True
- if exc_type:
- # print(exc_val)
- self.it.throw(exc_type(exc_val))
- return True
- def __call__(self, *args, **kwargs):
- return foo(*args, **kwargs)
- return cls
- @contextmanager
- def foo():
- print("__enter__")
- try:
- yield 42
- except Exception as e:
- print(type(e))
- print(str(e))
- finally:
- print("__exit__")
- return
- with foo() as i:
- print(i)
- raise ValueError("OOOPS")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement