diff --git a/Lib/posixpath.py b/Lib/posixpath.py index c9829e18d04..9570a364a00 100644 --- a/Lib/posixpath.py +++ b/Lib/posixpath.py @@ -266,8 +266,8 @@ def expanduser(path): root = b'/' else: root = '/' - userhome = userhome.rstrip(root) or userhome - return userhome + path[i:] + userhome = userhome.rstrip(root) + return (userhome + path[i:]) or root # Expand paths containing shell variable substitutions. diff --git a/Lib/test/test_posixpath.py b/Lib/test/test_posixpath.py index 709ef040fad..daebeaa8ee4 100644 --- a/Lib/test/test_posixpath.py +++ b/Lib/test/test_posixpath.py @@ -300,6 +300,7 @@ class PosixPathTest(unittest.TestCase): with support.EnvironmentVarGuard() as env: env['HOME'] = '/' self.assertEqual(posixpath.expanduser("~"), "/") + self.assertEqual(posixpath.expanduser("~/foo"), "/foo") # expanduser should fall back to using the password database del env['HOME'] home = pwd.getpwuid(os.getuid()).pw_dir diff --git a/Misc/ACKS b/Misc/ACKS index 6e38038bce2..c7dc2f4098c 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -335,6 +335,7 @@ Andrew Francis Martin Franklin Bruce Frederiksen Robin Friedrich +Bradley Froehle Ivan Frohne Matthias Fuchs Jim Fulton diff --git a/Misc/NEWS b/Misc/NEWS index 0850336c90f..9109bf4ac0a 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -139,6 +139,8 @@ Library - Issue #13815: TarFile.extractfile() now returns io.BufferedReader objects. +- Issue #14768: os.path.expanduser('~/a') doesn't works correctly when HOME is '/'. + - Issue #14371: Support bzip2 in zipfile module. Patch by Serhiy Storchaka. - Issue #13183: Fix pdb skipping frames after hitting a breakpoint and running