]> granicus.if.org Git - python/commitdiff
Issue #25583: Avoid incorrect errors raised by os.makedirs(exist_ok=True)
authorMartin Panter <vadmium+py@gmail.com>
Thu, 19 Nov 2015 04:48:44 +0000 (04:48 +0000)
committerMartin Panter <vadmium+py@gmail.com>
Thu, 19 Nov 2015 04:48:44 +0000 (04:48 +0000)
Lib/os.py
Lib/test/test_os.py
Misc/NEWS

index a8f6a0b8db93cf20c611c407a7c4de750a112032..27b241ae97d50bf05bc7c067d8439019365ca246 100644 (file)
--- a/Lib/os.py
+++ b/Lib/os.py
@@ -226,7 +226,7 @@ def makedirs(name, mode=0o777, exist_ok=False):
         try:
             makedirs(head, mode, exist_ok)
         except FileExistsError:
-            # be happy if someone already created the path
+            # Defeats race condition when another thread created the path
             pass
         cdir = curdir
         if isinstance(tail, bytes):
@@ -235,8 +235,10 @@ def makedirs(name, mode=0o777, exist_ok=False):
             return
     try:
         mkdir(name, mode)
-    except OSError as e:
-        if not exist_ok or e.errno != errno.EEXIST or not path.isdir(name):
+    except OSError:
+        # 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
 
 def removedirs(name):
index 1f7e49c637798942f2496a2ec58f8bbfa41d5fc5..e29b0d585c723004635d1d02d6669e587fd6ec66 100644 (file)
@@ -971,6 +971,9 @@ class MakedirTests(unittest.TestCase):
         os.makedirs(path, mode=mode, exist_ok=True)
         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')
     def test_chown_uid_gid_arguments_must_be_index(self):
         stat = os.stat(support.TESTFN)
index e98e4a648885709838da5cedd3abab9de1746f39..f82696ac481462fbc2afe8f1385f7058d84ad536 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -106,6 +106,9 @@ Core and Builtins
 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 #6973: When we know a subprocess.Popen process has died, do