]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-127381: pathlib ABCs: remove remaining uncommon `PathBase` methods (#127714)
authorBarney Gale <barney.gale@gmail.com>
Thu, 12 Dec 2024 06:49:34 +0000 (06:49 +0000)
committerGitHub <noreply@github.com>
Thu, 12 Dec 2024 06:49:34 +0000 (06:49 +0000)
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.

Lib/pathlib/_abc.py
Lib/pathlib/_local.py
Lib/test/test_pathlib/test_pathlib_abc.py

index 02c6e0500617aaa572f2726cf34c32ec1e856b98..b10aba85132332b770c938135e9f067faaa6421e 100644 (file)
@@ -571,12 +571,6 @@ class PathBase(PurePathBase):
                 yield path, dirnames, filenames
                 paths += [path.joinpath(d) for d in reversed(dirnames)]
 
-    def expanduser(self):
-        """ Return a new path with expanded ~ and ~user constructs
-        (as returned by os.path.expanduser)
-        """
-        raise UnsupportedOperation(self._unsupported_msg('expanduser()'))
-
     def readlink(self):
         """
         Return the path to which the symbolic link points.
@@ -597,20 +591,6 @@ class PathBase(PurePathBase):
         """
         self.symlink_to(link.readlink())
 
-    def hardlink_to(self, target):
-        """
-        Make this path a hard link pointing to the same file as *target*.
-
-        Note the order of arguments (self, target) is the reverse of os.link's.
-        """
-        raise UnsupportedOperation(self._unsupported_msg('hardlink_to()'))
-
-    def touch(self, mode=0o666, exist_ok=True):
-        """
-        Create this file with the given access mode, if it doesn't exist.
-        """
-        raise UnsupportedOperation(self._unsupported_msg('touch()'))
-
     def mkdir(self, mode=0o777, parents=False, exist_ok=False):
         """
         Create a new directory at this given path.
@@ -729,37 +709,3 @@ class PathBase(PurePathBase):
         else:
             target = self.with_segments(target_dir, name)
         return self.move(target)
-
-    def chmod(self, mode, *, follow_symlinks=True):
-        """
-        Change the permissions of the path, like os.chmod().
-        """
-        raise UnsupportedOperation(self._unsupported_msg('chmod()'))
-
-    def lchmod(self, mode):
-        """
-        Like chmod(), except if the path points to a symlink, the symlink's
-        permissions are changed, rather than its target's.
-        """
-        self.chmod(mode, follow_symlinks=False)
-
-    def owner(self, *, follow_symlinks=True):
-        """
-        Return the login name of the file owner.
-        """
-        raise UnsupportedOperation(self._unsupported_msg('owner()'))
-
-    def group(self, *, follow_symlinks=True):
-        """
-        Return the group name of the file gid.
-        """
-        raise UnsupportedOperation(self._unsupported_msg('group()'))
-
-    @classmethod
-    def from_uri(cls, uri):
-        """Return a new path from the given 'file' URI."""
-        raise UnsupportedOperation(cls._unsupported_msg('from_uri()'))
-
-    def as_uri(self):
-        """Return the path as a URI."""
-        raise UnsupportedOperation(self._unsupported_msg('as_uri()'))
index 85437ec80bfcc4f562719851368ef8986828f203..0dfe9d2390ecffb4d54ad40b6033534660650b14 100644 (file)
@@ -526,7 +526,6 @@ class Path(PathBase, PurePath):
     but cannot instantiate a WindowsPath on a POSIX system or vice versa.
     """
     __slots__ = ()
-    as_uri = PurePath.as_uri
 
     @classmethod
     def _unsupported_msg(cls, attribute):
@@ -813,6 +812,12 @@ class Path(PathBase, PurePath):
             """
             uid = self.stat(follow_symlinks=follow_symlinks).st_uid
             return pwd.getpwuid(uid).pw_name
+    else:
+        def owner(self, *, follow_symlinks=True):
+            """
+            Return the login name of the file owner.
+            """
+            raise UnsupportedOperation(self._unsupported_msg('owner()'))
 
     if grp:
         def group(self, *, follow_symlinks=True):
@@ -821,6 +826,12 @@ class Path(PathBase, PurePath):
             """
             gid = self.stat(follow_symlinks=follow_symlinks).st_gid
             return grp.getgrgid(gid).gr_name
+    else:
+        def group(self, *, follow_symlinks=True):
+            """
+            Return the group name of the file gid.
+            """
+            raise UnsupportedOperation(self._unsupported_msg('group()'))
 
     if hasattr(os, "readlink"):
         def readlink(self):
@@ -892,6 +903,13 @@ class Path(PathBase, PurePath):
         """
         os.chmod(self, mode, follow_symlinks=follow_symlinks)
 
+    def lchmod(self, mode):
+        """
+        Like chmod(), except if the path points to a symlink, the symlink's
+        permissions are changed, rather than its target's.
+        """
+        self.chmod(mode, follow_symlinks=False)
+
     def unlink(self, missing_ok=False):
         """
         Remove this file or link.
@@ -988,6 +1006,14 @@ class Path(PathBase, PurePath):
             Note the order of arguments (self, target) is the reverse of os.link's.
             """
             os.link(target, self)
+    else:
+        def hardlink_to(self, target):
+            """
+            Make this path a hard link pointing to the same file as *target*.
+
+            Note the order of arguments (self, target) is the reverse of os.link's.
+            """
+            raise UnsupportedOperation(self._unsupported_msg('hardlink_to()'))
 
     def expanduser(self):
         """ Return a new path with expanded ~ and ~user constructs
index f4c364c6fe51092f641fa83a90af3292d3d8602e..d770b87dc6a104cb22b12357162321dcd567cf41 100644 (file)
@@ -1312,21 +1312,9 @@ class PathBaseTest(PurePathBaseTest):
         self.assertRaises(e, lambda: list(p.glob('*')))
         self.assertRaises(e, lambda: list(p.rglob('*')))
         self.assertRaises(e, lambda: list(p.walk()))
-        self.assertRaises(e, p.expanduser)
         self.assertRaises(e, p.readlink)
         self.assertRaises(e, p.symlink_to, 'foo')
-        self.assertRaises(e, p.hardlink_to, 'foo')
         self.assertRaises(e, p.mkdir)
-        self.assertRaises(e, p.touch)
-        self.assertRaises(e, p.chmod, 0o755)
-        self.assertRaises(e, p.lchmod, 0o755)
-        self.assertRaises(e, p.owner)
-        self.assertRaises(e, p.group)
-        self.assertRaises(e, p.as_uri)
-
-    def test_as_uri_common(self):
-        e = UnsupportedOperation
-        self.assertRaises(e, self.cls('').as_uri)
 
     def test_fspath_common(self):
         self.assertRaises(TypeError, os.fspath, self.cls(''))