@@ -400,19 +400,23 @@ def parse_datetime(value):
400
400
try :
401
401
from django .utils .html import smart_urlquote
402
402
except ImportError :
403
+ import re
404
+ from django .utils .encoding import smart_str
403
405
try :
404
406
from urllib .parse import quote , urlsplit , urlunsplit
405
407
except ImportError : # Python 2
406
408
from urllib import quote
407
409
from urlparse import urlsplit , urlunsplit
408
410
411
+ unquoted_percents_re = re .compile (r'%(?![0-9A-Fa-f]{2})' )
412
+
409
413
def smart_urlquote (url ):
410
414
"Quotes a URL if it isn't already quoted."
411
415
# Handle IDN before quoting.
412
416
scheme , netloc , path , query , fragment = urlsplit (url )
413
417
try :
414
- netloc = netloc .encode ('idna' ).decode ('ascii' ) # IDN -> ACE
415
- except UnicodeError : # invalid domain part
418
+ netloc = netloc .encode ('idna' ).decode ('ascii' ) # IDN -> ACE
419
+ except UnicodeError : # invalid domain part
416
420
pass
417
421
else :
418
422
url = urlunsplit ((scheme , netloc , path , query , fragment ))
@@ -421,7 +425,7 @@ def smart_urlquote(url):
421
425
# contains a % not followed by two hexadecimal digits. See #9655.
422
426
if '%' not in url or unquoted_percents_re .search (url ):
423
427
# See http://bugs.python.org/issue2637
424
- url = quote (force_bytes (url ), safe = b'!*\' ();:@&=+$,/?#[]~' )
428
+ url = quote (smart_str (url ), safe = b'!*\' ();:@&=+$,/?#[]~' )
425
429
426
430
return force_text (url )
427
431
0 commit comments