Skip to content

Commit e304e33

Browse files
bpo-19930: The mode argument of os.makedirs() no longer affects the file (#799)
permission bits of newly-created intermediate-level directories.
1 parent 5619ab2 commit e304e33

File tree

5 files changed

+32
-3
lines changed

5 files changed

+32
-3
lines changed

Doc/library/os.rst

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1741,8 +1741,11 @@ features:
17411741
Recursive directory creation function. Like :func:`mkdir`, but makes all
17421742
intermediate-level directories needed to contain the leaf directory.
17431743

1744-
The *mode* parameter is passed to :func:`mkdir`; see :ref:`the mkdir()
1745-
description <mkdir_modebits>` for how it is interpreted.
1744+
The *mode* parameter is passed to :func:`mkdir` for creating the leaf
1745+
directory; see :ref:`the mkdir() description <mkdir_modebits>` for how it
1746+
is interpreted. To set the file permission bits of any newly-created parent
1747+
directories you can set the umask before invoking :func:`makedirs`. The
1748+
file permission bits of existing parent directories are not changed.
17461749

17471750
If *exist_ok* is ``False`` (the default), an :exc:`OSError` is raised if the
17481751
target directory already exists.
@@ -1767,6 +1770,10 @@ features:
17671770
.. versionchanged:: 3.6
17681771
Accepts a :term:`path-like object`.
17691772

1773+
.. versionchanged:: 3.7
1774+
The *mode* argument no longer affects the file permission bits of
1775+
newly-created intermediate-level directories.
1776+
17701777

17711778
.. function:: mkfifo(path, mode=0o666, *, dir_fd=None)
17721779

Doc/whatsnew/3.7.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,13 @@ Changes in the Python API
247247
and module are affected by this change.
248248
(Contributed by INADA Naoki and Eugene Toder in :issue:`29463`.)
249249

250+
* The *mode* argument of :func:`os.makedirs` no longer affects the file
251+
permission bits of newly-created intermediate-level directories.
252+
To set their file permission bits you can set the umask before invoking
253+
``makedirs()``.
254+
(Contributed by Serhiy Storchaka in :issue:`19930`.)
255+
256+
250257
CPython bytecode changes
251258
------------------------
252259

Lib/os.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ def makedirs(name, mode=0o777, exist_ok=False):
207207
head, tail = path.split(head)
208208
if head and tail and not path.exists(head):
209209
try:
210-
makedirs(head, mode, exist_ok)
210+
makedirs(head, exist_ok=exist_ok)
211211
except FileExistsError:
212212
# Defeats race condition when another thread created the path
213213
pass

Lib/test/test_os.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,6 +1118,18 @@ def test_makedir(self):
11181118
'dir5', 'dir6')
11191119
os.makedirs(path)
11201120

1121+
def test_mode(self):
1122+
with support.temp_umask(0o002):
1123+
base = support.TESTFN
1124+
parent = os.path.join(base, 'dir1')
1125+
path = os.path.join(parent, 'dir2')
1126+
os.makedirs(path, 0o555)
1127+
self.assertTrue(os.path.exists(path))
1128+
self.assertTrue(os.path.isdir(path))
1129+
if os.name != 'nt':
1130+
self.assertEqual(stat.S_IMODE(os.stat(path).st_mode), 0o555)
1131+
self.assertEqual(stat.S_IMODE(os.stat(parent).st_mode), 0o775)
1132+
11211133
def test_exist_ok_existing_directory(self):
11221134
path = os.path.join(support.TESTFN, 'dir1')
11231135
mode = 0o777

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,9 @@ Extension Modules
287287
Library
288288
-------
289289

290+
- bpo-19930: The mode argument of os.makedirs() no longer affects the file
291+
permission bits of newly-created intermediate-level directories.
292+
290293
- bpo-29884: faulthandler: Restore the old sigaltstack during teardown.
291294
Patch by Christophe Zeitouny.
292295

0 commit comments

Comments
 (0)