Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 173ff4a

Browse files
cooperleesambv
authored andcommitted
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()
1 parent 9eef9e8 commit 173ff4a

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

Lib/pathlib.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,6 +1329,27 @@ def is_file(self):
13291329
# (see https://bitbucket.org/pitrou/pathlib/issue/12/)
13301330
return False
13311331

1332+
def is_mount(self):
1333+
"""
1334+
Check if this path is a POSIX mount point
1335+
"""
1336+
# Need to exist and be a dir
1337+
if not self.exists() or not self.is_dir():
1338+
return False
1339+
1340+
parent = Path(self.parent)
1341+
try:
1342+
parent_dev = parent.stat().st_dev
1343+
except OSError:
1344+
return False
1345+
1346+
dev = self.stat().st_dev
1347+
if dev != parent_dev:
1348+
return True
1349+
ino = self.stat().st_ino
1350+
parent_ino = parent.stat().st_ino
1351+
return ino == parent_ino
1352+
13321353
def is_symlink(self):
13331354
"""
13341355
Whether this path is a symbolic link.
@@ -1416,3 +1437,6 @@ def owner(self):
14161437

14171438
def group(self):
14181439
raise NotImplementedError("Path.group() is unsupported on this system")
1440+
1441+
def is_mount(self):
1442+
raise NotImplementedError("Path.is_mount() is unsupported on this system")

Lib/test/test_pathlib.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1880,6 +1880,18 @@ def test_is_file(self):
18801880
self.assertFalse((P / 'linkB').is_file())
18811881
self.assertFalse((P/ 'brokenLink').is_file())
18821882

1883+
@only_posix
1884+
def test_is_mount(self):
1885+
P = self.cls(BASE)
1886+
R = self.cls('/') # TODO: Work out windows
1887+
self.assertFalse((P / 'fileA').is_mount())
1888+
self.assertFalse((P / 'dirA').is_mount())
1889+
self.assertFalse((P / 'non-existing').is_mount())
1890+
self.assertFalse((P / 'fileA' / 'bah').is_mount())
1891+
self.assertTrue(R.is_mount())
1892+
if support.can_symlink():
1893+
self.assertFalse((P / 'linkA').is_mount())
1894+
18831895
def test_is_symlink(self):
18841896
P = self.cls(BASE)
18851897
self.assertFalse((P / 'fileA').is_symlink())

0 commit comments

Comments
 (0)