]> granicus.if.org Git - python/commitdiff
Close #18849: Fixed a Windows-specific tempfile bug where collision with an
authorEli Bendersky <eliben@gmail.com>
Fri, 6 Sep 2013 13:17:15 +0000 (06:17 -0700)
committerEli Bendersky <eliben@gmail.com>
Fri, 6 Sep 2013 13:17:15 +0000 (06:17 -0700)
existing directory caused mkstemp and related APIs to fail instead of
retrying. Report and fix by Vlad Shcherbina.

Lib/tempfile.py
Lib/test/test_tempfile.py
Misc/ACKS
Misc/NEWS

index b5dce974e18776fe4936d287221d5aff0f0f8cdf..7154d2c6b52141fbeb7d74e53433d57b738b29a4 100644 (file)
@@ -242,6 +242,10 @@ def _mkstemp_inner(dir, pre, suf, flags):
         except OSError, e:
             if e.errno == _errno.EEXIST:
                 continue # try again
+            if _os.name == 'nt' and e.errno == _errno.EACCES:
+                # On windows, when a directory with the chosen name already
+                # exists, EACCES error code is returned instead of EEXIST.
+                continue
             raise
 
     raise IOError, (_errno.EEXIST, "No usable temporary file name found")
index b4d23addf2bb39db86bbe8e130efb0ed11445fd8..eb3e2b9a6dd233199e40ae951466f0098580df73 100644 (file)
@@ -386,6 +386,32 @@ class test__mkstemp_inner(TC):
         self.do_create(bin=0).write("blat\n")
         # XXX should test that the file really is a text file
 
+    def test_collision_with_existing_directory(self):
+        # _mkstemp_inner tries another name when a directory with
+        # the chosen name already exists
+        container_dir = tempfile.mkdtemp()
+        try:
+            def mock_get_candidate_names():
+                return iter(['aaa', 'aaa', 'bbb'])
+            with support.swap_attr(tempfile,
+                                   '_get_candidate_names',
+                                   mock_get_candidate_names):
+                dir = tempfile.mkdtemp(dir=container_dir)
+                self.assertTrue(dir.endswith('aaa'))
+
+                flags = tempfile._bin_openflags
+                (fd, name) = tempfile._mkstemp_inner(container_dir,
+                                                     tempfile.template,
+                                                     '',
+                                                     flags)
+                try:
+                    self.assertTrue(name.endswith('bbb'))
+                finally:
+                    os.close(fd)
+                    os.unlink(name)
+        finally:
+            support.rmtree(container_dir)
+
 test_classes.append(test__mkstemp_inner)
 
 
index 8d29290a709464ea925bf8180cf8c1c2d78a2b78..e251596c988f479fb1e90924fb3fb4bf779ff78b 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -936,6 +936,7 @@ Aman Shah
 Ha Shao
 Mark Shannon
 Richard Shapiro
+Vlad Shcherbina
 Justin Sheehy
 Charlie Shepherd
 Bruce Sherwood
index 8c67ad1d3a1339ecc51c7287d7787ff8ef9631a7..53be4c14ffb1d4e10df82a686150676d5421ef79 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -156,6 +156,11 @@ Library
 - Issue #18113: Fixed a refcount leak in the curses.panel module's
   set_userptr() method.  Reported by Atsuo Ishimoto.
 
+- Issue #18849: Fixed a Windows-specific tempfile bug where collision with an
+  existing directory caused mkstemp and related APIs to fail instead of
+  retrying. Report and fix by Vlad Shcherbina.
+
+
 Tools/Demos
 -----------