Closes #14768: os.path.expanduser('~/a') doesn't works correctly when HOME is '/'
diff --git a/Lib/posixpath.py b/Lib/posixpath.py
index aae38d5..163c00c 100644
--- a/Lib/posixpath.py
+++ b/Lib/posixpath.py
@@ -267,8 +267,8 @@
except KeyError:
return path
userhome = pwent.pw_dir
- userhome = userhome.rstrip('/') or userhome
- return userhome + path[i:]
+ userhome = userhome.rstrip('/')
+ return (userhome + path[i:]) or '/'
# Expand paths containing shell variable substitutions.
diff --git a/Lib/test/test_posixpath.py b/Lib/test/test_posixpath.py
index 8bb78d6..957a903 100644
--- a/Lib/test/test_posixpath.py
+++ b/Lib/test/test_posixpath.py
@@ -201,6 +201,7 @@
with test_support.EnvironmentVarGuard() as env:
env['HOME'] = '/'
self.assertEqual(posixpath.expanduser("~"), "/")
+ self.assertEqual(posixpath.expanduser("~/foo"), "/foo")
def test_normpath(self):
self.assertEqual(posixpath.normpath(""), ".")