Skip to content

Commit 3f0ef20

Browse files
committed
#5471: fix expanduser() for $HOME set to "/".
1 parent a7ec072 commit 3f0ef20

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

Lib/posixpath.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ def expanduser(path):
262262
except KeyError:
263263
return path
264264
userhome = pwent.pw_dir
265-
userhome = userhome.rstrip('/')
265+
userhome = userhome.rstrip('/') or userhome
266266
return userhome + path[i:]
267267

268268

Lib/test/test_posixpath.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,11 @@ def test_expanduser(self):
345345
self.assert_(isinstance(posixpath.expanduser("~root/"), basestring))
346346
self.assert_(isinstance(posixpath.expanduser("~foo/"), basestring))
347347

348+
orig_home = os.environ['HOME']
349+
os.environ['HOME'] = '/'
350+
self.assertEqual(posixpath.expanduser("~"), "/")
351+
os.environ['HOME'] = orig_home
352+
348353
self.assertRaises(TypeError, posixpath.expanduser)
349354

350355
def test_expandvars(self):

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,8 @@ Core and Builtins
212212
Library
213213
-------
214214

215+
- Issue 5471: Fix os.path.expanduser() for $HOME set to '/'.
216+
215217
- Issue 1326077: fix the formatting of SyntaxErrors by the traceback module.
216218

217219
- Issue 1726172: fix IndexError in the case of and empty response in ftplib.

0 commit comments

Comments
 (0)