bpo-25803: Avoid incorrect errors raised by Path.mkdir(exist_ok=True)… · python/cpython@af7b9ec
@@ -1220,25 +1220,23 @@ def touch(self, mode=0o666, exist_ok=True):
12201220os.close(fd)
1221122112221222def mkdir(self, mode=0o777, parents=False, exist_ok=False):
1223+"""
1224+ Create a new directory at this given path.
1225+ """
12231226if self._closed:
12241227self._raise_closed()
1225-if not parents:
1226-try:
1227-self._accessor.mkdir(self, mode)
1228-except FileExistsError:
1229-if not exist_ok or not self.is_dir():
1230-raise
1231-else:
1232-try:
1233-self._accessor.mkdir(self, mode)
1234-except FileExistsError:
1235-if not exist_ok or not self.is_dir():
1236-raise
1237-except OSError as e:
1238-if e.errno != ENOENT or self.parent == self:
1239-raise
1240-self.parent.mkdir(parents=True)
1241-self._accessor.mkdir(self, mode)
1228+try:
1229+self._accessor.mkdir(self, mode)
1230+except FileNotFoundError:
1231+if not parents or self.parent == self:
1232+raise
1233+self.parent.mkdir(parents=True)
1234+self._accessor.mkdir(self, mode)
1235+except OSError:
1236+# Cannot rely on checking for EEXIST, since the operating system
1237+# could give priority to other errors like EACCES or EROFS
1238+if not exist_ok or not self.is_dir():
1239+raise
1242124012431241def chmod(self, mode):
12441242"""