Skip to content

GH-127381: pathlib ABCs: remove PathBase.cwd() and home() #127427

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

Merged
merged 1 commit into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 0 additions & 15 deletions Lib/pathlib/_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -735,27 +735,12 @@ def absolute(self):
# 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.
Expand Down
17 changes: 17 additions & 0 deletions Lib/pathlib/_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,14 @@ def absolute(self):
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
Expand Down Expand Up @@ -907,6 +915,15 @@ def expanduser(self):

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."""
Expand Down
2 changes: 0 additions & 2 deletions Lib/test/test_pathlib/test_pathlib_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1371,9 +1371,7 @@ def test_unsupported_operation(self):
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')
Expand Down
Loading