Exceptions and locks

Duncan Booth duncan at rcp.co.uk
Wed Apr 11 09:29:05 EDT 2001


brianr at liffe.com wrote in <vt1yqzvb68.fsf at liffe.com>:
>In a similar vein, would the following work? :
>
>class Guard:
<snip>

No it wouldn't work. The reason is that if no exception is thrown the
guard variable will be deleted when g goes out of scope, i.e. when the
function returns, but if an exception is thrown a traceback object is
created which maintains a reference to all the local variables including
g. If this happens the lock will not be released until the traceback is
destroyed which will not happen until another exception is thrown, and
possibly not even then.

I think that even if no exception is thrown this won't work under Jython 
either as it (if my memory serves) depends on garbage collection to clean up 
objects.



More information about the Python-list mailing list