Skip to content

bpo-25803: Avoid incorrect errors raised by Path.mkdir(exist_ok=True)… #806

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 15 additions & 17 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1220,25 +1220,23 @@ def touch(self, mode=0o666, exist_ok=True):
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):
"""
Expand Down
5 changes: 5 additions & 0 deletions Lib/test/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1776,6 +1776,11 @@ def test_mkdir_exist_ok_with_parent(self):
self.assertTrue(p.exists())
self.assertEqual(p.stat().st_ctime, st_ctime_first)

def test_mkdir_exist_ok_root(self):
# Issue #25803: A drive root could raise PermissionError on Windows
self.cls('/').resolve().mkdir(exist_ok=True)
self.cls('/').resolve().mkdir(parents=True, exist_ok=True)

@only_nt # XXX: not sure how to test this on POSIX
def test_mkdir_with_unknown_drive(self):
for d in 'ZYXWVUTSRQPONMLKJIHGFEDCBA':
Expand Down
3 changes: 3 additions & 0 deletions Misc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ Core and Builtins
Library
-------

- bpo-25803: Avoid incorrect errors raised by Path.mkdir(exist_ok=True)
when the OS gives priority to errors such as EACCES over EEXIST.

- bpo-29861: Release references to tasks, their arguments and their results
as soon as they are finished in multiprocessing.Pool.

Expand Down