|
Packit |
562c7a |
import sys
|
|
Packit |
562c7a |
|
|
Packit |
562c7a |
|
|
Packit |
562c7a |
def typename(t):
|
|
Packit |
562c7a |
name = type(t).__name__
|
|
Packit |
562c7a |
if sys.version_info < (2,5):
|
|
Packit |
562c7a |
if name == 'classobj' and issubclass(t, MyException):
|
|
Packit |
562c7a |
name = 'type'
|
|
Packit |
562c7a |
elif name == 'instance' and isinstance(t, MyException):
|
|
Packit |
562c7a |
name = 'MyException'
|
|
Packit |
562c7a |
return "<type '%s'>" % name
|
|
Packit |
562c7a |
|
|
Packit |
562c7a |
|
|
Packit |
562c7a |
class MyException(Exception):
|
|
Packit |
562c7a |
pass
|
|
Packit |
562c7a |
|
|
Packit |
562c7a |
|
|
Packit |
562c7a |
class ContextManager(object):
|
|
Packit |
562c7a |
def __init__(self, value, exit_ret = None):
|
|
Packit |
562c7a |
self.value = value
|
|
Packit |
562c7a |
self.exit_ret = exit_ret
|
|
Packit |
562c7a |
|
|
Packit |
562c7a |
def __exit__(self, a, b, tb):
|
|
Packit |
562c7a |
print("exit %s %s %s" % (typename(a), typename(b), typename(tb)))
|
|
Packit |
562c7a |
return self.exit_ret
|
|
Packit |
562c7a |
|
|
Packit |
562c7a |
def __enter__(self):
|
|
Packit |
562c7a |
print("enter")
|
|
Packit |
562c7a |
return self.value
|
|
Packit |
562c7a |
|
|
Packit |
562c7a |
|
|
Packit |
562c7a |
def no_as():
|
|
Packit |
562c7a |
"""
|
|
Packit |
562c7a |
>>> no_as()
|
|
Packit |
562c7a |
enter
|
|
Packit |
562c7a |
hello
|
|
Packit |
562c7a |
exit <type 'NoneType'> <type 'NoneType'> <type 'NoneType'>
|
|
Packit |
562c7a |
"""
|
|
Packit |
562c7a |
with ContextManager("value"):
|
|
Packit |
562c7a |
print("hello")
|
|
Packit |
562c7a |
|
|
Packit |
562c7a |
|
|
Packit |
562c7a |
def basic():
|
|
Packit |
562c7a |
"""
|
|
Packit |
562c7a |
>>> basic()
|
|
Packit |
562c7a |
enter
|
|
Packit |
562c7a |
value
|
|
Packit |
562c7a |
exit <type 'NoneType'> <type 'NoneType'> <type 'NoneType'>
|
|
Packit |
562c7a |
"""
|
|
Packit |
562c7a |
with ContextManager("value") as x:
|
|
Packit |
562c7a |
print(x)
|
|
Packit |
562c7a |
|
|
Packit |
562c7a |
|
|
Packit |
562c7a |
def with_pass():
|
|
Packit |
562c7a |
"""
|
|
Packit |
562c7a |
>>> with_pass()
|
|
Packit |
562c7a |
enter
|
|
Packit |
562c7a |
exit <type 'NoneType'> <type 'NoneType'> <type 'NoneType'>
|
|
Packit |
562c7a |
"""
|
|
Packit |
562c7a |
with ContextManager("value") as x:
|
|
Packit |
562c7a |
pass
|
|
Packit |
562c7a |
|
|
Packit |
562c7a |
|
|
Packit |
562c7a |
def with_return():
|
|
Packit |
562c7a |
"""
|
|
Packit |
562c7a |
>>> print(with_return())
|
|
Packit |
562c7a |
enter
|
|
Packit |
562c7a |
exit <type 'NoneType'> <type 'NoneType'> <type 'NoneType'>
|
|
Packit |
562c7a |
value
|
|
Packit |
562c7a |
"""
|
|
Packit |
562c7a |
with ContextManager("value") as x:
|
|
Packit |
562c7a |
return x
|
|
Packit |
562c7a |
|
|
Packit |
562c7a |
|
|
Packit |
562c7a |
def with_break():
|
|
Packit |
562c7a |
"""
|
|
Packit |
562c7a |
>>> print(with_break())
|
|
Packit |
562c7a |
enter
|
|
Packit |
562c7a |
exit <type 'NoneType'> <type 'NoneType'> <type 'NoneType'>
|
|
Packit |
562c7a |
a
|
|
Packit |
562c7a |
"""
|
|
Packit |
562c7a |
for c in list("abc"):
|
|
Packit |
562c7a |
with ContextManager("value") as x:
|
|
Packit |
562c7a |
break
|
|
Packit |
562c7a |
print("FAILED")
|
|
Packit |
562c7a |
return c
|
|
Packit |
562c7a |
|
|
Packit |
562c7a |
|
|
Packit |
562c7a |
def with_continue():
|
|
Packit |
562c7a |
"""
|
|
Packit |
562c7a |
>>> print(with_continue())
|
|
Packit |
562c7a |
enter
|
|
Packit |
562c7a |
exit <type 'NoneType'> <type 'NoneType'> <type 'NoneType'>
|
|
Packit |
562c7a |
enter
|
|
Packit |
562c7a |
exit <type 'NoneType'> <type 'NoneType'> <type 'NoneType'>
|
|
Packit |
562c7a |
enter
|
|
Packit |
562c7a |
exit <type 'NoneType'> <type 'NoneType'> <type 'NoneType'>
|
|
Packit |
562c7a |
c
|
|
Packit |
562c7a |
"""
|
|
Packit |
562c7a |
for c in list("abc"):
|
|
Packit |
562c7a |
with ContextManager("value") as x:
|
|
Packit |
562c7a |
continue
|
|
Packit |
562c7a |
print("FAILED")
|
|
Packit |
562c7a |
return c
|
|
Packit |
562c7a |
|
|
Packit |
562c7a |
|
|
Packit |
562c7a |
def with_exception(exit_ret):
|
|
Packit |
562c7a |
"""
|
|
Packit |
562c7a |
>>> with_exception(None)
|
|
Packit |
562c7a |
enter
|
|
Packit |
562c7a |
value
|
|
Packit |
562c7a |
exit <type 'type'> <type 'MyException'> <type 'traceback'>
|
|
Packit |
562c7a |
outer except
|
|
Packit |
562c7a |
>>> with_exception(True)
|
|
Packit |
562c7a |
enter
|
|
Packit |
562c7a |
value
|
|
Packit |
562c7a |
exit <type 'type'> <type 'MyException'> <type 'traceback'>
|
|
Packit |
562c7a |
"""
|
|
Packit |
562c7a |
try:
|
|
Packit |
562c7a |
with ContextManager("value", exit_ret=exit_ret) as value:
|
|
Packit |
562c7a |
print(value)
|
|
Packit |
562c7a |
raise MyException()
|
|
Packit |
562c7a |
except:
|
|
Packit |
562c7a |
print("outer except")
|
|
Packit |
562c7a |
|
|
Packit |
562c7a |
|
|
Packit |
562c7a |
def with_real_lock():
|
|
Packit |
562c7a |
"""
|
|
Packit |
562c7a |
>>> with_real_lock()
|
|
Packit |
562c7a |
about to acquire lock
|
|
Packit |
562c7a |
holding lock
|
|
Packit |
562c7a |
lock no longer held
|
|
Packit |
562c7a |
"""
|
|
Packit |
562c7a |
from threading import Lock
|
|
Packit |
562c7a |
lock = Lock()
|
|
Packit |
562c7a |
|
|
Packit |
562c7a |
print("about to acquire lock")
|
|
Packit |
562c7a |
|
|
Packit |
562c7a |
with lock:
|
|
Packit |
562c7a |
print("holding lock")
|
|
Packit |
562c7a |
|
|
Packit |
562c7a |
print("lock no longer held")
|
|
Packit |
562c7a |
|
|
Packit |
562c7a |
|
|
Packit |
562c7a |
def functions_in_with():
|
|
Packit |
562c7a |
"""
|
|
Packit |
562c7a |
>>> f = functions_in_with()
|
|
Packit |
562c7a |
enter
|
|
Packit |
562c7a |
exit <type 'type'> <type 'MyException'> <type 'traceback'>
|
|
Packit |
562c7a |
outer except
|
|
Packit |
562c7a |
>>> f(1)[0]
|
|
Packit |
562c7a |
1
|
|
Packit |
562c7a |
>>> print(f(1)[1])
|
|
Packit |
562c7a |
value
|
|
Packit |
562c7a |
"""
|
|
Packit |
562c7a |
try:
|
|
Packit |
562c7a |
with ContextManager("value") as value:
|
|
Packit |
562c7a |
def f(x): return x, value
|
|
Packit |
562c7a |
make = lambda x:x()
|
|
Packit |
562c7a |
raise make(MyException)
|
|
Packit |
562c7a |
except:
|
|
Packit |
562c7a |
print("outer except")
|
|
Packit |
562c7a |
return f
|
|
Packit |
562c7a |
|
|
Packit |
562c7a |
|
|
Packit |
562c7a |
def multitarget():
|
|
Packit |
562c7a |
"""
|
|
Packit |
562c7a |
>>> multitarget()
|
|
Packit |
562c7a |
enter
|
|
Packit |
562c7a |
1 2 3 4 5
|
|
Packit |
562c7a |
exit <type 'NoneType'> <type 'NoneType'> <type 'NoneType'>
|
|
Packit |
562c7a |
"""
|
|
Packit |
562c7a |
with ContextManager((1, 2, (3, (4, 5)))) as (a, b, (c, (d, e))):
|
|
Packit |
562c7a |
print('%s %s %s %s %s' % (a, b, c, d, e))
|
|
Packit |
562c7a |
|
|
Packit |
562c7a |
|
|
Packit |
562c7a |
def tupletarget():
|
|
Packit |
562c7a |
"""
|
|
Packit |
562c7a |
>>> tupletarget()
|
|
Packit |
562c7a |
enter
|
|
Packit |
562c7a |
(1, 2, (3, (4, 5)))
|
|
Packit |
562c7a |
exit <type 'NoneType'> <type 'NoneType'> <type 'NoneType'>
|
|
Packit |
562c7a |
"""
|
|
Packit |
562c7a |
with ContextManager((1, 2, (3, (4, 5)))) as t:
|
|
Packit |
562c7a |
print(t)
|
|
Packit |
562c7a |
|
|
Packit |
562c7a |
|
|
Packit |
562c7a |
class GetManager(object):
|
|
Packit |
562c7a |
def get(self, *args):
|
|
Packit |
562c7a |
return ContextManager(*args)
|
|
Packit |
562c7a |
|
|
Packit |
562c7a |
|
|
Packit |
562c7a |
def manager_from_expression():
|
|
Packit |
562c7a |
"""
|
|
Packit |
562c7a |
>>> manager_from_expression()
|
|
Packit |
562c7a |
enter
|
|
Packit |
562c7a |
1
|
|
Packit |
562c7a |
exit <type 'NoneType'> <type 'NoneType'> <type 'NoneType'>
|
|
Packit |
562c7a |
enter
|
|
Packit |
562c7a |
2
|
|
Packit |
562c7a |
exit <type 'NoneType'> <type 'NoneType'> <type 'NoneType'>
|
|
Packit |
562c7a |
"""
|
|
Packit |
562c7a |
with GetManager().get(1) as x:
|
|
Packit |
562c7a |
print(x)
|
|
Packit |
562c7a |
g = GetManager()
|
|
Packit |
562c7a |
with g.get(2) as x:
|
|
Packit |
562c7a |
print(x)
|