summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBarney Gale <barney.gale@gmail.com>2024-11-30 18:39:39 (GMT)
committerGitHub <noreply@github.com>2024-11-30 18:39:39 (GMT)
commit328187cc4fcdd578db42cf6a16c197c3382157a7 (patch)
tree0e3801a45867fa87d4a7cfb5b7dcfda06d702a97
parent4e0a4cafe8d8ecb43db62aed1d5671af583104e7 (diff)
downloadcpython-328187cc4fcdd578db42cf6a16c197c3382157a7.zip
cpython-328187cc4fcdd578db42cf6a16c197c3382157a7.tar.gz
cpython-328187cc4fcdd578db42cf6a16c197c3382157a7.tar.bz2
GH-127381: pathlib ABCs: remove `PathBase.cwd()` and `home()` (#127427)
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.
-rw-r--r--Lib/pathlib/_abc.py15
-rw-r--r--Lib/pathlib/_local.py17
-rw-r--r--Lib/test/test_pathlib/test_pathlib_abc.py2
3 files changed, 17 insertions, 17 deletions
diff --git a/Lib/pathlib/_abc.py b/Lib/pathlib/_abc.py
index 697f329..2b314b6 100644
--- a/Lib/pathlib/_abc.py
+++ b/Lib/pathlib/_abc.py
@@ -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.
diff --git a/Lib/pathlib/_local.py b/Lib/pathlib/_local.py
index 25c1e3f..b5d9dc4 100644
--- a/Lib/pathlib/_local.py
+++ b/Lib/pathlib/_local.py
@@ -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
@@ -908,6 +916,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."""
if not uri.startswith('file:'):
diff --git a/Lib/test/test_pathlib/test_pathlib_abc.py b/Lib/test/test_pathlib/test_pathlib_abc.py
index 7ca35e3..af94ac0 100644
--- a/Lib/test/test_pathlib/test_pathlib_abc.py
+++ b/Lib/test/test_pathlib/test_pathlib_abc.py
@@ -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')