From: Walter Dörwald Date: Thu, 19 Jun 2003 10:21:14 +0000 (+0000) Subject: Use find() instead of looping over the string in expanduser(). X-Git-Tag: v2.3c1~374 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=a9da5ae07aa40d834b9bfb71de8af0f1b68f39ba;p=python Use find() instead of looping over the string in expanduser(). From SF patch #757058. --- diff --git a/Lib/posixpath.py b/Lib/posixpath.py index 1c63af8d47..7f907ef519 100644 --- a/Lib/posixpath.py +++ b/Lib/posixpath.py @@ -303,11 +303,11 @@ def expanduser(path): do nothing.""" if not path.startswith('~'): return path - i, n = 1, len(path) - while i < n and path[i] != '/': - i += 1 + i = path.find('/', 1) + if i < 0: + i = len(path) if i == 1: - if not 'HOME' in os.environ: + if 'HOME' not in os.environ: import pwd userhome = pwd.getpwuid(os.getuid()).pw_dir else: diff --git a/Lib/test/test_posixpath.py b/Lib/test/test_posixpath.py index 9ba721621b..30551d8390 100644 --- a/Lib/test/test_posixpath.py +++ b/Lib/test/test_posixpath.py @@ -332,12 +332,16 @@ class PosixPathTest(unittest.TestCase): def test_expanduser(self): self.assertEqual(posixpath.expanduser("foo"), "foo") - self.assert_(isinstance(posixpath.expanduser("~/"), basestring)) try: import pwd except ImportError: pass else: + self.assert_(isinstance(posixpath.expanduser("~/"), basestring)) + self.assertEqual( + posixpath.expanduser("~") + "/", + posixpath.expanduser("~/") + ) self.assert_(isinstance(posixpath.expanduser("~root/"), basestring)) self.assert_(isinstance(posixpath.expanduser("~foo/"), basestring))