39 lines
920 B
Python
39 lines
920 B
Python
# context/generator.py
|
|
from contextlib import contextmanager
|
|
|
|
@contextmanager
|
|
def my_context_manager():
|
|
print("Entering 'with' context")
|
|
val = object()
|
|
print(id(val))
|
|
try:
|
|
yield val
|
|
except Exception as e:
|
|
print(f"{type(e)=} {e=} {e.__traceback__=}")
|
|
finally:
|
|
print("Exiting 'with' context")
|
|
|
|
|
|
print("About to enter 'with' context")
|
|
with my_context_manager() as val:
|
|
print("Inside 'with' context")
|
|
print(id(val))
|
|
raise Exception("Exception inside 'with' context")
|
|
print("This line will never be reached")
|
|
|
|
print("After 'with' context")
|
|
|
|
|
|
"""
|
|
$ python context/generator.py
|
|
About to enter 'with' context
|
|
Entering 'with' context
|
|
139768531985040
|
|
Inside 'with' context
|
|
139768531985040
|
|
type(e)=<class 'Exception'> e=Exception("Exception inside 'with'
|
|
context") e.__traceback__=<traceback object at 0x7f1e65a42800>
|
|
Exiting 'with' context
|
|
After 'with' context
|
|
"""
|