[Python-checkins] cpython (merge 3.3 -> default): Issue #18849: Fixed a Windows-specific tempfile bug where collision with an

eli.bendersky python-checkins at python.org
Fri Sep 6 15:14:58 CEST 2013


http://hg.python.org/cpython/rev/035b61b52caa
changeset:   85570:035b61b52caa
parent:      85568:7627fea85a6d
parent:      85569:7611e7244bdd
user:        Eli Bendersky <eliben at gmail.com>
date:        Fri Sep 06 06:14:16 2013 -0700
summary:
  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.

files:
  Lib/tempfile.py           |   7 +++++++
  Lib/test/test_tempfile.py |  26 ++++++++++++++++++++++++++
  Misc/ACKS                 |   1 +
  Misc/NEWS                 |   4 ++++
  4 files changed, 38 insertions(+), 0 deletions(-)


diff --git a/Lib/tempfile.py b/Lib/tempfile.py
--- a/Lib/tempfile.py
+++ b/Lib/tempfile.py
@@ -199,6 +199,13 @@
             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")
diff --git a/Lib/test/test_tempfile.py b/Lib/test/test_tempfile.py
--- a/Lib/test/test_tempfile.py
+++ b/Lib/test/test_tempfile.py
@@ -373,6 +373,32 @@
         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()."""
diff --git a/Misc/ACKS b/Misc/ACKS
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1157,6 +1157,7 @@
 Ha Shao
 Mark Shannon
 Richard Shapiro
+Vlad Shcherbina
 Justin Sheehy
 Charlie Shepherd
 Bruce Sherwood
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -189,6 +189,10 @@
 
 - Issue #8860: Fixed rounding in timedelta constructor.
 
+- 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.
+
 Tests
 -----
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list