diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2013-02-12 22:38:48 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2013-02-12 22:38:48 (GMT) |
commit | 94cd10fa19d7174ce07c4680a1be7a94cb29dd8b (patch) | |
tree | 0aa07bbd57164c489e3f3a719705424a8166e056 | |
parent | 38e2a2afbd627187790f89baa7673c4bbcaf8779 (diff) | |
parent | ff7fef9601d15cdf274b4b984c7638f33002e879 (diff) | |
download | cpython-94cd10fa19d7174ce07c4680a1be7a94cb29dd8b.zip cpython-94cd10fa19d7174ce07c4680a1be7a94cb29dd8b.tar.gz cpython-94cd10fa19d7174ce07c4680a1be7a94cb29dd8b.tar.bz2 |
Issue #16800: tempfile.gettempdir() no longer left temporary files when
the disk is full. Original patch by Amir Szekely.
-rw-r--r-- | Lib/tempfile.py | 13 | ||||
-rw-r--r-- | Lib/test/test_tempfile.py | 40 | ||||
-rw-r--r-- | Misc/ACKS | 1 | ||||
-rw-r--r-- | Misc/NEWS | 3 |
4 files changed, 51 insertions, 6 deletions
diff --git a/Lib/tempfile.py b/Lib/tempfile.py index 908533f..21c7186 100644 --- a/Lib/tempfile.py +++ b/Lib/tempfile.py @@ -174,11 +174,14 @@ def _get_default_tempdir(): filename = _os.path.join(dir, name) try: fd = _os.open(filename, _bin_openflags, 0o600) - fp = _io.open(fd, 'wb') - fp.write(b'blat') - fp.close() - _os.unlink(filename) - del fp, fd + try: + try: + fp = _io.open(fd, 'wb', buffering=0, closefd=False) + fp.write(b'blat') + finally: + _os.close(fd) + finally: + _os.unlink(filename) return dir except FileExistsError: pass diff --git a/Lib/test/test_tempfile.py b/Lib/test/test_tempfile.py index c8d4b49..437b02b 100644 --- a/Lib/test/test_tempfile.py +++ b/Lib/test/test_tempfile.py @@ -1,6 +1,7 @@ # tempfile.py unit tests. import tempfile import errno +import io import os import signal import sys @@ -198,7 +199,44 @@ class TestCandidateTempdirList(BaseTestCase): # paths in this list. -# We test _get_default_tempdir by testing gettempdir. +# We test _get_default_tempdir some more by testing gettempdir. + +class TestGetDefaultTempdir(BaseTestCase): + """Test _get_default_tempdir().""" + + def test_no_files_left_behind(self): + # use a private empty directory + with tempfile.TemporaryDirectory() as our_temp_directory: + # force _get_default_tempdir() to consider our empty directory + def our_candidate_list(): + return [our_temp_directory] + + with support.swap_attr(tempfile, "_candidate_tempdir_list", + our_candidate_list): + # verify our directory is empty after _get_default_tempdir() + tempfile._get_default_tempdir() + self.assertEqual(os.listdir(our_temp_directory), []) + + def raise_OSError(*args, **kwargs): + raise OSError() + + with support.swap_attr(io, "open", raise_OSError): + # test again with failing io.open() + with self.assertRaises(FileNotFoundError): + tempfile._get_default_tempdir() + self.assertEqual(os.listdir(our_temp_directory), []) + + open = io.open + def bad_writer(*args, **kwargs): + fp = open(*args, **kwargs) + fp.write = raise_OSError + return fp + + with support.swap_attr(io, "open", bad_writer): + # test again with failing write() + with self.assertRaises(FileNotFoundError): + tempfile._get_default_tempdir() + self.assertEqual(os.listdir(our_temp_directory), []) class TestGetCandidateNames(BaseTestCase): @@ -1175,6 +1175,7 @@ Andrew Svetlov Paul Swartz Thenault Sylvain Péter Szabó +Amir Szekely Arfrever Frehtes Taifersar Arahesis Neil Tallim Geoff Talvola @@ -250,6 +250,9 @@ Core and Builtins Library ------- +- Issue #16800: tempfile.gettempdir() no longer left temporary files when + the disk is full. Original patch by Amir Szekely. + - Issue #16564: Fixed regression relative to Python2 in the operation of email.encoders.encode_7or8bit when used with binary data. |