Issue #18849: Fixed a Windows-specific tempfile bug where collision with an
authorEli Bendersky <eliben@gmail.com>
Fri, 6 Sep 2013 13:11:19 +0000 (06:11 -0700)
committerEli Bendersky <eliben@gmail.com>
Fri, 6 Sep 2013 13:11:19 +0000 (06:11 -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 8a165ed1c1dfd5686e5a361301dcce7e8f8e4aee..91332b61be72e605b1dd36deaef5a3c789329a21 100644 (file)
@@ -219,6 +219,13 @@ def _mkstemp_inner(dir, pre, suf, flags):
             return (fd, _os.path.abspath(file))
         except FileExistsError:
             continue    # try again
+        except PermissionError:
+            # This exception is thrown when a directory with the chosen name
+            # already exists on windows.
+            if _os.name == 'nt':
+                continue
+            else:
+                raise
 
     raise FileExistsError(_errno.EEXIST,
                           "No usable temporary file name found")
index 2962939ce0939d2e8b9a1682679be04b26052ea4..b56d64899ae7395763e8dd853de71ffb16083876 100644 (file)
@@ -372,6 +372,32 @@ class TestMkstempInner(BaseTestCase):
         os.lseek(f.fd, 0, os.SEEK_SET)
         self.assertEqual(os.read(f.fd, 20), b"blat")
 
+    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)
+
 
 class TestGetTempPrefix(BaseTestCase):
     """Test gettempprefix()."""
index 20eeafd03035b78e5d29ea42073ed6de41f7400e..db01d142a9e2622b75433d2e606a41040653ec70 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1121,6 +1121,7 @@ Daniel Shahaf
 Ha Shao
 Mark Shannon
 Richard Shapiro
+Vlad Shcherbina
 Justin Sheehy
 Charlie Shepherd
 Bruce Sherwood
index 8052c6b6b72e609e23b4d07e0a834cbd5ee55d94..1c0f3b23b6a4c62268a967091f39c666cb8cc5cb 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -280,6 +280,10 @@ 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.
+
 C API
 -----