Skip to content

Commit 292afd1

Browse files
authored
GH-127381: pathlib ABCs: remove remaining uncommon PathBase methods (#127714)
Remove the following methods from `pathlib._abc.PathBase`: - `expanduser()` - `hardlink_to()` - `touch()` - `chmod()` - `lchmod()` - `owner()` - `group()` - `from_uri()` - `as_uri()` These operations aren't regularly supported in virtual filesystems, so they don't win a place in the `PathBase` interface. (Some of them probably don't deserve a place in `Path` :P.) They're quasi-abstract (except `lchmod()`), and they're not called by other `PathBase` methods.
1 parent 8bbd379 commit 292afd1

File tree

3 files changed

+27
-67
lines changed

3 files changed

+27
-67
lines changed

Lib/pathlib/_abc.py

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -571,12 +571,6 @@ def walk(self, top_down=True, on_error=None, follow_symlinks=False):
571571
yield path, dirnames, filenames
572572
paths += [path.joinpath(d) for d in reversed(dirnames)]
573573

574-
def expanduser(self):
575-
""" Return a new path with expanded ~ and ~user constructs
576-
(as returned by os.path.expanduser)
577-
"""
578-
raise UnsupportedOperation(self._unsupported_msg('expanduser()'))
579-
580574
def readlink(self):
581575
"""
582576
Return the path to which the symbolic link points.
@@ -597,20 +591,6 @@ def _symlink_to_target_of(self, link):
597591
"""
598592
self.symlink_to(link.readlink())
599593

600-
def hardlink_to(self, target):
601-
"""
602-
Make this path a hard link pointing to the same file as *target*.
603-
604-
Note the order of arguments (self, target) is the reverse of os.link's.
605-
"""
606-
raise UnsupportedOperation(self._unsupported_msg('hardlink_to()'))
607-
608-
def touch(self, mode=0o666, exist_ok=True):
609-
"""
610-
Create this file with the given access mode, if it doesn't exist.
611-
"""
612-
raise UnsupportedOperation(self._unsupported_msg('touch()'))
613-
614594
def mkdir(self, mode=0o777, parents=False, exist_ok=False):
615595
"""
616596
Create a new directory at this given path.
@@ -729,37 +709,3 @@ def move_into(self, target_dir):
729709
else:
730710
target = self.with_segments(target_dir, name)
731711
return self.move(target)
732-
733-
def chmod(self, mode, *, follow_symlinks=True):
734-
"""
735-
Change the permissions of the path, like os.chmod().
736-
"""
737-
raise UnsupportedOperation(self._unsupported_msg('chmod()'))
738-
739-
def lchmod(self, mode):
740-
"""
741-
Like chmod(), except if the path points to a symlink, the symlink's
742-
permissions are changed, rather than its target's.
743-
"""
744-
self.chmod(mode, follow_symlinks=False)
745-
746-
def owner(self, *, follow_symlinks=True):
747-
"""
748-
Return the login name of the file owner.
749-
"""
750-
raise UnsupportedOperation(self._unsupported_msg('owner()'))
751-
752-
def group(self, *, follow_symlinks=True):
753-
"""
754-
Return the group name of the file gid.
755-
"""
756-
raise UnsupportedOperation(self._unsupported_msg('group()'))
757-
758-
@classmethod
759-
def from_uri(cls, uri):
760-
"""Return a new path from the given 'file' URI."""
761-
raise UnsupportedOperation(cls._unsupported_msg('from_uri()'))
762-
763-
def as_uri(self):
764-
"""Return the path as a URI."""
765-
raise UnsupportedOperation(self._unsupported_msg('as_uri()'))

Lib/pathlib/_local.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,6 @@ class Path(PathBase, PurePath):
526526
but cannot instantiate a WindowsPath on a POSIX system or vice versa.
527527
"""
528528
__slots__ = ()
529-
as_uri = PurePath.as_uri
530529

531530
@classmethod
532531
def _unsupported_msg(cls, attribute):
@@ -813,6 +812,12 @@ def owner(self, *, follow_symlinks=True):
813812
"""
814813
uid = self.stat(follow_symlinks=follow_symlinks).st_uid
815814
return pwd.getpwuid(uid).pw_name
815+
else:
816+
def owner(self, *, follow_symlinks=True):
817+
"""
818+
Return the login name of the file owner.
819+
"""
820+
raise UnsupportedOperation(self._unsupported_msg('owner()'))
816821

817822
if grp:
818823
def group(self, *, follow_symlinks=True):
@@ -821,6 +826,12 @@ def group(self, *, follow_symlinks=True):
821826
"""
822827
gid = self.stat(follow_symlinks=follow_symlinks).st_gid
823828
return grp.getgrgid(gid).gr_name
829+
else:
830+
def group(self, *, follow_symlinks=True):
831+
"""
832+
Return the group name of the file gid.
833+
"""
834+
raise UnsupportedOperation(self._unsupported_msg('group()'))
824835

825836
if hasattr(os, "readlink"):
826837
def readlink(self):
@@ -892,6 +903,13 @@ def chmod(self, mode, *, follow_symlinks=True):
892903
"""
893904
os.chmod(self, mode, follow_symlinks=follow_symlinks)
894905

906+
def lchmod(self, mode):
907+
"""
908+
Like chmod(), except if the path points to a symlink, the symlink's
909+
permissions are changed, rather than its target's.
910+
"""
911+
self.chmod(mode, follow_symlinks=False)
912+
895913
def unlink(self, missing_ok=False):
896914
"""
897915
Remove this file or link.
@@ -988,6 +1006,14 @@ def hardlink_to(self, target):
9881006
Note the order of arguments (self, target) is the reverse of os.link's.
9891007
"""
9901008
os.link(target, self)
1009+
else:
1010+
def hardlink_to(self, target):
1011+
"""
1012+
Make this path a hard link pointing to the same file as *target*.
1013+
1014+
Note the order of arguments (self, target) is the reverse of os.link's.
1015+
"""
1016+
raise UnsupportedOperation(self._unsupported_msg('hardlink_to()'))
9911017

9921018
def expanduser(self):
9931019
""" Return a new path with expanded ~ and ~user constructs

Lib/test/test_pathlib/test_pathlib_abc.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,21 +1312,9 @@ def test_unsupported_operation(self):
13121312
self.assertRaises(e, lambda: list(p.glob('*')))
13131313
self.assertRaises(e, lambda: list(p.rglob('*')))
13141314
self.assertRaises(e, lambda: list(p.walk()))
1315-
self.assertRaises(e, p.expanduser)
13161315
self.assertRaises(e, p.readlink)
13171316
self.assertRaises(e, p.symlink_to, 'foo')
1318-
self.assertRaises(e, p.hardlink_to, 'foo')
13191317
self.assertRaises(e, p.mkdir)
1320-
self.assertRaises(e, p.touch)
1321-
self.assertRaises(e, p.chmod, 0o755)
1322-
self.assertRaises(e, p.lchmod, 0o755)
1323-
self.assertRaises(e, p.owner)
1324-
self.assertRaises(e, p.group)
1325-
self.assertRaises(e, p.as_uri)
1326-
1327-
def test_as_uri_common(self):
1328-
e = UnsupportedOperation
1329-
self.assertRaises(e, self.cls('').as_uri)
13301318

13311319
def test_fspath_common(self):
13321320
self.assertRaises(TypeError, os.fspath, self.cls(''))

0 commit comments

Comments
 (0)