summaryrefslogtreecommitdiffstats
path: root/Lib/pathlib.py
diff options
context:
space:
mode:
authorBarney Gale <barney.gale@gmail.com>2021-04-07 22:50:13 (GMT)
committerGitHub <noreply@github.com>2021-04-07 22:50:13 (GMT)
commit3f3d82b84823eb28abeedf317bbe107bbe7f6492 (patch)
tree8b2176164b9bd84834d8381f1e2b102f8bb5f44d /Lib/pathlib.py
parentdf5dc1c7a536abc2b497cb9506f8a37949838309 (diff)
downloadcpython-3f3d82b84823eb28abeedf317bbe107bbe7f6492.zip
cpython-3f3d82b84823eb28abeedf317bbe107bbe7f6492.tar.gz
cpython-3f3d82b84823eb28abeedf317bbe107bbe7f6492.tar.bz2
bpo-39899: os.path.expanduser(): don't guess other Windows users' home directories if the basename of the current user's home directory doesn't match their username. (GH-18841)
This makes `ntpath.expanduser()` match `pathlib.Path.expanduser()` in this regard, and is more in line with `posixpath.expanduser()`'s cautious approach. Also remove the near-duplicate implementation of `expanduser()` in pathlib, and by doing so fix a bug where KeyError could be raised when expanding another user's home directory.
Diffstat (limited to 'Lib/pathlib.py')
-rw-r--r--Lib/pathlib.py51
1 files changed, 6 insertions, 45 deletions
diff --git a/Lib/pathlib.py b/Lib/pathlib.py
index 9e682dc..19d45a3 100644
--- a/Lib/pathlib.py
+++ b/Lib/pathlib.py
@@ -246,34 +246,6 @@ class _WindowsFlavour(_Flavour):
# It's a path on a network drive => 'file://host/share/a/b'
return 'file:' + urlquote_from_bytes(path.as_posix().encode('utf-8'))
- def gethomedir(self, username):
- if 'USERPROFILE' in os.environ:
- userhome = os.environ['USERPROFILE']
- elif 'HOMEPATH' in os.environ:
- try:
- drv = os.environ['HOMEDRIVE']
- except KeyError:
- drv = ''
- userhome = drv + os.environ['HOMEPATH']
- else:
- raise RuntimeError("Can't determine home directory")
-
- if username:
- # Try to guess user home directory. By default all users
- # directories are located in the same place and are named by
- # corresponding usernames. If current user home directory points
- # to nonstandard place, this guess is likely wrong.
- if os.environ['USERNAME'] != username:
- drv, root, parts = self.parse_parts((userhome,))
- if parts[-1] != os.environ['USERNAME']:
- raise RuntimeError("Can't determine home directory "
- "for %r" % username)
- parts[-1] = username
- if drv or root:
- userhome = drv + root + self.join(parts[1:])
- else:
- userhome = self.join(parts)
- return userhome
class _PosixFlavour(_Flavour):
sep = '/'
@@ -364,21 +336,6 @@ class _PosixFlavour(_Flavour):
bpath = bytes(path)
return 'file://' + urlquote_from_bytes(bpath)
- def gethomedir(self, username):
- if not username:
- try:
- return os.environ['HOME']
- except KeyError:
- import pwd
- return pwd.getpwuid(os.getuid()).pw_dir
- else:
- import pwd
- try:
- return pwd.getpwnam(username).pw_dir
- except KeyError:
- raise RuntimeError("Can't determine home directory "
- "for %r" % username)
-
_windows_flavour = _WindowsFlavour()
_posix_flavour = _PosixFlavour()
@@ -463,6 +420,8 @@ class _NormalAccessor(_Accessor):
getcwd = os.getcwd
+ expanduser = staticmethod(os.path.expanduser)
+
_normal_accessor = _NormalAccessor()
@@ -1105,7 +1064,7 @@ class Path(PurePath):
"""Return a new path pointing to the user's home directory (as
returned by os.path.expanduser('~')).
"""
- return cls(cls()._flavour.gethomedir(None))
+ return cls("~").expanduser()
def samefile(self, other_path):
"""Return whether other_path is the same or not as this file
@@ -1517,7 +1476,9 @@ class Path(PurePath):
"""
if (not (self._drv or self._root) and
self._parts and self._parts[0][:1] == '~'):
- homedir = self._flavour.gethomedir(self._parts[0][1:])
+ homedir = self._accessor.expanduser(self._parts[0])
+ if homedir[:1] == "~":
+ raise RuntimeError("Could not determine home directory.")
return self._from_parts([homedir] + self._parts[1:])
return self