Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > RE: does python have useless destructors?

Reply
Thread Tools

RE: does python have useless destructors?

 
 
Tim Peters
Guest
Posts: n/a
 
      06-11-2004
[Delaney, Timothy C]
>>> myfile = open("myfilepath", "w")
>>>
>>> try:
>>> myfile.write(reallybigbuffer)
>>> finally:
>>> myfile.close()


[Tim Bradshaw]
>> I don't think this is save. Is it certain that there can be no problem
>> between the open and the try?


[Peter Hansen]
> 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.



 
Reply With Quote
 
 
 
 
Peter Hansen
Guest
Posts: n/a
 
      06-11-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
 
Reply With Quote
 
 
 
 
Paul Rubin
Guest
Posts: n/a
 
      06-12-2004
"Tim Peters" <(E-Mail Removed)> writes:
> >>> myfile = open("myfilepath", "w")
> >>> try:
> >>> myfile.write(reallybigbuffer)
> >>> finally:
> >>> myfile.close()

>
> 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.


Nice paper, though the solution proposed seems a bit cumbersome, and
blocking signals always needs to be done with care (suppose
"myfilepath" is on an HFS that takes a minute or two to physically
load a tape into a drive in order to open the file).

I think this particular example can be done with no blocking:

class unopened_file:
def close(self): pass
myfile = unopened_file()

try:
myfile = open("myfilepath", "w")
myfile.write(reallybigbuffer)
finally:
myfile.close()

If there's an interrupt before the open completes, "myfile.close" will
operate on the unopened_file and be a do-nothing. Maybe there's some
more general and less ugly way to do something like that.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Useless thread about some useless statistics Daniel Nogradi Python 0 11-15-2006 11:33 PM
does python have useless destructors? Michael P. Soulier Python 138 06-22-2004 09:35 AM
RE: does python have useless destructors? Tim Peters Python 7 06-18-2004 08:34 AM
RE: does python have useless destructors? Delaney, Timothy C (Timothy) Python 4 06-14-2004 06:38 PM
RE: does python have useless destructors? Robert Brewer Python 1 06-10-2004 04:29 PM



Advertisments