changeset: 95061:c84a0b35999a parent: 95059:e18062a98a33 parent: 95060:7fa741fe9425 user: Serhiy Storchaka date: Thu Mar 19 15:24:27 2015 +0200 files: Lib/tempfile.py Misc/NEWS description: Issue #23700: Iterator of NamedTemporaryFile now keeps a reference to NamedTemporaryFile instance. Patch by Bohuslav Kabrda. diff -r e18062a98a33 -r c84a0b35999a Lib/tempfile.py --- a/Lib/tempfile.py Wed Mar 18 21:36:23 2015 -0500 +++ b/Lib/tempfile.py Thu Mar 19 15:24:27 2015 +0200 @@ -426,7 +426,9 @@ # iter() doesn't use __getattr__ to find the __iter__ method def __iter__(self): - return iter(self.file) + # don't return iter(self.file), but yield from it to avoid closing + # file as long as it's being used as iterator, see issue #23000 + yield from iter(self.file) def NamedTemporaryFile(mode='w+b', buffering=-1, encoding=None, diff -r e18062a98a33 -r c84a0b35999a Lib/test/test_tempfile.py --- a/Lib/test/test_tempfile.py Wed Mar 18 21:36:23 2015 -0500 +++ b/Lib/test/test_tempfile.py Thu Mar 19 15:24:27 2015 +0200 @@ -707,6 +707,19 @@ # No reference cycle was created. self.assertIsNone(wr()) + def test_iter(self): + # Issue #23700: 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 f + for i, l in enumerate(make_file()): + self.assertEqual(l, lines[i]) + self.assertEqual(i, len(lines) - 1) + def test_creates_named(self): # NamedTemporaryFile creates files with names f = tempfile.NamedTemporaryFile() diff -r e18062a98a33 -r c84a0b35999a Misc/NEWS --- a/Misc/NEWS Wed Mar 18 21:36:23 2015 -0500 +++ b/Misc/NEWS Thu Mar 19 15:24:27 2015 +0200 @@ -18,6 +18,9 @@ Library ------- +- Issue #23700: Iterator of NamedTemporaryFile now keeps a reference to + NamedTemporaryFile instance. Patch by Bohuslav Kabrda. + - Issue #22903: The fake test case created by unittest.loader when it fails importing a test module is now picklable.