From 3f0ef20269aef0408a0a325bf5300a1ca676aa52 Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Sun, 5 Apr 2009 14:48:49 +0000 Subject: [PATCH] #5471: fix expanduser() for $HOME set to "/". --- Lib/posixpath.py | 2 +- Lib/test/test_posixpath.py | 5 +++++ Misc/NEWS | 2 ++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/Lib/posixpath.py b/Lib/posixpath.py index 6eb45fd0ead..0ec430ddf7c 100644 --- a/Lib/posixpath.py +++ b/Lib/posixpath.py @@ -262,7 +262,7 @@ def expanduser(path): except KeyError: return path userhome = pwent.pw_dir - userhome = userhome.rstrip('/') + userhome = userhome.rstrip('/') or userhome return userhome + path[i:] diff --git a/Lib/test/test_posixpath.py b/Lib/test/test_posixpath.py index 46ac0672631..e6f750a0c2e 100644 --- a/Lib/test/test_posixpath.py +++ b/Lib/test/test_posixpath.py @@ -345,6 +345,11 @@ class PosixPathTest(unittest.TestCase): self.assert_(isinstance(posixpath.expanduser("~root/"), basestring)) self.assert_(isinstance(posixpath.expanduser("~foo/"), basestring)) + orig_home = os.environ['HOME'] + os.environ['HOME'] = '/' + self.assertEqual(posixpath.expanduser("~"), "/") + os.environ['HOME'] = orig_home + self.assertRaises(TypeError, posixpath.expanduser) def test_expandvars(self): diff --git a/Misc/NEWS b/Misc/NEWS index bfcd9fa9114..fd35fad3120 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -212,6 +212,8 @@ Core and Builtins Library ------- +- Issue 5471: Fix os.path.expanduser() for $HOME set to '/'. + - Issue 1326077: fix the formatting of SyntaxErrors by the traceback module. - Issue 1726172: fix IndexError in the case of and empty response in ftplib.