does python have useless destructors?
Sat Jun 12 02:08:42 CEST 2004
"Tim Peters" <tim.one at comcast.net> 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:
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.
More information about the Python-list