Skip to content

Commit a82642f

Browse files
committed
Issue #25583: Avoid incorrect errors raised by os.makedirs(exist_ok=True)
1 parent 41f69f4 commit a82642f

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

Lib/os.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ def makedirs(name, mode=0o777, exist_ok=False):
226226
try:
227227
makedirs(head, mode, exist_ok)
228228
except FileExistsError:
229-
# be happy if someone already created the path
229+
# Defeats race condition when another thread created the path
230230
pass
231231
cdir = curdir
232232
if isinstance(tail, bytes):
@@ -235,8 +235,10 @@ def makedirs(name, mode=0o777, exist_ok=False):
235235
return
236236
try:
237237
mkdir(name, mode)
238-
except OSError as e:
239-
if not exist_ok or e.errno != errno.EEXIST or not path.isdir(name):
238+
except OSError:
239+
# Cannot rely on checking for EEXIST, since the operating system
240+
# could give priority to other errors like EACCES or EROFS
241+
if not exist_ok or not path.isdir(name):
240242
raise
241243

242244
def removedirs(name):

Lib/test/test_os.py

+3
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,9 @@ def test_exist_ok_existing_directory(self):
971971
os.makedirs(path, mode=mode, exist_ok=True)
972972
os.umask(old_mask)
973973

974+
# Issue #25583: A drive root could raise PermissionError on Windows
975+
os.makedirs(os.path.abspath('/'), exist_ok=True)
976+
974977
@unittest.skipUnless(hasattr(os, 'chown'), 'test needs os.chown')
975978
def test_chown_uid_gid_arguments_must_be_index(self):
976979
stat = os.stat(support.TESTFN)

Misc/NEWS

+3
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ Core and Builtins
106106
Library
107107
-------
108108

109+
- Issue #25583: Avoid incorrect errors raised by os.makedirs(exist_ok=True)
110+
when the OS gives priority to errors such as EACCES over EEXIST.
111+
109112
- Issue #25593: Change semantics of EventLoop.stop() in asyncio.
110113

111114
- Issue #6973: When we know a subprocess.Popen process has died, do

0 commit comments

Comments
 (0)