[Python-checkins] cpython: Issue #19921: When Path.mkdir() is called with parents=True, any missing parent

antoine.pitrou python-checkins at python.org
Mon Dec 16 20:22:44 CET 2013


http://hg.python.org/cpython/rev/87b81b7df7f0
changeset:   88004:87b81b7df7f0
user:        Antoine Pitrou <solipsis at pitrou.net>
date:        Mon Dec 16 20:22:37 2013 +0100
summary:
  Issue #19921: When Path.mkdir() is called with parents=True, any missing parent is created with the default permissions, ignoring the mode argument (mimicking the POSIX "mkdir -p" command).

Patch by Serhiy.

files:
  Doc/library/pathlib.rst  |   5 ++++-
  Lib/pathlib.py           |   2 +-
  Lib/test/test_pathlib.py |  13 +++++++++++--
  Misc/NEWS                |   4 ++++
  4 files changed, 20 insertions(+), 4 deletions(-)


diff --git a/Doc/library/pathlib.rst b/Doc/library/pathlib.rst
--- a/Doc/library/pathlib.rst
+++ b/Doc/library/pathlib.rst
@@ -768,7 +768,10 @@
    and access flags.  If the path already exists, :exc:`OSError` is raised.
 
    If *parents* is true, any missing parents of this path are created
-   as needed.  If *parents* is false (the default), a missing parent raises
+   as needed; they are created with the default permissions without taking
+   *mode* into account (mimicking the POSIX ``mkdir -p`` command).
+
+   If *parents* is false (the default), a missing parent raises
    :exc:`OSError`.
 
 
diff --git a/Lib/pathlib.py b/Lib/pathlib.py
--- a/Lib/pathlib.py
+++ b/Lib/pathlib.py
@@ -1101,7 +1101,7 @@
             except OSError as e:
                 if e.errno != ENOENT:
                     raise
-                self.parent.mkdir(mode, True)
+                self.parent.mkdir(parents=True)
                 self._accessor.mkdir(self, mode)
 
     def chmod(self, mode):
diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py
--- a/Lib/test/test_pathlib.py
+++ b/Lib/test/test_pathlib.py
@@ -1478,7 +1478,6 @@
         with self.assertRaises(OSError) as cm:
             p.mkdir()
         self.assertEqual(cm.exception.errno, errno.EEXIST)
-        # XXX test `mode` arg
 
     def test_mkdir_parents(self):
         # Creating a chain of directories
@@ -1493,7 +1492,17 @@
         with self.assertRaises(OSError) as cm:
             p.mkdir(parents=True)
         self.assertEqual(cm.exception.errno, errno.EEXIST)
-        # XXX test `mode` arg
+        # test `mode` arg
+        mode = stat.S_IMODE(p.stat().st_mode) # default mode
+        p = self.cls(BASE, 'newdirD', 'newdirE')
+        p.mkdir(0o555, parents=True)
+        self.assertTrue(p.exists())
+        self.assertTrue(p.is_dir())
+        if os.name != 'nt':
+            # the directory's permissions follow the mode argument
+            self.assertEqual(stat.S_IMODE(p.stat().st_mode), 0o555 & mode)
+        # the parent's permissions follow the default process settings
+        self.assertEqual(stat.S_IMODE(p.parent.stat().st_mode), mode)
 
     @with_symlinks
     def test_symlink_to(self):
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -44,6 +44,10 @@
 Library
 -------
 
+- Issue #19921: When Path.mkdir() is called with parents=True, any missing
+  parent is created with the default permissions, ignoring the mode argument
+  (mimicking the POSIX "mkdir -p" command).
+
 - Issue #19887: Improve the Path.resolve() algorithm to support certain
   symlink chains.
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list