Commit 3f91379e by Tom Christie

Fix 1.3 compat issue. Closes #780

parent c2280e34
...@@ -400,19 +400,23 @@ except ImportError: ...@@ -400,19 +400,23 @@ except ImportError:
try: try:
from django.utils.html import smart_urlquote from django.utils.html import smart_urlquote
except ImportError: except ImportError:
import re
from django.utils.encoding import smart_str
try: try:
from urllib.parse import quote, urlsplit, urlunsplit from urllib.parse import quote, urlsplit, urlunsplit
except ImportError: # Python 2 except ImportError: # Python 2
from urllib import quote from urllib import quote
from urlparse import urlsplit, urlunsplit from urlparse import urlsplit, urlunsplit
unquoted_percents_re = re.compile(r'%(?![0-9A-Fa-f]{2})')
def smart_urlquote(url): def smart_urlquote(url):
"Quotes a URL if it isn't already quoted." "Quotes a URL if it isn't already quoted."
# Handle IDN before quoting. # Handle IDN before quoting.
scheme, netloc, path, query, fragment = urlsplit(url) scheme, netloc, path, query, fragment = urlsplit(url)
try: try:
netloc = netloc.encode('idna').decode('ascii') # IDN -> ACE netloc = netloc.encode('idna').decode('ascii') # IDN -> ACE
except UnicodeError: # invalid domain part except UnicodeError: # invalid domain part
pass pass
else: else:
url = urlunsplit((scheme, netloc, path, query, fragment)) url = urlunsplit((scheme, netloc, path, query, fragment))
...@@ -421,7 +425,7 @@ except ImportError: ...@@ -421,7 +425,7 @@ except ImportError:
# contains a % not followed by two hexadecimal digits. See #9655. # contains a % not followed by two hexadecimal digits. See #9655.
if '%' not in url or unquoted_percents_re.search(url): if '%' not in url or unquoted_percents_re.search(url):
# See http://bugs.python.org/issue2637 # See http://bugs.python.org/issue2637
url = quote(force_bytes(url), safe=b'!*\'();:@&=+$,/?#[]~') url = quote(smart_str(url), safe=b'!*\'();:@&=+$,/?#[]~')
return force_text(url) return force_text(url)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment