Re: Weird exception handling behavior -- late evaluation in except clause

2012-12-02 Thread Chris Angelico
On Mon, Dec 3, 2012 at 6:30 PM, Steven D'Aprano wrote: > Yeah, in hindsight it was a pretty crappy example. But this sort of > dynamism really is useful: > > def testRaises(exc, func, *args): > try: > result = func(*args) > except exc: > return > raise AssertionError("e

Re: Weird exception handling behavior -- late evaluation in except clause

2012-12-02 Thread Steven D'Aprano
On Mon, 03 Dec 2012 16:24:50 +1100, Chris Angelico wrote: > On Mon, Dec 3, 2012 at 8:31 AM, Steven D'Aprano > wrote: >> Consider this piece of legal Python code: >> >> Err = None >> if condition(x) > 100: >> Err = OneException >> elif another_condition(x): >> Err = AnotherException >> try

Re: Weird exception handling behavior -- late evaluation in except clause

2012-12-02 Thread Chris Angelico
On Mon, Dec 3, 2012 at 8:31 AM, Steven D'Aprano wrote: > Consider this piece of legal Python code: > > Err = None > if condition(x) > 100: > Err = OneException > elif another_condition(x): > Err = AnotherException > try: > spam(a, b, c) > except Err: > recover() Legal it may be, b

Re: Weird exception handling behavior -- late evaluation in except clause

2012-12-02 Thread Steven D'Aprano
On Sun, 02 Dec 2012 12:25:22 -0500, Roy Smith wrote: > This is kind of weird (Python 2.7.3): > > try: > print "hello" > except foo: > print "foo" > > prints "hello". The problem (IMHO) is that apparently the except clause > doesn't get evaluated until after some exception is caught. Wh

Re: Weird exception handling behavior -- late evaluation in except clause

2012-12-02 Thread Terry Reedy
On 12/2/2012 12:25 PM, Roy Smith wrote: This is kind of weird (Python 2.7.3): try: print "hello" except foo: print "foo" prints "hello". The problem (IMHO) is that apparently the except clause doesn't get evaluated until after some exception is caught. Which means it never notices t

Re: Weird exception handling behavior -- late evaluation in except clause

2012-12-02 Thread Hans Mulder
On 2/12/12 18:25:22, Roy Smith wrote: > This is kind of weird (Python 2.7.3): > > try: > print "hello" > except foo: > print "foo" > > prints "hello". The problem (IMHO) is that apparently the except clause > doesn't get evaluated until after some exception is caught. Which means > it