[New-bugs-announce] [issue10125] Closing a file within its writelines method causes segfault

intuited report at bugs.python.org
Sat Oct 16 19:00:21 CEST 2010


New submission from intuited <cecinemapasderange at gmail.com>:

Discovered when using the current Ubuntu 10.04 package of Python: 2.6.5-0ubuntu1

Reproducible with ::

    outfile = open("tmpout", "w")
    outfile.writelines(f() or "line" for f in (outfile.close,))

This problem is probably most likely to be encountered when using the ``fileinput`` module, because of the way it abstracts away the closing of files.  E.G.::

    from fileinput import input
    lines = input("tmpout", inplace=1)
    first = lines.next()
    from sys import stdout
    stdout.writelines(lines)

Both of the above pieces of code cause Segmentation Faults.

It looks like in line 1779 of ``Objects/fileobject.c``, ``f->fp`` is being passed to ``fwrite`` as 0.  I guess this happens because no check is done after the call to ``PyIter_Next`` on line 1730 to see if the file is still open.

I don't see this as a big issue, though it is annoying that it seemingly prevents generators from being used with the `fileinput` module.  Doing so would be a bit awkward and hacky anyway though.

----------
components: IO
messages: 118877
nosy: intuited
priority: normal
severity: normal
status: open
title: Closing a file within its writelines method causes segfault
type: crash
versions: Python 2.6

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue10125>
_______________________________________


More information about the New-bugs-announce mailing list