Locking and try/finally
Bengt Richter
bokr at oz.net
Thu Dec 5 17:23:05 EST 2002
On Thu, 05 Dec 2002 09:57:46 +0100, Ype Kingma <ykingma at accessforall.nl> wrote:
[...]
>I've had this problem, in jython. I solved it by using methods
>with the try/finally statements which in turn call other methods
>passed as arguments:
>
>def callLocked(l, fun, funArgs):
> l.lock()
> try:
> return apply(fun, funArgs)
> finally:
> l.unlock()
>
>This will let you get rid of all those other try/finally lines.
>One could also use the *funArgs idiom.
>
>Btw. Jython 2.1 has a bug when returning from nested try finally's,
>ie. for a single syntactical level of try/finally the return works ok.,
>but for multiple syntactical levels of try/finally not all finally clauses
>are executed. When I needed multiple locks I encountered the bug.
>Since one of the locks was a read lock this wasn't a nice experience.
>The workaround is simple, just save the return value and return
>after the last finally. The bug was fixed in the meantime, but
>Jython 2.2 is not yet available.
>
Interestingly, it seems that after the l.unlock() in the finally block
you have the possibility of making some test and *conditionally* doing
an overriding return, e.g.,
if not funArgs: return '<non-True funArgs was passed>'
I wonder if that is sanctioned.
Regards,
Bengt Richter
More information about the Python-list
mailing list