mirror of https://github.com/python/cpython
Issue #16800: tempfile.gettempdir() no longer left temporary files when
the disk is full. Original patch by Amir Szekely.
This commit is contained in:
parent
e4ad8aacd1
commit
f6b361ec1a
|
@ -175,11 +175,14 @@ def _get_default_tempdir():
|
||||||
filename = _os.path.join(dir, name)
|
filename = _os.path.join(dir, name)
|
||||||
try:
|
try:
|
||||||
fd = _os.open(filename, _bin_openflags, 0o600)
|
fd = _os.open(filename, _bin_openflags, 0o600)
|
||||||
fp = _io.open(fd, 'wb')
|
try:
|
||||||
|
try:
|
||||||
|
fp = _io.open(fd, 'wb', buffering=0, closefd=False)
|
||||||
fp.write(b'blat')
|
fp.write(b'blat')
|
||||||
fp.close()
|
finally:
|
||||||
|
_os.close(fd)
|
||||||
|
finally:
|
||||||
_os.unlink(filename)
|
_os.unlink(filename)
|
||||||
del fp, fd
|
|
||||||
return dir
|
return dir
|
||||||
except (OSError, IOError) as e:
|
except (OSError, IOError) as e:
|
||||||
if e.args[0] != _errno.EEXIST:
|
if e.args[0] != _errno.EEXIST:
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
# tempfile.py unit tests.
|
# tempfile.py unit tests.
|
||||||
import tempfile
|
import tempfile
|
||||||
|
import errno
|
||||||
|
import io
|
||||||
import os
|
import os
|
||||||
import signal
|
import signal
|
||||||
import sys
|
import sys
|
||||||
|
@ -211,8 +213,48 @@ class test__candidate_tempdir_list(TC):
|
||||||
|
|
||||||
test_classes.append(test__candidate_tempdir_list)
|
test_classes.append(test__candidate_tempdir_list)
|
||||||
|
|
||||||
|
# We test _get_default_tempdir some more by testing gettempdir.
|
||||||
|
|
||||||
# We test _get_default_tempdir by testing gettempdir.
|
class TestGetDefaultTempdir(TC):
|
||||||
|
"""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(-1)
|
||||||
|
|
||||||
|
with support.swap_attr(io, "open", raise_OSError):
|
||||||
|
# test again with failing io.open()
|
||||||
|
with self.assertRaises(IOError) as cm:
|
||||||
|
tempfile._get_default_tempdir()
|
||||||
|
self.assertEqual(cm.exception.args[0], errno.ENOENT)
|
||||||
|
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(IOError) as cm:
|
||||||
|
tempfile._get_default_tempdir()
|
||||||
|
self.assertEqual(cm.exception.errno, errno.ENOENT)
|
||||||
|
self.assertEqual(os.listdir(our_temp_directory), [])
|
||||||
|
|
||||||
|
test_classes.append(TestGetDefaultTempdir)
|
||||||
|
|
||||||
|
|
||||||
class test__get_candidate_names(TC):
|
class test__get_candidate_names(TC):
|
||||||
|
|
|
@ -1051,6 +1051,7 @@ Andrew Svetlov
|
||||||
Paul Swartz
|
Paul Swartz
|
||||||
Thenault Sylvain
|
Thenault Sylvain
|
||||||
Péter Szabó
|
Péter Szabó
|
||||||
|
Amir Szekely
|
||||||
Arfrever Frehtes Taifersar Arahesis
|
Arfrever Frehtes Taifersar Arahesis
|
||||||
Neil Tallim
|
Neil Tallim
|
||||||
Geoff Talvola
|
Geoff Talvola
|
||||||
|
|
|
@ -221,6 +221,9 @@ Core and Builtins
|
||||||
Library
|
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
|
- Issue #16564: Fixed regression relative to Python2 in the operation of
|
||||||
email.encoders.encode_7or8bit when used with binary data.
|
email.encoders.encode_7or8bit when used with binary data.
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue