Re: does python have useless destructors?

From: Paul Rubin (//phr.cx_at_NOSPAM.invalid)
Date: 06/12/04

Date: 11 Jun 2004 17:08:42 -0700

"Tim Peters" <> 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()

       myfile = open("myfilepath", "w")

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.