diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2013-02-12 22:37:29 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2013-02-12 22:37:29 (GMT) |
commit | ff7fef9601d15cdf274b4b984c7638f33002e879 (patch) | |
tree | 3daf809b893ec457ef45c321e8319814251f343a /Lib/test/test_tempfile.py | |
parent | b52d432a780fdfb861000c95eb2bbb594fda9900 (diff) | |
parent | f6b361ec1a10b93ff8b927b400ca1777429bb23a (diff) | |
download | cpython-ff7fef9601d15cdf274b4b984c7638f33002e879.zip cpython-ff7fef9601d15cdf274b4b984c7638f33002e879.tar.gz cpython-ff7fef9601d15cdf274b4b984c7638f33002e879.tar.bz2 |
Issue #16800: tempfile.gettempdir() no longer left temporary files when
the disk is full. Original patch by Amir Szekely.
Diffstat (limited to 'Lib/test/test_tempfile.py')
-rw-r--r-- | Lib/test/test_tempfile.py | 40 |
1 files changed, 39 insertions, 1 deletions
diff --git a/Lib/test/test_tempfile.py b/Lib/test/test_tempfile.py index b638d87..2962939 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): |