Barry Searle wrote: > > in the case of nested try/except code generation, > your suggestion could cause a problem: > # 1) this is fine: > doInitialStuff # With no exceptions > excp = 0 > try: > doLotsHere() > except aParticularSetOfExceptions: > excp = 1 > if excp: > handleException() > doLotsMoreStuff() > > # 2) nested code generation has problem: > doInitialStuff # With no exceptions > excp = 0 > try: > > doMOREInitialStuff # With no exceptions > excp = 0 > try: > doMORELotsHere() > except aParticularSetOfExceptions: > excp = 1 #### this causes the problem > if excp: > handleException() > doMORELotsMoreStuff() > > doLotsHere() > except aParticularSetOfExceptions: > excp = 1 > #### excp=1 from inner nested code generation > if excp: > handleException() > doLotsMoreStuff() > > # 3) Hence I return to my planned template code generation: > excp = 0 #### Probaly need to do ONCE, to "bind" variable ?
Yes, but I am bothered that you appear to feel there is a need to 1) record whether an exception occurred and then 2) handle the exception only when you have finished recording whether exceptions occurred and under what conditions. > doInitialStuff # With no exceptions > try: > > doMOREInitialStuff # With no exceptions In which case you might as well put the excp =0 here. > try: > doMORELotsHere() > excp = 0 ##### always reset excp as last action That doesn't make sense to me. If an exception isn't raised then excp will have the value it's always had, which is to say zero, so I don't see why you feel the need for this "resetting" to take place (unless there's a chance that doMORELotsHere() changes excp's value). > except aParticularSetOfExceptions: > excp = 1 > if excp: > handleException() > doMORELotsMoreStuff() > > doLotsHere() > excp = 0 ##### always reset excp as last action > except aParticularSetOfExceptions: > excp = 1 > if excp: #### should be fine > handleException() > doLotsMoreStuff() > > [previous insanities deleted] I don't believe there isn't a simpler way to do what you appear to want to do. Since you are talking about machine-generated code we can agree not to care about ugly in terms of source, but I think we should be careful no to throw the baby out with the bath water here. Rather than casting your question as code it may prove helpful simply to describe the problem in English, and then see if others on the group can't help you structure your code better. Steve -- Steve Holden +44 150 684 7255 +1 800 494 3119 Holden Web LLC www.holdenweb.com PyCon TX 2006 www.pycon.org -- http://mail.python.org/mailman/listinfo/python-list