[Python-Dev] Destructors and Closing of File Objects

Nikolaus Rath Nikolaus at rath.org
Tue Apr 30 03:00:30 CEST 2013


Armin Rigo <arigo at tunes.org> writes:
> Hi Nikolaus,
>
> On Sat, Apr 27, 2013 at 4:39 AM, Nikolaus Rath <Nikolaus at rath.org> wrote:
>> It's indeed very informative, but it doesn't fully address the question
>> because of the _pyio module which certainly can't use any custom C code.
>> Does that mean that when I'm using x = _pyio.BufferedWriter(), I could loose
>> data in the write buffer when the interpreter exits without me calling
>> x.close(), but when using x = io.BufferedWriter(), the buffer is
>> guaranteed to get flushed?
>
> I actually described the behavior of CPython 2 while not realizing
> that CPython 3 silently dropped this guarantee.  (I also never
> realized that Jython/IronPython don't have the same guarantee; they
> could, if they implement 'atexit', like we did in PyPy.  That's
> however more acceptable if the platform itself doesn't offer the
> guarantee.)
>
> Anyway, it's a guarantee that the C offers, so personally I find it
> reasonable to expect CPython files to offer it too; not offering it is
> kind of saying that there is a feature of C that is actually present
> at a higher level than the exact same feature in Python, which looks
> backward to me.
>
> Additionally, this might be introducing subtle bugs in programs when
> porting them to Python 3.
>
> However I realize that the two arguments presented above might not be
> accepted as relevant.  (http://bugs.python.org/issue17852)

Thanks for the research! I tried writing a test for this myself, but
failed to get the cyclic reference in place properly for it to happen.

Seems I'm not the only one who was unaware and/or surprised by he change
in behavior.


Best,

   -Nikolaus

-- 
 »Time flies like an arrow, fruit flies like a Banana.«

  PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6  02CF A9AD B7F8 AE4E 425C



More information about the Python-Dev mailing list