Mercurial > hg
comparison mercurial/url.py @ 11035:e4f911ce21de stable
schemes: fix // breakage with Python 2.6.5 (issue2111)
Recent Pythons (e.g. 2.6.5 and 3.1) introduce a change that causes
urlparse.urlunparse(urlparse.urlparse('x://')) to return 'x:' instead of 'x://'i and
urlparse.urlunparse(urlparse.urlparse('x:///y')) to return 'x:/y' instead of 'x:///y'.
Fix url.hidepassword() and url.removeauth() to handle these cases.
author | Michael Glassford <glassfordmjg@gmail.com> |
---|---|
date | Thu, 08 Apr 2010 11:00:46 -0400 |
parents | 1148a968a070 |
children | a1e575b48563 |
comparison
equal
deleted
inserted
replaced
11034:3b89899934a6 | 11035:e4f911ce21de |
---|---|
9 | 9 |
10 import urllib, urllib2, urlparse, httplib, os, re, socket, cStringIO | 10 import urllib, urllib2, urlparse, httplib, os, re, socket, cStringIO |
11 from i18n import _ | 11 from i18n import _ |
12 import keepalive, util | 12 import keepalive, util |
13 | 13 |
14 def _urlunparse(scheme, netloc, path, params, query, fragment, url): | |
15 '''Handle cases where urlunparse(urlparse(x://)) doesn't preserve the "//"''' | |
16 result = urlparse.urlunparse((scheme, netloc, path, params, query, fragment)) | |
17 if (scheme and | |
18 result.startswith(scheme + ':') and | |
19 not result.startswith(scheme + '://') and | |
20 url.startswith(scheme + '://') | |
21 ): | |
22 result = scheme + '://' + result[len(scheme + ':'):] | |
23 return result | |
24 | |
14 def hidepassword(url): | 25 def hidepassword(url): |
15 '''hide user credential in a url string''' | 26 '''hide user credential in a url string''' |
16 scheme, netloc, path, params, query, fragment = urlparse.urlparse(url) | 27 scheme, netloc, path, params, query, fragment = urlparse.urlparse(url) |
17 netloc = re.sub('([^:]*):([^@]*)@(.*)', r'\1:***@\3', netloc) | 28 netloc = re.sub('([^:]*):([^@]*)@(.*)', r'\1:***@\3', netloc) |
18 return urlparse.urlunparse((scheme, netloc, path, params, query, fragment)) | 29 return _urlunparse(scheme, netloc, path, params, query, fragment, url) |
19 | 30 |
20 def removeauth(url): | 31 def removeauth(url): |
21 '''remove all authentication information from a url string''' | 32 '''remove all authentication information from a url string''' |
22 scheme, netloc, path, params, query, fragment = urlparse.urlparse(url) | 33 scheme, netloc, path, params, query, fragment = urlparse.urlparse(url) |
23 netloc = netloc[netloc.find('@')+1:] | 34 netloc = netloc[netloc.find('@')+1:] |
24 return urlparse.urlunparse((scheme, netloc, path, params, query, fragment)) | 35 return _urlunparse(scheme, netloc, path, params, query, fragment, url) |
25 | 36 |
26 def netlocsplit(netloc): | 37 def netlocsplit(netloc): |
27 '''split [user[:passwd]@]host[:port] into 4-tuple.''' | 38 '''split [user[:passwd]@]host[:port] into 4-tuple.''' |
28 | 39 |
29 a = netloc.find('@') | 40 a = netloc.find('@') |