Re: does python have useless destructors?
From: Paul Rubin (//phr.cx_at_NOSPAM.invalid)
Date: 06/12/04
- Next message: Paul Rubin: "Re: Anonymous file closing"
- Previous message: Robert Brewer: "RE: Inheritence confusion"
- In reply to: Tim Peters: "RE: does python have useless destructors?"
- Next in thread: Tim Bradshaw: "Re: does python have useless destructors?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 11 Jun 2004 17:08:42 -0700
"Tim Peters" <tim.one@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:
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.
- Next message: Paul Rubin: "Re: Anonymous file closing"
- Previous message: Robert Brewer: "RE: Inheritence confusion"
- In reply to: Tim Peters: "RE: does python have useless destructors?"
- Next in thread: Tim Bradshaw: "Re: does python have useless destructors?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]