[Python-checkins] cpython: Issue #19775: Add a samefile() method to pathlib Path objects.

antoine.pitrou python-checkins at python.org
Tue May 13 10:50:39 CEST 2014


http://hg.python.org/cpython/rev/197ac5d79456
changeset:   90684:197ac5d79456
user:        Antoine Pitrou <solipsis at pitrou.net>
date:        Tue May 13 10:50:15 2014 +0200
summary:
  Issue #19775: Add a samefile() method to pathlib Path objects.

Initial patch by Vajrasky Kok.

files:
  Doc/library/pathlib.rst  |  19 +++++++++++++++++++
  Lib/pathlib.py           |  11 +++++++++++
  Lib/test/test_pathlib.py |  20 ++++++++++++++++++++
  Misc/NEWS                |   3 +++
  4 files changed, 53 insertions(+), 0 deletions(-)


diff --git a/Doc/library/pathlib.rst b/Doc/library/pathlib.rst
--- a/Doc/library/pathlib.rst
+++ b/Doc/library/pathlib.rst
@@ -884,6 +884,25 @@
    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')
+      >>> q = Path('eggs')
+      >>> p.samefile(q)
+      False
+      >>> p.samefile('spam')
+      True
+
+   .. versionadded:: 3.5
+
+
 .. method:: Path.symlink_to(target, target_is_directory=False)
 
    Make this path a symbolic link to *target*.  Under Windows,
diff --git a/Lib/pathlib.py b/Lib/pathlib.py
--- a/Lib/pathlib.py
+++ b/Lib/pathlib.py
@@ -961,6 +961,17 @@
         """
         return cls(os.getcwd())
 
+    def samefile(self, other_path):
+        """Return whether `other_file` is the same or not as this file.
+        (as returned by os.path.samefile(file, other_file)).
+        """
+        st = self.stat()
+        try:
+            other_st = other_path.stat()
+        except AttributeError:
+            other_st = os.stat(other_path)
+        return os.path.samestat(st, other_st)
+
     def iterdir(self):
         """Iterate over the files in this directory.  Does not yield any
         result for the special paths '.' and '..'.
diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py
--- a/Lib/test/test_pathlib.py
+++ b/Lib/test/test_pathlib.py
@@ -1251,6 +1251,26 @@
         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)
+        pp = self.cls(fileA_path)
+        q = self.cls(fileB_path)
+        self.assertTrue(p.samefile(fileA_path))
+        self.assertTrue(p.samefile(pp))
+        self.assertFalse(p.samefile(fileB_path))
+        self.assertFalse(p.samefile(q))
+        # Test the non-existent file case
+        non_existent = os.path.join(BASE, 'foo')
+        r = self.cls(non_existent)
+        self.assertRaises(FileNotFoundError, p.samefile, r)
+        self.assertRaises(FileNotFoundError, p.samefile, non_existent)
+        self.assertRaises(FileNotFoundError, r.samefile, p)
+        self.assertRaises(FileNotFoundError, r.samefile, non_existent)
+        self.assertRaises(FileNotFoundError, r.samefile, r)
+        self.assertRaises(FileNotFoundError, r.samefile, non_existent)
+
     def test_empty_path(self):
         # The empty path points to '.'
         p = self.cls('')
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -84,6 +84,9 @@
 Library
 -------
 
+- Issue #19775: Add a samefile() method to pathlib Path objects.  Initial
+  patch by Vajrasky Kok.
+
 - Issue #21398: Fix an unicode error in the pydoc pager when the documentation
   contains characters not encodable to the stdout encoding.
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list