# HG changeset patch # Parent d1c11a78b43a7a56a7c68211306ca5964afdcadf Issue #25583: Avoid incorrect errors raised by os.makedirs(exist_ok=True) diff -r d1c11a78b43a Lib/os.py --- a/Lib/os.py Tue Nov 10 19:52:20 2015 +0200 +++ b/Lib/os.py Fri Nov 13 02:25:45 2015 +0000 @@ -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 -r d1c11a78b43a Lib/test/test_os.py --- a/Lib/test/test_os.py Tue Nov 10 19:52:20 2015 +0200 +++ b/Lib/test/test_os.py Fri Nov 13 02:25:45 2015 +0000 @@ -1039,6 +1039,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 -r d1c11a78b43a Misc/NEWS --- a/Misc/NEWS Tue Nov 10 19:52:20 2015 +0200 +++ b/Misc/NEWS Fri Nov 13 02:25:45 2015 +0000 @@ -79,6 +79,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 #25584: Added "escape" to the __all__ list in the glob module. - Issue #25584: Fixed recursive glob() with patterns starting with '\*\*'.