cpython: 197ac5d79456 (original) (raw)
Mercurial > cpython
changeset 90684:197ac5d79456
Issue #19775: Add a samefile() method to pathlib Path objects. Initial patch by Vajrasky Kok. [#19775]
Antoine Pitrou solipsis@pitrou.net | |
---|---|
date | Tue, 13 May 2014 10:50:15 +0200 |
parents | 560320c10564 |
children | 8885fc2e92b3 |
files | Doc/library/pathlib.rst Lib/pathlib.py Lib/test/test_pathlib.py Misc/NEWS |
diffstat | 4 files changed, 53 insertions(+), 0 deletions(-)[+] [-] Doc/library/pathlib.rst 19 Lib/pathlib.py 11 Lib/test/test_pathlib.py 20 Misc/NEWS 3 |
line wrap: on
line diff
--- a/Doc/library/pathlib.rst +++ b/Doc/library/pathlib.rst @@ -884,6 +884,25 @@ call fails (for example because the path Remove this directory. The directory must be empty. +.. method:: Path.samefile(other_path) +
- Return whether this path points to the same file as other_path, which
- can be either a Path object, or a string. The semantics are similar
- to :func:
os.path.samefile
and :func:os.path.samestat
. + - An :exc:
OSError
can be raised if either file cannot be accessed for some - reason. +
>>> p = Path('spam')[](#l1.16)
>>> q = Path('eggs')[](#l1.17)
>>> p.samefile(q)[](#l1.18)
False[](#l1.19)
>>> p.samefile('spam')[](#l1.20)
True[](#l1.21)
+ .. method:: Path.symlink_to(target, target_is_directory=False) Make this path a symbolic link to target. Under Windows,
--- a/Lib/pathlib.py +++ b/Lib/pathlib.py @@ -961,6 +961,17 @@ class Path(PurePath): """ return cls(os.getcwd())
- def samefile(self, other_path):
"""Return whether `other_file` is the same or not as this file.[](#l2.8)
(as returned by os.path.samefile(file, other_file)).[](#l2.9)
"""[](#l2.10)
st = self.stat()[](#l2.11)
try:[](#l2.12)
other_st = other_path.stat()[](#l2.13)
except AttributeError:[](#l2.14)
other_st = os.stat(other_path)[](#l2.15)
return os.path.samestat(st, other_st)[](#l2.16)
+ def iterdir(self): """Iterate over the files in this directory. Does not yield any result for the special paths '.' and '..'.
--- a/Lib/test/test_pathlib.py +++ b/Lib/test/test_pathlib.py @@ -1251,6 +1251,26 @@ class _BasePathTest(object): p = self.cls.cwd() self._test_cwd(p)
- def test_samefile(self):
fileA_path = os.path.join(BASE, 'fileA')[](#l3.8)
fileB_path = os.path.join(BASE, 'dirB', 'fileB')[](#l3.9)
p = self.cls(fileA_path)[](#l3.10)
pp = self.cls(fileA_path)[](#l3.11)
q = self.cls(fileB_path)[](#l3.12)
self.assertTrue(p.samefile(fileA_path))[](#l3.13)
self.assertTrue(p.samefile(pp))[](#l3.14)
self.assertFalse(p.samefile(fileB_path))[](#l3.15)
self.assertFalse(p.samefile(q))[](#l3.16)
# Test the non-existent file case[](#l3.17)
non_existent = os.path.join(BASE, 'foo')[](#l3.18)
r = self.cls(non_existent)[](#l3.19)
self.assertRaises(FileNotFoundError, p.samefile, r)[](#l3.20)
self.assertRaises(FileNotFoundError, p.samefile, non_existent)[](#l3.21)
self.assertRaises(FileNotFoundError, r.samefile, p)[](#l3.22)
self.assertRaises(FileNotFoundError, r.samefile, non_existent)[](#l3.23)
self.assertRaises(FileNotFoundError, r.samefile, r)[](#l3.24)
self.assertRaises(FileNotFoundError, r.samefile, non_existent)[](#l3.25)
+ def test_empty_path(self): # The empty path points to '.' p = self.cls('')
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -84,6 +84,9 @@ Core and Builtins Library ------- +- Issue #19775: Add a samefile() method to pathlib Path objects. Initial