diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2017-03-24 19:46:46 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-03-24 19:46:46 (GMT) |
commit | 1fc1f8d7f737f80a1ee5ea37b9781c0a790102b2 (patch) | |
tree | 1462ff3b86c04f43c51c8fb4c777db77373f41c4 /Lib/pathlib.py | |
parent | 80cb6ed4db9bae09de1e9ad6001d11cb45a4c1cc (diff) | |
download | cpython-1fc1f8d7f737f80a1ee5ea37b9781c0a790102b2.zip cpython-1fc1f8d7f737f80a1ee5ea37b9781c0a790102b2.tar.gz cpython-1fc1f8d7f737f80a1ee5ea37b9781c0a790102b2.tar.bz2 |
bpo-25803: Avoid incorrect errors raised by Path.mkdir(exist_ok=True) (#805) (#807)
when the OS gives priority to errors such as EACCES over EEXIST.
(cherry picked from commit af7b9ec5c855366feef4c67dc492d64b3baf84ca)
Diffstat (limited to 'Lib/pathlib.py')
-rw-r--r-- | Lib/pathlib.py | 32 |
1 files changed, 15 insertions, 17 deletions
diff --git a/Lib/pathlib.py b/Lib/pathlib.py index c0d858e..16b99cd 100644 --- a/Lib/pathlib.py +++ b/Lib/pathlib.py @@ -1207,25 +1207,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): """ |