[issue23700] tempfile.NamedTemporaryFile can close too early if used as iterator

Paul Moore report at bugs.python.org
Thu Mar 19 14:22:10 CET 2015


Paul Moore added the comment:

Agreed, the test is sufficient documentation. However, I can't make the test fail here (Windows 7, Python 3.4.3):

>py ti.py
b'spam\n' b'spam\n'
b'eggs\n' b'eggs\n'
b'beans\n' b'beans\n'
>cat ti.py
import tempfile

def test_iter():
    # getting iterator from a temporary file should keep it alive
    # as long as it's being iterated over
    lines = [b'spam\n', b'eggs\n', b'beans\n']
    def make_file():
        f = tempfile.NamedTemporaryFile(mode='w+b')
        f.write(b''.join(lines))
        f.seek(0)
        return iter(f)
    for i, l in enumerate(make_file()):
        print(l, lines[i])

test_iter()

Is it somehow OS-specific?

Regardless, the patch seems fine and I have no problem with it being applied.

----------
nosy: +paul.moore

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


More information about the Python-bugs-list mailing list