mirror of
https://github.com/python/cpython.git
synced 2025-09-27 10:50:04 +00:00
Issue 8143: sync unquote in urlparse with urllib; add comment about doing so.
unquote is duplicated in the two files to avoid a circular reference. (This is fixed in Python3.) Updates keep getting made to the public unquote without fixing the urlparse one, however, so this fix syncs the two and adds a comment to both to make sure changes are applied to both.
This commit is contained in:
parent
b64c89bd7a
commit
bfbdefe539
2 changed files with 18 additions and 10 deletions
|
@ -1156,6 +1156,10 @@ def splitvalue(attr):
|
||||||
if match: return match.group(1, 2)
|
if match: return match.group(1, 2)
|
||||||
return attr, None
|
return attr, None
|
||||||
|
|
||||||
|
# urlparse contains a duplicate of this method to avoid a circular import. If
|
||||||
|
# you update this method, also update the copy in urlparse. This code
|
||||||
|
# duplication does not exist in Python3.
|
||||||
|
|
||||||
_hexdig = '0123456789ABCDEFabcdef'
|
_hexdig = '0123456789ABCDEFabcdef'
|
||||||
_hextochr = dict((a + b, chr(int(a + b, 16)))
|
_hextochr = dict((a + b, chr(int(a + b, 16)))
|
||||||
for a in _hexdig for b in _hexdig)
|
for a in _hexdig for b in _hexdig)
|
||||||
|
|
|
@ -301,25 +301,29 @@ def urldefrag(url):
|
||||||
return url, ''
|
return url, ''
|
||||||
|
|
||||||
# unquote method for parse_qs and parse_qsl
|
# unquote method for parse_qs and parse_qsl
|
||||||
# Cannot use directly from urllib as it would create circular reference.
|
# Cannot use directly from urllib as it would create a circular reference
|
||||||
# urllib uses urlparse methods ( urljoin)
|
# because urllib uses urlparse methods (urljoin). If you update this function,
|
||||||
|
# update it also in urllib. This code duplication does not existin in Python3.
|
||||||
|
|
||||||
_hexdig = '0123456789ABCDEFabcdef'
|
_hexdig = '0123456789ABCDEFabcdef'
|
||||||
_hextochr = dict((a+b, chr(int(a+b,16))) for a in _hexdig for b in _hexdig)
|
_hextochr = dict((a+b, chr(int(a+b,16)))
|
||||||
|
for a in _hexdig for b in _hexdig)
|
||||||
|
|
||||||
def unquote(s):
|
def unquote(s):
|
||||||
"""unquote('abc%20def') -> 'abc def'."""
|
"""unquote('abc%20def') -> 'abc def'."""
|
||||||
res = s.split('%')
|
res = s.split('%')
|
||||||
for i in xrange(1, len(res)):
|
# fastpath
|
||||||
item = res[i]
|
if len(res) == 1:
|
||||||
|
return s
|
||||||
|
s = res[0]
|
||||||
|
for item in res[1:]:
|
||||||
try:
|
try:
|
||||||
res[i] = _hextochr[item[:2]] + item[2:]
|
s += _hextochr[item[:2]] + item[2:]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
res[i] = '%' + item
|
s += '%' + item
|
||||||
except UnicodeDecodeError:
|
except UnicodeDecodeError:
|
||||||
res[i] = unichr(int(item[:2], 16)) + item[2:]
|
s += unichr(int(item[:2], 16)) + item[2:]
|
||||||
return "".join(res)
|
return s
|
||||||
|
|
||||||
def parse_qs(qs, keep_blank_values=0, strict_parsing=0):
|
def parse_qs(qs, keep_blank_values=0, strict_parsing=0):
|
||||||
"""Parse a query given as a string argument.
|
"""Parse a query given as a string argument.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue