diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2015-05-19 21:10:56 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2015-05-19 21:10:56 (GMT) |
commit | 9eb0c0e73ec53b8d0e2917c44bcae05a18b56f53 (patch) | |
tree | f00384f5dc4923fb518019163abef8d6de9526a5 /Lib/tempfile.py | |
parent | 49f2ccf83dfd9f111caddeb85cbd72d1fab84a92 (diff) | |
download | cpython-9eb0c0e73ec53b8d0e2917c44bcae05a18b56f53.zip cpython-9eb0c0e73ec53b8d0e2917c44bcae05a18b56f53.tar.gz cpython-9eb0c0e73ec53b8d0e2917c44bcae05a18b56f53.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.py | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/Lib/tempfile.py b/Lib/tempfile.py index fbda8eb..184dfc1 100644 --- a/Lib/tempfile.py +++ b/Lib/tempfile.py @@ -205,9 +205,14 @@ def _get_default_tempdir(): _os.unlink(filename) return dir except (OSError, IOError) as e: - if e.args[0] != _errno.EEXIST: - break # no point trying more names in this directory - pass + if e.args[0] == _errno.EEXIST: + continue + if (_os.name == 'nt' and e.args[0] == _errno.EACCES and + _os.path.isdir(dir) and _os.access(dir, _os.W_OK)): + # On windows, when a directory with the chosen name already + # exists, EACCES error code is returned instead of EEXIST. + continue + break # no point trying more names in this directory raise IOError, (_errno.ENOENT, ("No usable temporary directory found in %s" % dirlist)) @@ -242,7 +247,8 @@ def _mkstemp_inner(dir, pre, suf, flags): except OSError, e: if e.errno == _errno.EEXIST: continue # try again - if _os.name == 'nt' and e.errno == _errno.EACCES: + if (_os.name == 'nt' and e.errno == _errno.EACCES and + _os.path.isdir(dir) and _os.access(dir, _os.W_OK)): # On windows, when a directory with the chosen name already # exists, EACCES error code is returned instead of EEXIST. continue @@ -335,6 +341,11 @@ def mkdtemp(suffix="", prefix=template, dir=None): except OSError, e: if e.errno == _errno.EEXIST: continue # try again + if (_os.name == 'nt' and e.errno == _errno.EACCES and + _os.path.isdir(dir) and _os.access(dir, _os.W_OK)): + # 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 directory name found") |