try:
func1(1)
finally:
try:
func2(2,3)
finally:
func3(4,5,6)
func4()
func1()
Хотелось бы так:
with try_finally:
func1(1)
with try_finally:
func2(2,3)
raise_all_exceptions()
try:
func1(1)
finally:
try:
func2(2,3)
finally:
func3(4,5,6)
func4()
func1()
with try_finally:
func1(1)
with try_finally:
func2(2,3)
raise_all_exceptions()
def safe_call( func, ex_list, *args, **kw ):
try:
func( *args, **kw )
except:
ex_list.append( sys.exc_info() )
ex_list = []
safe_call( func1, ex_list, 1 )
safe_call( func2, ex_list, 1, 2, 3 )
proc_exceptions( ex_list )
import time, sys
class Agent():
def doAction(self):
print "Agent.doAction"
def clear(self):
print "Agent.clear"
def check(self):
return "Agent.check"
class Customer():
def clear(self):
print "Customer.clear"
class Admin():
def doAction(self):
print "Admin.doAction"
class Cl():
def __init__(self):
self.admin = Admin()
self.customer = Customer()
self.agent = Agent()
self.permission = (0,1)
self.permissions = dict((((1,2), True), ((3), True),((4), True),((0), True),))
def clean(self):
a,b,c,d,e = range(5)
c_id = condition = None
try:
print 1
if self.customer:
self.customer.clear()
if self.agent:
self.agent.clear()
err = self.agent.check()
if err:
print >> sys.stderr, "Exception 1"
raise Exception("Exception 1")
finally:
if self.permission in ((a, b), c, d, e):
try:
print 2
if self.permission == (a, b):
try:
print 3
self.admin.doAction()
print >> sys.stderr, "Exception 2"
raise Exception("Exception 2")
finally:
if self.permission == c:
self.agent.doAction()
finally:
try:
print 4
reason = None if condition else 'Q'
self.agent.doAction()
time.sleep(1)
state = self.agent.doAction()
if state != 'P':
print 'Tried one more time'
self.agent.doAction()
if False:
print >> sys.stderr, "Exception 3"
raise Exception("Exception 3")
finally:
if self.permission == (a, b):
self.agent.doAction()
print >> sys.stderr, "Exception 4"
raise Exception("Exception 4")
Cl().clean()
import time, sys
class Agent():
def doAction(self):
print "Agent.doAction"
def clear(self):
print "Agent.clear"
def check(self):
return "Agent.check"
class Customer():
def clear(self):
print "Customer.clear"
class Admin():
def doAction(self):
print "Admin.doAction"
class Eater(object):
def __init__(self):
self.exc = None
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
if exc_val is not None:
self.exc = exc_val
return True
def finish(self):
if self.exc is not None:
raise self.exc
class Cl(object):
def __init__(self):
self.admin = Admin()
self.customer = Customer()
self.agent = Agent()
self.permission = (0,1)
self.permissions = dict((((1,2), True),
((3), True),
((4), True),
((0), True),))
def clean(self):
a, b, c, d, e = range(5)
c_id = condition = None
try:
print 1
if self.customer:
self.customer.clear()
if self.agent:
self.agent.clear()
err = self.agent.check()
if err:
print >> sys.stderr, "Exception 1"
raise Exception("Exception 1")
finally:
if self.permission in ((a, b), c, d, e):
try:
print 2
if self.permission == (a, b):
try:
print 3
self.admin.doAction()
print >> sys.stderr, "Exception 2"
raise Exception("Exception 2")
finally:
if self.permission == c:
self.agent.doAction()
finally:
try:
print 4
reason = None if condition else 'Q'
self.agent.doAction()
time.sleep(1)
state = self.agent.doAction()
if state != 'P':
print 'Tried one more time'
self.agent.doAction()
if False:
print >> sys.stderr, "Exception 3"
raise Exception("Exception 3")
finally:
if self.permission == (a, b):
self.agent.doAction()
print >> sys.stderr, "Exception 4"
raise Exception("Exception 4")
def do(self):
eater = Eater()
a, b, c, d, e = range(5)
c_id = condition = None
with eater:
print 1
if self.customer:
self.customer.clear()
if self.agent:
self.agent.clear()
err = self.agent.check()
if err:
print >> sys.stderr, "Exception 1"
raise Exception("Exception 1")
with eater:
if self.permission in ((a, b), c, d, e):
with eater:
print 2
if self.permission == (a, b):
with eater:
print 3
self.admin.doAction()
print >> sys.stderr, "Exception 2"
raise Exception("Exception 2")
with eater:
if self.permission == c:
self.agent.doAction()
with eater:
print 4
reason = None if condition else 'Q'
self.agent.doAction()
time.sleep(1)
state = self.agent.doAction()
if state != 'P':
print 'Tried one more time'
self.agent.doAction()
if False:
print >> sys.stderr, "Exception 3"
raise Exception("Exception 3")
with eater:
if self.permission == (a, b):
self.agent.doAction()
print >> sys.stderr, "Exception 4"
raise Exception("Exception 4")
eater.finish()
import sys
sys.stderr = sys.stdout # drop stdio buffering effect
try:
Cl().clean()
except:
import traceback; traceback.print_exc()
print '-' * 20
Cl().do()