summaryrefslogtreecommitdiffstats
path: root/Lib/tempfile.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2015-05-19 21:14:00 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2015-05-19 21:14:00 (GMT)
commit492f0277933fd5714762a99ec99fd7f97d99866a (patch)
tree85451c0d2ed74f4b10266dd2d71205ffb5fc9a87 /Lib/tempfile.py
parent873e0df946e53af525b515df7773ff72884a8c3b (diff)
parent5d6b7b1cb7943255b8682ea3663ce2c0da500e96 (diff)
downloadcpython-492f0277933fd5714762a99ec99fd7f97d99866a.zip
cpython-492f0277933fd5714762a99ec99fd7f97d99866a.tar.gz
cpython-492f0277933fd5714762a99ec99fd7f97d99866a.tar.bz2
Issue #22107: tempfile.gettempdir() and tempfile.mkdtemp() now try again
when a directory with the chosen name already exists on Windows as well as on Unix. tempfile.mkstemp() now fails early if parent directory is not valid (not exists or is a file) on Windows.
Diffstat (limited to 'Lib/tempfile.py')
-rw-r--r--Lib/tempfile.py18
1 files changed, 17 insertions, 1 deletions
diff --git a/Lib/tempfile.py b/Lib/tempfile.py
index 81c289a..226c6e6 100644
--- a/Lib/tempfile.py
+++ b/Lib/tempfile.py
@@ -166,6 +166,13 @@ def _get_default_tempdir():
return dir
except FileExistsError:
pass
+ except PermissionError:
+ # This exception is thrown when a directory with the chosen name
+ # already exists on windows.
+ if (_os.name == 'nt' and _os.path.isdir(dir) and
+ _os.access(dir, _os.W_OK)):
+ continue
+ break # no point trying more names in this directory
except OSError:
break # no point trying more names in this directory
raise FileNotFoundError(_errno.ENOENT,
@@ -204,7 +211,8 @@ def _mkstemp_inner(dir, pre, suf, flags):
except PermissionError:
# This exception is thrown when a directory with the chosen name
# already exists on windows.
- if _os.name == 'nt':
+ if (_os.name == 'nt' and _os.path.isdir(dir) and
+ _os.access(dir, _os.W_OK)):
continue
else:
raise
@@ -296,6 +304,14 @@ def mkdtemp(suffix="", prefix=template, dir=None):
return 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' and _os.path.isdir(dir) and
+ _os.access(dir, _os.W_OK)):
+ continue
+ else:
+ raise
raise FileExistsError(_errno.EEXIST,
"No usable temporary directory name found")