Skip to content

Commit

Permalink
Issue #25583: Avoid incorrect errors raised by os.makedirs(exist_ok=T…
Browse files Browse the repository at this point in the history
…rue)
  • Loading branch information
vadmium committed Nov 19, 2015
1 parent 41f69f4 commit a82642f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
8 changes: 5 additions & 3 deletions Lib/os.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down
3 changes: 3 additions & 0 deletions Lib/test/test_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,9 @@ def test_exist_ok_existing_directory(self):
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)
Expand Down
3 changes: 3 additions & 0 deletions Misc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a82642f

Please sign in to comment.