comparison mercurial/util.py @ 14077:c285bdb0572a

util.url: copy urllib.unquote() into util to improve startup times The ui class uses util.hasscheme() in a couple of places, causing hg to import urllib even when it doesn't need to. This copies urllib.unquote() to avoid that import. perfstartup time before the URL refactoring (8796fb6af67e): ! wall 0.050692 comb 0.000000 user 0.000000 sys 0.000000 (best of 100) before this change: ! wall 0.064742 comb 0.000000 user 0.000000 sys 0.000000 (best of 100) after this change: ! wall 0.052126 comb 0.000000 user 0.000000 sys 0.000000 (best of 100
author Brodie Rao <brodie@bitheap.org>
date Sat, 30 Apr 2011 09:43:23 -0700
parents 924c82157d46
children 0824a0a3cefc
comparison
equal deleted inserted replaced
14076:924c82157d46 14077:c285bdb0572a
1282 1282
1283 If s is not a valid boolean, returns None. 1283 If s is not a valid boolean, returns None.
1284 """ 1284 """
1285 return _booleans.get(s.lower(), None) 1285 return _booleans.get(s.lower(), None)
1286 1286
1287 _hexdig = '0123456789ABCDEFabcdef'
1288 _hextochr = dict((a + b, chr(int(a + b, 16)))
1289 for a in _hexdig for b in _hexdig)
1290
1291 def _urlunquote(s):
1292 """unquote('abc%20def') -> 'abc def'."""
1293 res = s.split('%')
1294 # fastpath
1295 if len(res) == 1:
1296 return s
1297 s = res[0]
1298 for item in res[1:]:
1299 try:
1300 s += _hextochr[item[:2]] + item[2:]
1301 except KeyError:
1302 s += '%' + item
1303 except UnicodeDecodeError:
1304 s += unichr(int(item[:2], 16)) + item[2:]
1305 return s
1306
1287 class url(object): 1307 class url(object):
1288 """Reliable URL parser. 1308 """Reliable URL parser.
1289 1309
1290 This parses URLs and provides attributes for the following 1310 This parses URLs and provides attributes for the following
1291 components: 1311 components:
1425 1445
1426 for a in ('user', 'passwd', 'host', 'port', 1446 for a in ('user', 'passwd', 'host', 'port',
1427 'path', 'query', 'fragment'): 1447 'path', 'query', 'fragment'):
1428 v = getattr(self, a) 1448 v = getattr(self, a)
1429 if v is not None: 1449 if v is not None:
1430 setattr(self, a, urllib.unquote(v)) 1450 setattr(self, a, _urlunquote(v))
1431 1451
1432 def __repr__(self): 1452 def __repr__(self):
1433 attrs = [] 1453 attrs = []
1434 for a in ('scheme', 'user', 'passwd', 'host', 'port', 'path', 1454 for a in ('scheme', 'user', 'passwd', 'host', 'port', 'path',
1435 'query', 'fragment'): 1455 'query', 'fragment'):