cpython: 63f0ae6e218a (original) (raw)

Mercurial > cpython

changeset 96163:63f0ae6e218a 2.7

Issue #22107: tempfile.gettempdir() and tempfile.mkdtemp() now try again when a directory with the chosen name already exists on Windows as well as on Unix. tempfile.mkstemp() now fails early if parent directory is not valid (not exists or is a file) on Windows. [#22107]

Serhiy Storchaka storchaka@gmail.com
date Wed, 20 May 2015 00:10:56 +0300
parents 6969bac411fa
children 56e1d24806b3
files Lib/tempfile.py Lib/test/test_tempfile.py Misc/NEWS
diffstat 3 files changed, 64 insertions(+), 10 deletions(-)[+] [-] Lib/tempfile.py 19 Lib/test/test_tempfile.py 50 Misc/NEWS 5

line wrap: on

line diff

--- a/Lib/tempfile.py +++ b/Lib/tempfile.py @@ -205,9 +205,14 @@ def _get_default_tempdir(): _os.unlink(filename) return dir except (OSError, IOError) as e:

@@ -242,7 +247,8 @@ def _mkstemp_inner(dir, pre, suf, flags) except OSError, e: if e.errno == _errno.EEXIST: continue # try again

@@ -335,6 +341,11 @@ def mkdtemp(suffix="", prefix=template, except OSError, e: if e.errno == _errno.EEXIST: continue # try again

raise IOError, (_errno.EEXIST, "No usable temporary directory name found")

--- a/Lib/test/test_tempfile.py +++ b/Lib/test/test_tempfile.py @@ -287,7 +287,42 @@ def _mock_candidate_names(*names): lambda: iter(names)) -class test__mkstemp_inner(TC): +class TestBadTempdir: +

+

+

+ + +class test__mkstemp_inner(TestBadTempdir, TC): """Test the internal function _mkstemp_inner.""" class mkstemped: @@ -400,7 +435,7 @@ class test__mkstemp_inner(TC): self.do_create(bin=0).write("blat\n") # XXX should test that the file really is a text file

@@ -411,11 +446,11 @@ class test__mkstemp_inner(TC): # the chosen name already exists with _inside_empty_temp_dir(), [](#l2.58) _mock_candidate_names('aaa', 'aaa', 'bbb'):

@@ -427,7 +462,7 @@ class test__mkstemp_inner(TC): dir = tempfile.mkdtemp() self.assertTrue(dir.endswith('aaa'))

@@ -542,9 +577,12 @@ class test_mkstemp(TC): test_classes.append(test_mkstemp) -class test_mkdtemp(TC): +class test_mkdtemp(TestBadTempdir, TC): """Test mkdtemp()."""

+ def do_create(self, dir=None, pre="", suf=""): if dir is None: dir = tempfile.gettempdir()

--- a/Misc/NEWS +++ b/Misc/NEWS @@ -15,6 +15,11 @@ Core and Builtins Library ------- +- Issue #22107: tempfile.gettempdir() and tempfile.mkdtemp() now try again