Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bf6816d

Browse files
committed
FIX: subtle race condition in cbook.mkdirs
1 parent 429d376 commit bf6816d

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

lib/matplotlib/cbook.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,18 +1012,26 @@ def mkdirs(newdir, mode=0o777):
10121012
> mkdir -p NEWDIR
10131013
> chmod MODE NEWDIR
10141014
"""
1015-
try:
1016-
if not os.path.exists(newdir):
1017-
parts = os.path.split(newdir)
1018-
for i in range(1, len(parts) + 1):
1019-
thispart = os.path.join(*parts[:i])
1020-
if not os.path.exists(thispart):
1021-
os.makedirs(thispart, mode)
1022-
1023-
except OSError as err:
1024-
# Reraise the error unless it's about an already existing directory
1025-
if err.errno != errno.EEXIST or not os.path.isdir(newdir):
1026-
raise
1015+
# this functionality is now in core python as of 3.2
1016+
if six.PY3:
1017+
os.makedirs(newdir, mode=mode, exist_ok=True)
1018+
return
1019+
1020+
1021+
def _make_leaf(newdir, mode):
1022+
if os.path.exists(newdir):
1023+
return
1024+
try:
1025+
os.makedirs(thispart, mode)
1026+
except OSError as err:
1027+
# Reraise the error unless it's about an already existing directory
1028+
if err.errno != errno.EEXIST or not os.path.isdir(newdir):
1029+
raise
1030+
1031+
parts = os.path.split(newdir)
1032+
for i in range(1, len(parts) + 1):
1033+
thispart = os.path.join(*parts[:i])
1034+
_make_leaf(thispart, mode)
10271035

10281036

10291037
class GetRealpathAndStat(object):

0 commit comments

Comments
 (0)