[Zope3-dev] Please, no bare 'except:' clauses!
Guido van Rossum
guido@python.org
Mon, 11 Nov 2002 13:02:24 -0500
> BTW, here's a higher order transact() function from my LL2 slides.
> The idea is that yoiu have some function or method that you want to
> execute as a transaction -- call the function spam(). You pass the
> function to spam() and use the result. Example usage:
>
> def spam(...):
> ...
>
> txn_spam = transact(spam)
>
> def transact(f, retries=3):
> def wrapper(*args, **kwargs):
> n = retries
> while n:
> try:
> try:
> return f(*args, **kwargs)
> finally:
> get_transaction().commit()
> except ConflictError:
> n -= 1
> if n == 0:
> raise
> except:
> get_transaction().abort()
> raise
> return wrapper
>
> The txn_spam() function will be executed within a transaction. If
> there are conflicts, it will be tried at most three times. If an
> unexpected error occurs, the transaction will be aborted and the
> objects it modified will be reverted to their former state.
Hm? It looks as if when an exception hapens in the call to f(), the
transaction will first be committed and then aborted.
--Guido van Rossum (home page: http://www.python.org/~guido/)