diff -r 1f1498fe50e5 Doc/library/pathlib.rst --- a/Doc/library/pathlib.rst Wed Dec 04 23:29:51 2013 +0100 +++ b/Doc/library/pathlib.rst Thu Dec 05 11:07:21 2013 +0800 @@ -598,6 +598,19 @@ PosixPath('/home/antoine/pathlib') +.. method:: Path.samefile(file) + + Return whether the file is the same or not as the filesystem path represented + by Path, like :func:`os.path.samefile`. + + >>> p = Path('spam') + >>> q = Path('eggs') + >>> p.samefile(q) + False + >>> p.samefile('spam') + True + + .. method:: Path.stat() Return information about this path (similarly to :func:`os.stat`). diff -r 1f1498fe50e5 Lib/pathlib.py --- a/Lib/pathlib.py Wed Dec 04 23:29:51 2013 +0100 +++ b/Lib/pathlib.py Thu Dec 05 11:07:21 2013 +0800 @@ -950,6 +950,12 @@ """ return cls(os.getcwd()) + def samefile(self, other_file): + """Return whether `other_file` is the same or not as this file. + (as returned by os.path.samefile(file, other_file)). + """ + return os.path.samefile(str(self), str(other_file)) + def iterdir(self): """Iterate over the files in this directory. Does not yield any result for the special paths '.' and '..'. diff -r 1f1498fe50e5 Lib/test/test_pathlib.py --- a/Lib/test/test_pathlib.py Wed Dec 04 23:29:51 2013 +0100 +++ b/Lib/test/test_pathlib.py Thu Dec 05 11:07:21 2013 +0800 @@ -1130,6 +1130,25 @@ p = self.cls.cwd() self._test_cwd(p) + def test_samefile(self): + fileA_path = os.path.join(BASE, 'fileA') + fileB_path = os.path.join(BASE, 'dirB', 'fileB') + p = self.cls(fileA_path) + q = self.cls(fileB_path) + self.assertTrue(p.samefile(fileA_path)) + self.assertTrue(p.samefile(p)) + self.assertFalse(p.samefile(fileB_path)) + self.assertFalse(p.samefile(q)) + # Test the non-existent file case + nonExistentFile_path = os.path.join(BASE, 'foo') + r = self.cls(nonExistentFile_path) + self.assertRaises(FileNotFoundError, p.samefile, r) + self.assertRaises(FileNotFoundError, p.samefile, nonExistentFile_path) + self.assertRaises(FileNotFoundError, r.samefile, p) + self.assertRaises(FileNotFoundError, r.samefile, fileA_path) + self.assertRaises(FileNotFoundError, r.samefile, r) + self.assertRaises(FileNotFoundError, r.samefile, nonExistentFile_path) + def test_empty_path(self): # The empty path points to '.' p = self.cls('')