summaryrefslogtreecommitdiff
path: root/Lib/pathlib.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2017-03-24 20:51:53 +0200
committerGitHub <noreply@github.com>2017-03-24 20:51:53 +0200
commitaf7b9ec5c855366feef4c67dc492d64b3baf84ca (patch)
tree48f0d7ad1aad75910e0a9f87876b5d9144fb55b5 /Lib/pathlib.py
parent8988945cdc27ffa86ba8c624e095b51c459f5154 (diff)
downloadcpython-git-af7b9ec5c855366feef4c67dc492d64b3baf84ca.tar.gz
bpo-25803: Avoid incorrect errors raised by Path.mkdir(exist_ok=True) (#805)
when the OS gives priority to errors such as EACCES over EEXIST.
Diffstat (limited to 'Lib/pathlib.py')
-rw-r--r--Lib/pathlib.py32
1 files changed, 15 insertions, 17 deletions
diff --git a/Lib/pathlib.py b/Lib/pathlib.py
index 9f347216b1..8c1cb96bad 100644
--- a/Lib/pathlib.py
+++ b/Lib/pathlib.py
@@ -1220,25 +1220,23 @@ class Path(PurePath):
os.close(fd)
def mkdir(self, mode=0o777, parents=False, exist_ok=False):
+ """
+ Create a new directory at this given path.
+ """
if self._closed:
self._raise_closed()
- if not parents:
- try:
- self._accessor.mkdir(self, mode)
- except FileExistsError:
- if not exist_ok or not self.is_dir():
- raise
- else:
- try:
- self._accessor.mkdir(self, mode)
- except FileExistsError:
- if not exist_ok or not self.is_dir():
- raise
- except OSError as e:
- if e.errno != ENOENT or self.parent == self:
- raise
- self.parent.mkdir(parents=True)
- self._accessor.mkdir(self, mode)
+ try:
+ self._accessor.mkdir(self, mode)
+ except FileNotFoundError:
+ if not parents or self.parent == self:
+ raise
+ self.parent.mkdir(parents=True)
+ self._accessor.mkdir(self, mode)
+ 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 self.is_dir():
+ raise
def chmod(self, mode):
"""