RE: does python have useless destructors?

Discussion in 'Python' started by Tim Peters, Jun 11, 2004.

  1. Tim Peters

    Tim Peters Guest

    [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.
     
    Tim Peters, Jun 11, 2004
    #1
    1. Advertising

  2. Tim Peters

    Peter Hansen Guest

    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
     
    Peter Hansen, Jun 11, 2004
    #2
    1. Advertising

  3. Tim Peters

    Paul Rubin Guest

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

    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.
     
    Paul Rubin, Jun 12, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael P. Soulier

    does python have useless destructors?

    Michael P. Soulier, Jun 9, 2004, in forum: Python
    Replies:
    138
    Views:
    2,222
    Christos TZOTZIOY Georgiou
    Jun 22, 2004
  2. Delaney, Timothy C (Timothy)

    RE: does python have useless destructors?

    Delaney, Timothy C (Timothy), Jun 10, 2004, in forum: Python
    Replies:
    4
    Views:
    255
    Tim Bradshaw
    Jun 14, 2004
  3. Robert Brewer

    RE: does python have useless destructors?

    Robert Brewer, Jun 10, 2004, in forum: Python
    Replies:
    1
    Views:
    549
    Duncan Booth
    Jun 10, 2004
  4. Tim Peters
    Replies:
    7
    Views:
    3,593
    David Turner
    Jun 18, 2004
  5. Delaney, Timothy C (Timothy)

    RE: does python have useless destructors?

    Delaney, Timothy C (Timothy), Jun 15, 2004, in forum: Python
    Replies:
    2
    Views:
    413
    Ype Kingma
    Jun 15, 2004
Loading...

Share This Page