Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-86943: implement pathlib.WindowsPath.is_mount() #31458

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Doc/library/pathlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -847,10 +847,15 @@ call fails (for example because the path doesn't exist).
function checks whether *path*'s parent, :file:`path/..`, is on a different
device than *path*, or whether :file:`path/..` and *path* point to the same
i-node on the same device --- this should detect mount points for all Unix
and POSIX variants. Not implemented on Windows.
and POSIX variants. On Windows, a mount point is considered to be a drive
letter root (e.g. ``c:\``), a UNC share (e.g. ``\\server\share``), or a
volume mounted on a filesystem folder.
barneygale marked this conversation as resolved.
Show resolved Hide resolved

.. versionadded:: 3.7

.. versionchanged:: 3.11
Windows support was added.


.. method:: Path.is_symlink()

Expand Down
21 changes: 2 additions & 19 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1269,23 +1269,9 @@ def is_file(self):

def is_mount(self):
"""
Check if this path is a POSIX mount point
Check if this path is a mount point
"""
# Need to exist and be a dir
if not self.exists() or not self.is_dir():
return False

try:
parent_dev = self.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 = self.parent.stat().st_ino
return ino == parent_ino
return self._flavour.pathmod.ismount(self)

def is_symlink(self):
"""
Expand Down Expand Up @@ -1393,6 +1379,3 @@ class WindowsPath(Path, PureWindowsPath):
On a Windows system, instantiating a Path should return this object.
"""
__slots__ = ()

def is_mount(self):
raise NotImplementedError("Path.is_mount() is unsupported on this system")
10 changes: 6 additions & 4 deletions Lib/test/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -2275,19 +2275,21 @@ def test_is_file(self):
self.assertIs((P / 'fileA\udfff').is_file(), False)
self.assertIs((P / 'fileA\x00').is_file(), False)

@only_posix
def test_is_mount(self):
P = self.cls(BASE)
R = self.cls('/') # TODO: Work out Windows.
if os.name == 'nt':
R = self.cls('c:\\')
else:
R = self.cls('/')
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 os_helper.can_symlink():
self.assertFalse((P / 'linkA').is_mount())
self.assertIs(self.cls('/\udfff').is_mount(), False)
self.assertIs(self.cls('/\x00').is_mount(), False)
self.assertIs((R / '\udfff').is_mount(), False)
self.assertIs((R / '\x00').is_mount(), False)

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