aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/posixpath.py
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@redhat.com>2018-12-05 16:49:35 +0100
committerGitHub <noreply@github.com>2018-12-05 16:49:35 +0100
commitf2f4555d8287ad217a1dba7bbd93103ad4daf3a8 (patch)
treed3c3dfaee0aa56df5cdf4d9d60db7b65c3e33db4 /Lib/posixpath.py
parent398bd27967690f2c1a8cbf8d47a5613edd9cfb2a (diff)
downloadcpython-f2f4555d8287ad217a1dba7bbd93103ad4daf3a8.tar.gz
cpython-f2f4555d8287ad217a1dba7bbd93103ad4daf3a8.zip
bpo-10496: posixpath.expanduser() catchs pwd.getpwuid() error (GH-10919)
* posixpath.expanduser() now returns the input path unchanged if the HOME environment variable is not set and pwd.getpwuid() raises KeyError (the current user identifier doesn't exist in the password database). * Add test_no_home_directory() to test_site.
Diffstat (limited to 'Lib/posixpath.py')
-rw-r--r--Lib/posixpath.py9
1 files changed, 8 insertions, 1 deletions
diff --git a/Lib/posixpath.py b/Lib/posixpath.py
index 7e3f3db4b6d..4914a1728ab 100644
--- a/Lib/posixpath.py
+++ b/Lib/posixpath.py
@@ -246,7 +246,12 @@ def expanduser(path):
if i == 1:
if 'HOME' not in os.environ:
import pwd
- userhome = pwd.getpwuid(os.getuid()).pw_dir
+ try:
+ userhome = pwd.getpwuid(os.getuid()).pw_dir
+ except KeyError:
+ # bpo-10496: if the current user identifier doesn't exist in the
+ # password database, return the path unchanged
+ return path
else:
userhome = os.environ['HOME']
else:
@@ -257,6 +262,8 @@ def expanduser(path):
try:
pwent = pwd.getpwnam(name)
except KeyError:
+ # bpo-10496: if the user name from the path doesn't exist in the
+ # password database, return the path unchanged
return path
userhome = pwent.pw_dir
if isinstance(path, bytes):