Commit a82642f9 authored by Martin Panter's avatar Martin Panter

Issue #25583: Avoid incorrect errors raised by os.makedirs(exist_ok=True)

parent 41f69f4c
...@@ -226,7 +226,7 @@ def makedirs(name, mode=0o777, exist_ok=False): ...@@ -226,7 +226,7 @@ def makedirs(name, mode=0o777, exist_ok=False):
try: try:
makedirs(head, mode, exist_ok) makedirs(head, mode, exist_ok)
except FileExistsError: except FileExistsError:
# be happy if someone already created the path # Defeats race condition when another thread created the path
pass pass
cdir = curdir cdir = curdir
if isinstance(tail, bytes): if isinstance(tail, bytes):
...@@ -235,8 +235,10 @@ def makedirs(name, mode=0o777, exist_ok=False): ...@@ -235,8 +235,10 @@ def makedirs(name, mode=0o777, exist_ok=False):
return return
try: try:
mkdir(name, mode) mkdir(name, mode)
except OSError as e: except OSError:
if not exist_ok or e.errno != errno.EEXIST or not path.isdir(name): # Cannot rely on checking for EEXIST, since the operating system
# could give priority to other errors like EACCES or EROFS
if not exist_ok or not path.isdir(name):
raise raise
def removedirs(name): def removedirs(name):
......
...@@ -971,6 +971,9 @@ class MakedirTests(unittest.TestCase): ...@@ -971,6 +971,9 @@ class MakedirTests(unittest.TestCase):
os.makedirs(path, mode=mode, exist_ok=True) os.makedirs(path, mode=mode, exist_ok=True)
os.umask(old_mask) os.umask(old_mask)
# Issue #25583: A drive root could raise PermissionError on Windows
os.makedirs(os.path.abspath('/'), exist_ok=True)
@unittest.skipUnless(hasattr(os, 'chown'), 'test needs os.chown') @unittest.skipUnless(hasattr(os, 'chown'), 'test needs os.chown')
def test_chown_uid_gid_arguments_must_be_index(self): def test_chown_uid_gid_arguments_must_be_index(self):
stat = os.stat(support.TESTFN) stat = os.stat(support.TESTFN)
......
...@@ -106,6 +106,9 @@ Core and Builtins ...@@ -106,6 +106,9 @@ Core and Builtins
Library Library
------- -------
- Issue #25583: Avoid incorrect errors raised by os.makedirs(exist_ok=True)
when the OS gives priority to errors such as EACCES over EEXIST.
- Issue #25593: Change semantics of EventLoop.stop() in asyncio. - Issue #25593: Change semantics of EventLoop.stop() in asyncio.
- Issue #6973: When we know a subprocess.Popen process has died, do - Issue #6973: When we know a subprocess.Popen process has died, do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment