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.
This commit is contained in:
parent
43c6ef1899
commit
f315df31bd
|
@ -219,6 +219,13 @@ def _mkstemp_inner(dir, pre, suf, flags):
|
||||||
return (fd, _os.path.abspath(file))
|
return (fd, _os.path.abspath(file))
|
||||||
except FileExistsError:
|
except FileExistsError:
|
||||||
continue # try again
|
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,
|
raise FileExistsError(_errno.EEXIST,
|
||||||
"No usable temporary file name found")
|
"No usable temporary file name found")
|
||||||
|
|
|
@ -372,6 +372,32 @@ class TestMkstempInner(BaseTestCase):
|
||||||
os.lseek(f.fd, 0, os.SEEK_SET)
|
os.lseek(f.fd, 0, os.SEEK_SET)
|
||||||
self.assertEqual(os.read(f.fd, 20), b"blat")
|
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):
|
class TestGetTempPrefix(BaseTestCase):
|
||||||
"""Test gettempprefix()."""
|
"""Test gettempprefix()."""
|
||||||
|
|
|
@ -1121,6 +1121,7 @@ Daniel Shahaf
|
||||||
Ha Shao
|
Ha Shao
|
||||||
Mark Shannon
|
Mark Shannon
|
||||||
Richard Shapiro
|
Richard Shapiro
|
||||||
|
Vlad Shcherbina
|
||||||
Justin Sheehy
|
Justin Sheehy
|
||||||
Charlie Shepherd
|
Charlie Shepherd
|
||||||
Bruce Sherwood
|
Bruce Sherwood
|
||||||
|
|
|
@ -280,6 +280,10 @@ Library
|
||||||
- Issue #18113: Fixed a refcount leak in the curses.panel module's
|
- Issue #18113: Fixed a refcount leak in the curses.panel module's
|
||||||
set_userptr() method. Reported by Atsuo Ishimoto.
|
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
|
C API
|
||||||
-----
|
-----
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue