Re[2]: [Tutor] fileobject.close() question

Corran Webster cwebster@nevada.edu
Thu, 13 Apr 2000 08:02:08 -0700


>      If the file object is closed at gc (in this case, at the end of the
>      program) and before the buffer is gc'd, why doesn't that close flush
>      the buffer out to the file?  I was under the impression that the
>      buffer was gc'd first and then the close was called (therefore
>      explaining why bytes are lost), the other way doesn't seem to explain
>      what happens.

If you read the original question, you will see that the code was being run
from the interactive prompt.  Because of this, garbage collection never had
a chance to kick in because Python was never shut down (indeed, the file
object never even went out of scope).

If you check the Python source code (in Objects/fileobject.c), you will see
that when the file object is deallocated, it checks to see if its file is
closed, and then closes it if needed.

As other writers have pointed out, however, this garbage collection feature
is _not_ documented as a feature in the language or library references.  As
a result, it may be implementation dependent, and there are no guarantees
that the behaviour will continue in the future (although, to be honest, it
is likely that it will).

Regards,
Corran