[Python-checkins] bpo-30897: Add is_mount() to pathlib.Path (#2669)

Łukasz Langa webhook-mailer at python.org
Tue Aug 1 18:35:50 EDT 2017


https://github.com/python/cpython/commit/173ff4a58a6b337fe8a0eb44f211f33f278d3d5f
commit: 173ff4a58a6b337fe8a0eb44f211f33f278d3d5f
branch: master
author: Cooper Lees <me at cooperlees.com>
committer: Łukasz Langa <lukasz at langa.pl>
date: 2017-08-01T15:35:45-07:00
summary:

bpo-30897: Add is_mount() to pathlib.Path (#2669)

* Add in is_mount() call to pathlib.Path similiar to os.path.ismount(path)
* Add tests for is_mount()

files:
M Lib/pathlib.py
M Lib/test/test_pathlib.py

diff --git a/Lib/pathlib.py b/Lib/pathlib.py
index 0e65c61f654..c14ddd03356 100644
--- a/Lib/pathlib.py
+++ b/Lib/pathlib.py
@@ -1329,6 +1329,27 @@ def is_file(self):
             # (see https://bitbucket.org/pitrou/pathlib/issue/12/)
             return False
 
+    def is_mount(self):
+        """
+        Check if this path is a POSIX mount point
+        """
+        # Need to exist and be a dir
+        if not self.exists() or not self.is_dir():
+            return False
+
+        parent = Path(self.parent)
+        try:
+            parent_dev = parent.stat().st_dev
+        except OSError:
+            return False
+
+        dev = self.stat().st_dev
+        if dev != parent_dev:
+            return True
+        ino = self.stat().st_ino
+        parent_ino = parent.stat().st_ino
+        return ino == parent_ino
+
     def is_symlink(self):
         """
         Whether this path is a symbolic link.
@@ -1416,3 +1437,6 @@ def owner(self):
 
     def group(self):
         raise NotImplementedError("Path.group() is unsupported on this system")
+
+    def is_mount(self):
+        raise NotImplementedError("Path.is_mount() is unsupported on this system")
diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py
index 16bfee0a47d..962adde38da 100644
--- a/Lib/test/test_pathlib.py
+++ b/Lib/test/test_pathlib.py
@@ -1880,6 +1880,18 @@ def test_is_file(self):
             self.assertFalse((P / 'linkB').is_file())
             self.assertFalse((P/ 'brokenLink').is_file())
 
+    @only_posix
+    def test_is_mount(self):
+        P = self.cls(BASE)
+        R = self.cls('/')  # TODO: Work out windows
+        self.assertFalse((P / 'fileA').is_mount())
+        self.assertFalse((P / 'dirA').is_mount())
+        self.assertFalse((P / 'non-existing').is_mount())
+        self.assertFalse((P / 'fileA' / 'bah').is_mount())
+        self.assertTrue(R.is_mount())
+        if support.can_symlink():
+            self.assertFalse((P / 'linkA').is_mount())
+
     def test_is_symlink(self):
         P = self.cls(BASE)
         self.assertFalse((P / 'fileA').is_symlink())



More information about the Python-checkins mailing list