]> granicus.if.org Git - python/commitdiff
Use find() instead of looping over the string in expanduser().
authorWalter Dörwald <walter@livinglogic.de>
Thu, 19 Jun 2003 10:21:14 +0000 (10:21 +0000)
committerWalter Dörwald <walter@livinglogic.de>
Thu, 19 Jun 2003 10:21:14 +0000 (10:21 +0000)
From SF patch #757058.

Lib/posixpath.py
Lib/test/test_posixpath.py

index 1c63af8d47ed9e629bcff8a55f188f8a574306ac..7f907ef519c1d48f4bd6228d7422993047b6e01a 100644 (file)
@@ -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:
index 9ba721621bd901b0b990e87c0dc121f5447b0ccd..30551d83900621ae2b9c9d65f12d60a8543ae6dd 100644 (file)
@@ -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))