changeset: 85571:e0037f266d45 branch: 2.7 parent: 85566:4d45f1ed1179 user: Eli Bendersky date: Fri Sep 06 06:17:15 2013 -0700 files: Lib/tempfile.py Lib/test/test_tempfile.py Misc/ACKS Misc/NEWS description: Close #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. diff -r 4d45f1ed1179 -r e0037f266d45 Lib/tempfile.py --- a/Lib/tempfile.py Fri Sep 06 10:24:08 2013 +0100 +++ b/Lib/tempfile.py Fri Sep 06 06:17:15 2013 -0700 @@ -242,6 +242,10 @@ 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") diff -r 4d45f1ed1179 -r e0037f266d45 Lib/test/test_tempfile.py --- a/Lib/test/test_tempfile.py Fri Sep 06 10:24:08 2013 +0100 +++ b/Lib/test/test_tempfile.py Fri Sep 06 06:17:15 2013 -0700 @@ -386,6 +386,32 @@ 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) diff -r 4d45f1ed1179 -r e0037f266d45 Misc/ACKS --- a/Misc/ACKS Fri Sep 06 10:24:08 2013 +0100 +++ b/Misc/ACKS Fri Sep 06 06:17:15 2013 -0700 @@ -936,6 +936,7 @@ Ha Shao Mark Shannon Richard Shapiro +Vlad Shcherbina Justin Sheehy Charlie Shepherd Bruce Sherwood diff -r 4d45f1ed1179 -r e0037f266d45 Misc/NEWS --- a/Misc/NEWS Fri Sep 06 10:24:08 2013 +0100 +++ b/Misc/NEWS Fri Sep 06 06:17:15 2013 -0700 @@ -156,6 +156,11 @@ - 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 -----------