Skip to content

Commit a2e6c87

Browse files
barneygalesrinivasreddy
authored andcommitted
pythonGH-127381: pathlib ABCs: remove PathBase.cwd() and home() (python#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.
1 parent 269aa07 commit a2e6c87

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

Lib/pathlib/_abc.py

-15
Original file line numberDiff line numberDiff line change
@@ -735,27 +735,12 @@ def absolute(self):
735735
# Treat the root directory as the current working directory.
736736
return self.with_segments('/', *self._raw_paths)
737737

738-
@classmethod
739-
def cwd(cls):
740-
"""Return a new path pointing to the current working directory."""
741-
# We call 'absolute()' rather than using 'os.getcwd()' directly to
742-
# enable users to replace the implementation of 'absolute()' in a
743-
# subclass and benefit from the new behaviour here. This works because
744-
# os.path.abspath('.') == os.getcwd().
745-
return cls().absolute()
746-
747738
def expanduser(self):
748739
""" Return a new path with expanded ~ and ~user constructs
749740
(as returned by os.path.expanduser)
750741
"""
751742
raise UnsupportedOperation(self._unsupported_msg('expanduser()'))
752743

753-
@classmethod
754-
def home(cls):
755-
"""Return a new path pointing to expanduser('~').
756-
"""
757-
return cls("~").expanduser()
758-
759744
def readlink(self):
760745
"""
761746
Return the path to which the symbolic link points.

Lib/pathlib/_local.py

+17
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,14 @@ def absolute(self):
726726
tail.extend(self._tail)
727727
return self._from_parsed_parts(drive, root, tail)
728728

729+
@classmethod
730+
def cwd(cls):
731+
"""Return a new path pointing to the current working directory."""
732+
cwd = os.getcwd()
733+
path = cls(cwd)
734+
path._str = cwd # getcwd() returns a normalized path
735+
return path
736+
729737
def resolve(self, strict=False):
730738
"""
731739
Make the path absolute, resolving all symlinks on the way and also
@@ -907,6 +915,15 @@ def expanduser(self):
907915

908916
return self
909917

918+
@classmethod
919+
def home(cls):
920+
"""Return a new path pointing to expanduser('~').
921+
"""
922+
homedir = os.path.expanduser("~")
923+
if homedir == "~":
924+
raise RuntimeError("Could not determine home directory.")
925+
return cls(homedir)
926+
910927
@classmethod
911928
def from_uri(cls, uri):
912929
"""Return a new path from the given 'file' URI."""

Lib/test/test_pathlib/test_pathlib_abc.py

-2
Original file line numberDiff line numberDiff line change
@@ -1371,9 +1371,7 @@ def test_unsupported_operation(self):
13711371
self.assertRaises(e, p.rglob, '*')
13721372
self.assertRaises(e, lambda: list(p.walk()))
13731373
self.assertRaises(e, p.absolute)
1374-
self.assertRaises(e, P.cwd)
13751374
self.assertRaises(e, p.expanduser)
1376-
self.assertRaises(e, p.home)
13771375
self.assertRaises(e, p.readlink)
13781376
self.assertRaises(e, p.symlink_to, 'foo')
13791377
self.assertRaises(e, p.hardlink_to, 'foo')

0 commit comments

Comments
 (0)