Skip to content
Open
12 changes: 11 additions & 1 deletion Doc/library/os.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2523,7 +2523,8 @@ features:
Windows now handles a *mode* of ``0o700``.


.. function:: makedirs(name, mode=0o777, exist_ok=False)
.. function:: makedirs(name, mode=0o777, exist_ok=False, *, \
parent_mode=None)

.. index::
single: directory; creating
Expand All @@ -2541,6 +2542,10 @@ features:
If *exist_ok* is ``False`` (the default), a :exc:`FileExistsError` is
raised if the target directory already exists.

If *parent_mode* is not ``None``, it will be used as the mode for any
newly-created, intermediate-level directories. Otherwise, intermediate
directories are created with the default permissions (respecting umask).

.. note::

:func:`makedirs` will become confused if the path elements to create
Expand All @@ -2567,6 +2572,11 @@ features:
The *mode* argument no longer affects the file permission bits of
newly created intermediate-level directories.

.. versionadded:: next
The *parent_mode* parameter. To match the behavior from Python 3.6 and
earlier (where *mode* was applied to all created directories), pass
``parent_mode=mode``.


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

Expand Down
11 changes: 10 additions & 1 deletion Doc/library/pathlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1492,7 +1492,8 @@ Creating files and directories
:meth:`~Path.write_bytes` methods are often used to create files.


.. method:: Path.mkdir(mode=0o777, parents=False, exist_ok=False)
.. method:: Path.mkdir(mode=0o777, parents=False, exist_ok=False, *, \
parent_mode=None)

Create a new directory at this given path. If *mode* is given, it is
combined with the process's ``umask`` value to determine the file mode
Expand All @@ -1503,6 +1504,11 @@ Creating files and directories
as needed; they are created with the default permissions without taking
*mode* into account (mimicking the POSIX ``mkdir -p`` command).

If *parent_mode* is not ``None``, it will be used as the mode for any
newly-created, intermediate-level directories when *parents* is true.
Otherwise, intermediate directories are created with the default
permissions (respecting umask).

If *parents* is false (the default), a missing parent raises
:exc:`FileNotFoundError`.

Expand All @@ -1516,6 +1522,9 @@ Creating files and directories
.. versionchanged:: 3.5
The *exist_ok* parameter was added.

.. versionadded:: next
The *parent_mode* parameter.


.. method:: Path.symlink_to(target, target_is_directory=False)

Expand Down
13 changes: 13 additions & 0 deletions Doc/whatsnew/3.15.rst
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,15 @@ math
(Contributed by Bénédikt Tran in :gh:`135853`.)


os
--

* :func:`os.makedirs` function now has a *parent_mode* parameter that allows
specifying the mode for intermediate directories. This can be used to match
the behavior from Python 3.6 and earlier by passing ``parent_mode=mode``.
(Contributed by Zackery Spytz and Gregory P. Smith in :gh:`86533`.)


os.path
-------

Expand Down Expand Up @@ -541,6 +550,10 @@ http.server
pathlib
-------

* :meth:`pathlib.Path.mkdir` now has a *parent_mode* parameter that allows
specifying the mode for intermediate directories when ``parents=True``.
(Contributed by Gregory P. Smith in :gh:`86533`.)

* Removed deprecated :meth:`!pathlib.PurePath.is_reserved`.
Use :func:`os.path.isreserved` to detect reserved paths on Windows.
(Contributed by Nikita Sobolev in :gh:`133875`.)
Expand Down
15 changes: 11 additions & 4 deletions Lib/os.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,22 +208,29 @@ def _add(str, fn):
# Super directory utilities.
# (Inspired by Eric Raymond; the doc strings are mostly his)

def makedirs(name, mode=0o777, exist_ok=False):
"""makedirs(name [, mode=0o777][, exist_ok=False])
def makedirs(name, mode=0o777, exist_ok=False, *, parent_mode=None):
"""makedirs(name [, mode=0o777][, exist_ok=False][, parent_mode=None])

Super-mkdir; create a leaf directory and all intermediate ones. Works like
mkdir, except that any intermediate path segment (not just the rightmost)
will be created if it does not exist. If the target directory already
exists, raise an OSError if exist_ok is False. Otherwise no exception is
raised. This is recursive.
raised. If parent_mode is not None, it will be used as the mode for any
newly-created, intermediate-level directories. Otherwise, intermediate
directories are created with the default permissions (respecting umask).
This is recursive.

"""
head, tail = path.split(name)
if not tail:
head, tail = path.split(head)
if head and tail and not path.exists(head):
try:
makedirs(head, exist_ok=exist_ok)
if parent_mode is not None:
makedirs(head, mode=parent_mode, exist_ok=exist_ok,
parent_mode=parent_mode)
else:
makedirs(head, exist_ok=exist_ok)
except FileExistsError:
# Defeats race condition when another thread created the path
pass
Expand Down
8 changes: 6 additions & 2 deletions Lib/pathlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -997,7 +997,7 @@ def touch(self, mode=0o666, exist_ok=True):
fd = os.open(self, flags, mode)
os.close(fd)

def mkdir(self, mode=0o777, parents=False, exist_ok=False):
def mkdir(self, mode=0o777, parents=False, exist_ok=False, *, parent_mode=None):
"""
Create a new directory at this given path.
"""
Expand All @@ -1006,7 +1006,11 @@ def mkdir(self, mode=0o777, parents=False, exist_ok=False):
except FileNotFoundError:
if not parents or self.parent == self:
raise
self.parent.mkdir(parents=True, exist_ok=True)
if parent_mode is not None:
self.parent.mkdir(mode=parent_mode, parents=True, exist_ok=True,
parent_mode=parent_mode)
else:
self.parent.mkdir(parents=True, exist_ok=True)
self.mkdir(mode, parents=False, exist_ok=exist_ok)
except OSError:
# Cannot rely on checking for EEXIST, since the operating system
Expand Down
65 changes: 60 additions & 5 deletions Lib/test/test_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -1921,17 +1921,72 @@ def test_makedir(self):
def test_mode(self):
# Note: in some cases, the umask might already be 2 in which case this
# will pass even if os.umask is actually broken.
parent = os.path.join(os_helper.TESTFN, 'dir1')
path = os.path.join(parent, 'dir2')
with os_helper.temp_umask(0o002):
base = os_helper.TESTFN
parent = os.path.join(base, 'dir1')
path = os.path.join(parent, 'dir2')
os.makedirs(path, 0o555)
os.makedirs(path, 0o705)
self.assertTrue(os.path.exists(path))
self.assertTrue(os.path.isdir(path))
if os.name != 'nt':
self.assertEqual(os.stat(path).st_mode & 0o777, 0o555)
# Leaf directory gets the specified mode
self.assertEqual(os.stat(path).st_mode & 0o777, 0o705)
# Parent directory uses default permissions (respecting umask)
self.assertEqual(os.stat(parent).st_mode & 0o777, 0o775)

@unittest.skipIf(
support.is_wasi,
"WASI's umask is a stub."
)
def test_mode_with_parent_mode(self):
# Test the parent_mode parameter
parent = os.path.join(os_helper.TESTFN, 'dir1')
path = os.path.join(parent, 'dir2')
with os_helper.temp_umask(0o002):
# Specify mode for both leaf and parent directories
os.makedirs(path, 0o770, parent_mode=0o750)
self.assertTrue(os.path.exists(path))
self.assertTrue(os.path.isdir(path))
if os.name != 'nt':
# Leaf directory gets the mode parameter
self.assertEqual(os.stat(path).st_mode & 0o777, 0o770)
# Parent directory gets the parent_mode parameter
self.assertEqual(os.stat(parent).st_mode & 0o777, 0o750)

@unittest.skipIf(
support.is_wasi,
"WASI's umask is a stub."
)
def test_parent_mode_deep_hierarchy(self):
# Test parent_mode with deep directory hierarchy
base = os.path.join(os_helper.TESTFN, 'dir1', 'dir2', 'dir3')
with os_helper.temp_umask(0o002):
os.makedirs(base, 0o755, parent_mode=0o700)
self.assertTrue(os.path.exists(base))
if os.name != 'nt':
# Check that all parent directories have parent_mode
level1 = os.path.join(os_helper.TESTFN, 'dir1')
level2 = os.path.join(level1, 'dir2')
self.assertEqual(os.stat(level1).st_mode & 0o777, 0o700)
self.assertEqual(os.stat(level2).st_mode & 0o777, 0o700)
# Leaf directory has the regular mode
self.assertEqual(os.stat(base).st_mode & 0o777, 0o755)

@unittest.skipIf(
support.is_wasi,
"WASI's umask is a stub."
)
def test_parent_mode_same_as_mode(self):
# Test emulating Python 3.6 behavior by setting parent_mode=mode
parent = os.path.join(os_helper.TESTFN, 'dir1')
path = os.path.join(parent, 'dir2')
with os_helper.temp_umask(0o002):
os.makedirs(path, 0o705, parent_mode=0o705)
self.assertTrue(os.path.exists(path))
if os.name != 'nt':
# Both directories should have the same mode
self.assertEqual(os.stat(path).st_mode & 0o777, 0o705)
self.assertEqual(os.stat(parent).st_mode & 0o777, 0o705)

@unittest.skipIf(
support.is_wasi,
"WASI's umask is a stub."
Expand Down
113 changes: 113 additions & 0 deletions Lib/test/test_pathlib/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -2489,6 +2489,119 @@ def my_mkdir(path, mode=0o777):
self.assertNotIn(str(p12), concurrently_created)
self.assertTrue(p.exists())

@unittest.skipIf(
is_emscripten or is_wasi,
"umask is not implemented on Emscripten/WASI."
)
@unittest.skipIf(
sys.platform == "android",
"Android filesystem may not honor requested permissions."
)
def test_mkdir_parents_umask(self):
# Test that parent directories respect umask when parent_mode is not set
p = self.cls(self.base, 'umasktest', 'child')
self.assertFalse(p.exists())
if os.name != 'nt':
old_mask = os.umask(0o002)
try:
p.mkdir(0o755, parents=True)
self.assertTrue(p.exists())
# Leaf directory gets the specified mode
self.assertEqual(p.stat().st_mode & 0o777, 0o755)
# Parent directory respects umask (0o777 & ~0o002 = 0o775)
self.assertEqual(p.parent.stat().st_mode & 0o777, 0o775)
finally:
os.umask(old_mask)

@unittest.skipIf(
is_emscripten or is_wasi,
"umask is not implemented on Emscripten/WASI."
)
@unittest.skipIf(
sys.platform == "android",
"Android filesystem may not honor requested permissions."
)
def test_mkdir_with_parent_mode(self):
# Test the parent_mode parameter
p = self.cls(self.base, 'newdirPM', 'subdirPM')
self.assertFalse(p.exists())
if os.name != 'nt':
# Specify different modes for parent and leaf directories
p.mkdir(0o755, parents=True, parent_mode=0o750)
self.assertTrue(p.exists())
self.assertTrue(p.is_dir())
# Leaf directory gets the mode parameter
self.assertEqual(p.stat().st_mode & 0o777, 0o755)
# Parent directory gets the parent_mode parameter
self.assertEqual(p.parent.stat().st_mode & 0o777, 0o750)

@unittest.skipIf(
is_emscripten or is_wasi,
"umask is not implemented on Emscripten/WASI."
)
@unittest.skipIf(
sys.platform == "android",
"Android filesystem may not honor requested permissions."
)
def test_mkdir_parent_mode_deep_hierarchy(self):
# Test parent_mode with deep directory hierarchy
p = self.cls(self.base, 'level1PM', 'level2PM', 'level3PM')
self.assertFalse(p.exists())
if os.name != 'nt':
p.mkdir(0o755, parents=True, parent_mode=0o700)
self.assertTrue(p.exists())
# Check that all parent directories have parent_mode
level1 = self.cls(self.base, 'level1PM')
level2 = level1 / 'level2PM'
self.assertEqual(level1.stat().st_mode & 0o777, 0o700)
self.assertEqual(level2.stat().st_mode & 0o777, 0o700)
# Leaf directory has the regular mode
self.assertEqual(p.stat().st_mode & 0o777, 0o755)

@unittest.skipIf(
is_emscripten or is_wasi,
"umask is not implemented on Emscripten/WASI."
)
@unittest.skipIf(
sys.platform == "android",
"Android filesystem may not honor requested permissions."
)
def test_mkdir_parent_mode_overrides_umask(self):
# Test that parent_mode overrides umask for parent directories
p = self.cls(self.base, 'overridetest', 'child')
self.assertFalse(p.exists())
if os.name != 'nt':
old_mask = os.umask(0o022) # Restrictive umask
try:
# parent_mode should override umask for parents
p.mkdir(0o755, parents=True, parent_mode=0o700)
self.assertTrue(p.exists())
# Leaf directory gets the specified mode
self.assertEqual(p.stat().st_mode & 0o777, 0o755)
# Parent directory gets parent_mode, not affected by umask
self.assertEqual(p.parent.stat().st_mode & 0o777, 0o700)
finally:
os.umask(old_mask)

@unittest.skipIf(
is_emscripten or is_wasi,
"umask is not implemented on Emscripten/WASI."
)
@unittest.skipIf(
sys.platform == "android",
"Android filesystem may not honor requested permissions."
)
def test_mkdir_parent_mode_same_as_mode(self):
# Test setting parent_mode same as mode
p = self.cls(self.base, 'samedirPM', 'subdirPM')
self.assertFalse(p.exists())
if os.name != 'nt':
p.mkdir(0o705, parents=True, parent_mode=0o705)
self.assertTrue(p.exists())
# Both directories should have the same mode
self.assertEqual(p.stat().st_mode & 0o777, 0o705)
self.assertEqual(p.parent.stat().st_mode & 0o777, 0o705)

@needs_symlinks
def test_symlink_to(self):
P = self.cls(self.base)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
The :func:`os.makedirs` function and :meth:`pathlib.Path.mkdir` method now have
a *parent_mode* parameter to specify the mode for intermediate directories when
creating parent directories. This allows one to match the behavior from Python
3.6 and earlier for :func:`os.makedirs`.
Loading