Skip to content

Commit 29650fe

Browse files
authored
gh-86943: implement pathlib.WindowsPath.is_mount() (GH-31458)
Have `pathlib.WindowsPath.is_mount()` call `ntpath.ismount()`. Previously it raised `NotImplementedError` unconditionally. https://bugs.python.org/issue42777
1 parent a302a27 commit 29650fe

File tree

4 files changed

+14
-24
lines changed

4 files changed

+14
-24
lines changed

Doc/library/pathlib.rst

+6-1
Original file line numberDiff line numberDiff line change
@@ -876,10 +876,15 @@ call fails (for example because the path doesn't exist).
876876
function checks whether *path*'s parent, :file:`path/..`, is on a different
877877
device than *path*, or whether :file:`path/..` and *path* point to the same
878878
i-node on the same device --- this should detect mount points for all Unix
879-
and POSIX variants. Not implemented on Windows.
879+
and POSIX variants. On Windows, a mount point is considered to be a drive
880+
letter root (e.g. ``c:\``), a UNC share (e.g. ``\\server\share``), or a
881+
mounted filesystem directory.
880882

881883
.. versionadded:: 3.7
882884

885+
.. versionchanged:: 3.12
886+
Windows support was added.
887+
883888

884889
.. method:: Path.is_symlink()
885890

Lib/pathlib.py

+2-19
Original file line numberDiff line numberDiff line change
@@ -1211,23 +1211,9 @@ def is_file(self):
12111211

12121212
def is_mount(self):
12131213
"""
1214-
Check if this path is a POSIX mount point
1214+
Check if this path is a mount point
12151215
"""
1216-
# Need to exist and be a dir
1217-
if not self.exists() or not self.is_dir():
1218-
return False
1219-
1220-
try:
1221-
parent_dev = self.parent.stat().st_dev
1222-
except OSError:
1223-
return False
1224-
1225-
dev = self.stat().st_dev
1226-
if dev != parent_dev:
1227-
return True
1228-
ino = self.stat().st_ino
1229-
parent_ino = self.parent.stat().st_ino
1230-
return ino == parent_ino
1216+
return self._flavour.pathmod.ismount(self)
12311217

12321218
def is_symlink(self):
12331219
"""
@@ -1378,6 +1364,3 @@ class WindowsPath(Path, PureWindowsPath):
13781364
On a Windows system, instantiating a Path should return this object.
13791365
"""
13801366
__slots__ = ()
1381-
1382-
def is_mount(self):
1383-
raise NotImplementedError("Path.is_mount() is unsupported on this system")

Lib/test/test_pathlib.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -2294,19 +2294,20 @@ def test_is_file(self):
22942294
self.assertIs((P / 'fileA\udfff').is_file(), False)
22952295
self.assertIs((P / 'fileA\x00').is_file(), False)
22962296

2297-
@only_posix
22982297
def test_is_mount(self):
22992298
P = self.cls(BASE)
2300-
R = self.cls('/') # TODO: Work out Windows.
2299+
if os.name == 'nt':
2300+
R = self.cls('c:\\')
2301+
else:
2302+
R = self.cls('/')
23012303
self.assertFalse((P / 'fileA').is_mount())
23022304
self.assertFalse((P / 'dirA').is_mount())
23032305
self.assertFalse((P / 'non-existing').is_mount())
23042306
self.assertFalse((P / 'fileA' / 'bah').is_mount())
23052307
self.assertTrue(R.is_mount())
23062308
if os_helper.can_symlink():
23072309
self.assertFalse((P / 'linkA').is_mount())
2308-
self.assertIs(self.cls('/\udfff').is_mount(), False)
2309-
self.assertIs(self.cls('/\x00').is_mount(), False)
2310+
self.assertIs((R / '\udfff').is_mount(), False)
23102311

23112312
def test_is_symlink(self):
23122313
P = self.cls(BASE)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Implement :meth:`pathlib.Path.is_mount` for Windows paths.

0 commit comments

Comments
 (0)