changeset: 85569:7611e7244bdd branch: 3.3 parent: 85567:0577c9a82c0a user: Eli Bendersky date: Fri Sep 06 06:11:19 2013 -0700 files: Lib/tempfile.py Lib/test/test_tempfile.py Misc/ACKS Misc/NEWS description: 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. diff -r 0577c9a82c0a -r 7611e7244bdd Lib/tempfile.py --- a/Lib/tempfile.py Fri Sep 06 10:25:31 2013 +0100 +++ b/Lib/tempfile.py Fri Sep 06 06:11:19 2013 -0700 @@ -219,6 +219,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 -r 0577c9a82c0a -r 7611e7244bdd Lib/test/test_tempfile.py --- a/Lib/test/test_tempfile.py Fri Sep 06 10:25:31 2013 +0100 +++ b/Lib/test/test_tempfile.py Fri Sep 06 06:11:19 2013 -0700 @@ -372,6 +372,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 -r 0577c9a82c0a -r 7611e7244bdd Misc/ACKS --- a/Misc/ACKS Fri Sep 06 10:25:31 2013 +0100 +++ b/Misc/ACKS Fri Sep 06 06:11:19 2013 -0700 @@ -1121,6 +1121,7 @@ Ha Shao Mark Shannon Richard Shapiro +Vlad Shcherbina Justin Sheehy Charlie Shepherd Bruce Sherwood diff -r 0577c9a82c0a -r 7611e7244bdd Misc/NEWS --- a/Misc/NEWS Fri Sep 06 10:25:31 2013 +0100 +++ b/Misc/NEWS Fri Sep 06 06:11:19 2013 -0700 @@ -280,6 +280,10 @@ - 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 -----