FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-119993 ignore `NotADirectoryError` in `Path.unlink()` if `missing_ok` is `True` by MusicalNinjaDad · Pull Request #120049 · python/cpython · GitHub

/ cpython Public
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (3) .rst  (2) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
12 changes: 8 additions & 4 deletions Doc/library/pathlib.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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):
Comment thread
MusicalNinjaDad marked this conversation as resolved.
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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.

Back | FazBrowse Home | New Git URL