does python have useless destructors?

Peter Hansen peter at engcorp.com
Fri Jun 11 16:23:57 EDT 2004


Tim Peters wrote:

> [Peter Hansen] writing about the typical try/finally idiom:
> 
>>Yes, it's certain to be safe (barring multithreaded stuff rebinding
>>'myfile' in between, which is irrelevant to the discussion).  Are you
>>perhaps concerned that the open itself might fail?  If so, it needs its
>>own try/except block, as I noted elsewhere.  The finally is only need
>>*if* the open succeeds, but unless you insert code between open() and
>>'try', it's safe.
> 
> Believe it or not, it isn't entirely "safe", but for a different reason:
> it's quite possible for, e.g., KeyboardInterrupt to get raised and processed
> between the "finally:" and the "myfile.close()" -- Google on
> 
>     "safe asynchronous exceptions for python"
> 
> for a good paper on the topic.

Interesting.  Okay, so I should expand my "multithreaded" comment to
the more general "asynchronous".  For this discussion, I think the
"it's irrelevant" part stands, but it's good to know that unless
one can control all sources of asynchronous exceptions, even a
finally clause is not necessarily guaranteed to be executed.

-Peter



More information about the Python-list mailing list