Skip to content

gh-119993 ignore NotADirectoryError in Path.unlink() if missing_ok is True #120049

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

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 8 additions & 4 deletions Doc/library/pathlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1654,15 +1654,19 @@ Copying, moving and deleting
Remove this file or symbolic link. If the path points to a directory,
use :func:`Path.rmdir` instead.

If *missing_ok* is false (the default), :exc:`FileNotFoundError` is
raised if the path does not exist.
If *missing_ok* is false (the default), this method propagates any
:exc:`OSError` from the operating system.

If *missing_ok* is true, :exc:`FileNotFoundError` exceptions will be
ignored (same behavior as the POSIX ``rm -f`` command).
If *missing_ok* is true, :exc:`FileNotFoundError` and
:exc:`NotADirectoryError` exceptions will be ignored. This behavior is
similar to the POSIX ``rm -f`` command.

.. versionchanged:: 3.8
The *missing_ok* parameter was added.

.. versionchanged:: 3.14
Suppresses :exc:`NotADirectoryError` exceptions when *missing_ok* is
true.

.. method:: Path.rmdir()

Expand Down
2 changes: 1 addition & 1 deletion Lib/pathlib/_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -1104,7 +1104,7 @@ def unlink(self, missing_ok=False):
"""
try:
os.unlink(self)
except FileNotFoundError:
except (FileNotFoundError, NotADirectoryError):
if not missing_ok:
raise

Expand Down
9 changes: 9 additions & 0 deletions Lib/test/test_pathlib/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1882,6 +1882,15 @@ def test_unlink_missing_ok(self):
self.assertFileNotFound(p.unlink)
p.unlink(missing_ok=True)

#Windows will raise FileNotFoundError - handling already tested above.
@needs_posix
def test_unlink_missing_ok_intermediate_file(self):
p = self.cls(self.base) / 'fileAAA'
p.touch()
p = p / 'fileBBB'
self.assertNotADirectory(p.unlink)
p.unlink(missing_ok=True)

def test_rmdir(self):
p = self.cls(self.base) / 'dirA'
for q in p.iterdir():
Expand Down
5 changes: 5 additions & 0 deletions Lib/test/test_pathlib/test_pathlib_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1067,6 +1067,11 @@ def assertFileNotFound(self, func, *args, **kwargs):
func(*args, **kwargs)
self.assertEqual(cm.exception.errno, errno.ENOENT)

def assertNotADirectory(self, func, *args, **kwargs):
with self.assertRaises(NotADirectoryError) as cm:
func(*args, **kwargs)
self.assertEqual(cm.exception.errno, errno.ENOTDIR)

def assertEqualNormCase(self, path_a, path_b):
normcase = self.parser.normcase
self.assertEqual(normcase(path_a), normcase(path_b))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:meth:`pathlib.Path.unlink` will also ignore any :exc:`NotADirectoryError`
if *missing_ok* is true.
Loading