[Python-checkins] cpython (merge 3.4 -> 3.5): Issue #25583: Merge makedirs fix from 3.4 into 3.5

martin.panter python-checkins at python.org
Thu Nov 19 21:39:33 EST 2015


https://hg.python.org/cpython/rev/515f76bf1254
changeset:   99218:515f76bf1254
branch:      3.5
parent:      99215:158cc5701488
parent:      99217:05d6ddf2b7c2
user:        Martin Panter <vadmium+py at gmail.com>
date:        Fri Nov 20 02:35:46 2015 +0000
summary:
  Issue #25583: Merge makedirs fix from 3.4 into 3.5

files:
  Lib/os.py           |  8 +++++---
  Lib/test/test_os.py |  3 +++
  Misc/NEWS           |  3 +++
  3 files changed, 11 insertions(+), 3 deletions(-)


diff --git a/Lib/os.py b/Lib/os.py
--- a/Lib/os.py
+++ b/Lib/os.py
@@ -230,7 +230,7 @@
         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):
@@ -239,8 +239,10 @@
             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):
diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py
--- a/Lib/test/test_os.py
+++ b/Lib/test/test_os.py
@@ -1040,6 +1040,9 @@
         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)
+
     def test_exist_ok_s_isgid_directory(self):
         path = os.path.join(support.TESTFN, 'dir1')
         S_ISGID = stat.S_ISGID
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -77,6 +77,9 @@
 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

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list