mirror of
https://github.com/python/cpython.git
synced 2025-09-19 23:20:25 +00:00
Use find() instead of looping over the string in expanduser().
From SF patch #757058.
This commit is contained in:
parent
76ca1d428f
commit
a9da5ae07a
2 changed files with 9 additions and 5 deletions
|
@ -303,11 +303,11 @@ def expanduser(path):
|
||||||
do nothing."""
|
do nothing."""
|
||||||
if not path.startswith('~'):
|
if not path.startswith('~'):
|
||||||
return path
|
return path
|
||||||
i, n = 1, len(path)
|
i = path.find('/', 1)
|
||||||
while i < n and path[i] != '/':
|
if i < 0:
|
||||||
i += 1
|
i = len(path)
|
||||||
if i == 1:
|
if i == 1:
|
||||||
if not 'HOME' in os.environ:
|
if 'HOME' not in os.environ:
|
||||||
import pwd
|
import pwd
|
||||||
userhome = pwd.getpwuid(os.getuid()).pw_dir
|
userhome = pwd.getpwuid(os.getuid()).pw_dir
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -332,12 +332,16 @@ class PosixPathTest(unittest.TestCase):
|
||||||
|
|
||||||
def test_expanduser(self):
|
def test_expanduser(self):
|
||||||
self.assertEqual(posixpath.expanduser("foo"), "foo")
|
self.assertEqual(posixpath.expanduser("foo"), "foo")
|
||||||
self.assert_(isinstance(posixpath.expanduser("~/"), basestring))
|
|
||||||
try:
|
try:
|
||||||
import pwd
|
import pwd
|
||||||
except ImportError:
|
except ImportError:
|
||||||
pass
|
pass
|
||||||
else:
|
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("~root/"), basestring))
|
||||||
self.assert_(isinstance(posixpath.expanduser("~foo/"), basestring))
|
self.assert_(isinstance(posixpath.expanduser("~foo/"), basestring))
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue