]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-127381: pathlib ABCs: remove `PathBase.cwd()` and `home()` (#127427)
authorBarney Gale <barney.gale@gmail.com>
Sat, 30 Nov 2024 18:39:39 +0000 (18:39 +0000)
committerGitHub <noreply@github.com>
Sat, 30 Nov 2024 18:39:39 +0000 (18:39 +0000)
These classmethods presume that the user has retained the original
`__init__()` signature, which may not be the case. Also, many virtual
filesystems don't provide current or home directories.

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

index 697f32985ff652e3cb302a753a6ecdc0bcc55e80..2b314b6c9a16bf4746e8c592d7acb2201a29d87d 100644 (file)
@@ -735,27 +735,12 @@ class PathBase(PurePathBase):
             # Treat the root directory as the current working directory.
             return self.with_segments('/', *self._raw_paths)
 
-    @classmethod
-    def cwd(cls):
-        """Return a new path pointing to the current working directory."""
-        # We call 'absolute()' rather than using 'os.getcwd()' directly to
-        # enable users to replace the implementation of 'absolute()' in a
-        # subclass and benefit from the new behaviour here. This works because
-        # os.path.abspath('.') == os.getcwd().
-        return cls().absolute()
-
     def expanduser(self):
         """ Return a new path with expanded ~ and ~user constructs
         (as returned by os.path.expanduser)
         """
         raise UnsupportedOperation(self._unsupported_msg('expanduser()'))
 
-    @classmethod
-    def home(cls):
-        """Return a new path pointing to expanduser('~').
-        """
-        return cls("~").expanduser()
-
     def readlink(self):
         """
         Return the path to which the symbolic link points.
index 25c1e3f44ea7e820518f1f4acb8ff458000ce6cf..b5d9dc49f5846339225dccc4b4d40ce2c7c8680b 100644 (file)
@@ -726,6 +726,14 @@ class Path(PathBase, PurePath):
         tail.extend(self._tail)
         return self._from_parsed_parts(drive, root, tail)
 
+    @classmethod
+    def cwd(cls):
+        """Return a new path pointing to the current working directory."""
+        cwd = os.getcwd()
+        path = cls(cwd)
+        path._str = cwd  # getcwd() returns a normalized path
+        return path
+
     def resolve(self, strict=False):
         """
         Make the path absolute, resolving all symlinks on the way and also
@@ -907,6 +915,15 @@ class Path(PathBase, PurePath):
 
         return self
 
+    @classmethod
+    def home(cls):
+        """Return a new path pointing to expanduser('~').
+        """
+        homedir = os.path.expanduser("~")
+        if homedir == "~":
+            raise RuntimeError("Could not determine home directory.")
+        return cls(homedir)
+
     @classmethod
     def from_uri(cls, uri):
         """Return a new path from the given 'file' URI."""
index 7ca35e3dc7ee00819db3dfb6e9cd126db54424dd..af94ac039808f0ce3d63238960fd3c5a8b9c48f7 100644 (file)
@@ -1371,9 +1371,7 @@ class PathBaseTest(PurePathBaseTest):
         self.assertRaises(e, p.rglob, '*')
         self.assertRaises(e, lambda: list(p.walk()))
         self.assertRaises(e, p.absolute)
-        self.assertRaises(e, P.cwd)
         self.assertRaises(e, p.expanduser)
-        self.assertRaises(e, p.home)
         self.assertRaises(e, p.readlink)
         self.assertRaises(e, p.symlink_to, 'foo')
         self.assertRaises(e, p.hardlink_to, 'foo')