diff options
| author | Aymeric Augustin <aymeric.augustin@m4x.org> | 2012-07-20 15:36:52 +0200 |
|---|---|---|
| committer | Aymeric Augustin <aymeric.augustin@m4x.org> | 2012-07-22 09:29:55 +0200 |
| commit | 0d914d08a0d7b5a1521f498a8047971fe6cd61e7 (patch) | |
| tree | d0ab08b0b5b2041bd796c10a26a358ae60d0914a /django/utils | |
| parent | bdca5ea345c548a82a80d198906818c9ccbef896 (diff) | |
[py3] Updated urllib/urllib2/urlparse imports.
Lots of functions were moved. Use explicit imports in all cases
to keey it easy to identify where the functions come from.
Diffstat (limited to 'django/utils')
| -rw-r--r-- | django/utils/encoding.py | 13 | ||||
| -rw-r--r-- | django/utils/feedgenerator.py | 7 | ||||
| -rw-r--r-- | django/utils/html.py | 13 | ||||
| -rw-r--r-- | django/utils/http.py | 22 |
4 files changed, 35 insertions, 20 deletions
diff --git a/django/utils/encoding.py b/django/utils/encoding.py index 789e709da5..f2295444bf 100644 --- a/django/utils/encoding.py +++ b/django/utils/encoding.py @@ -1,10 +1,13 @@ from __future__ import unicode_literals -import urllib -import locale -import datetime import codecs +import datetime from decimal import Decimal +import locale +try: + from urllib.parse import quote +except ImportError: # Python 2 + from urllib import quote from django.utils.functional import Promise from django.utils import six @@ -165,7 +168,7 @@ def iri_to_uri(iri): # converted. if iri is None: return iri - return urllib.quote(smart_str(iri), safe=b"/#%[]=:;$&()+,!?*@'~") + return quote(smart_str(iri), safe=b"/#%[]=:;$&()+,!?*@'~") def filepath_to_uri(path): """Convert an file system path to a URI portion that is suitable for @@ -184,7 +187,7 @@ def filepath_to_uri(path): return path # I know about `os.sep` and `os.altsep` but I want to leave # some flexibility for hardcoding separators. - return urllib.quote(smart_str(path).replace("\\", "/"), safe=b"/~!*()'") + return quote(smart_str(path).replace("\\", "/"), safe=b"/~!*()'") # The encoding of the default system locale but falls back to the # given fallback encoding if the encoding is unsupported by python or could diff --git a/django/utils/feedgenerator.py b/django/utils/feedgenerator.py index 3dc66a68c6..6498aaf57c 100644 --- a/django/utils/feedgenerator.py +++ b/django/utils/feedgenerator.py @@ -24,7 +24,10 @@ http://web.archive.org/web/20110718035220/http://diveintomark.org/archives/2004/ from __future__ import unicode_literals import datetime -import urlparse +try: + from urllib.parse import urlparse +except ImportError: # Python 2 + from urlparse import urlparse from django.utils.xmlutils import SimplerXMLGenerator from django.utils.encoding import force_unicode, iri_to_uri from django.utils import datetime_safe @@ -67,7 +70,7 @@ def get_tag_uri(url, date): See http://web.archive.org/web/20110514113830/http://diveintomark.org/archives/2004/05/28/howto-atom-id """ - bits = urlparse.urlparse(url) + bits = urlparse(url) d = '' if date is not None: d = ',%s' % datetime_safe.new_datetime(date).strftime('%Y-%m-%d') diff --git a/django/utils/html.py b/django/utils/html.py index d5881996d9..7e35fdecb8 100644 --- a/django/utils/html.py +++ b/django/utils/html.py @@ -4,8 +4,11 @@ from __future__ import unicode_literals import re import string -import urllib -import urlparse +try: + from urllib.parse import quote, urlsplit, urlunsplit +except ImportError: # Python 2 + from urllib import quote + from urlparse import urlsplit, urlunsplit from django.utils.safestring import SafeData, mark_safe from django.utils.encoding import smart_str, force_unicode @@ -138,19 +141,19 @@ fix_ampersands = allow_lazy(fix_ampersands, six.text_type) def smart_urlquote(url): "Quotes a URL if it isn't already quoted." # Handle IDN before quoting. - scheme, netloc, path, query, fragment = urlparse.urlsplit(url) + scheme, netloc, path, query, fragment = urlsplit(url) try: netloc = netloc.encode('idna') # IDN -> ACE except UnicodeError: # invalid domain part pass else: - url = urlparse.urlunsplit((scheme, netloc, path, query, fragment)) + url = urlunsplit((scheme, netloc, path, query, fragment)) # An URL is considered unquoted if it contains no % characters or # contains a % not followed by two hexadecimal digits. See #9655. if '%' not in url or unquoted_percents_re.search(url): # See http://bugs.python.org/issue2637 - url = urllib.quote(smart_str(url), safe=b'!*\'();:@&=+$,/?#[]~') + url = quote(smart_str(url), safe=b'!*\'();:@&=+$,/?#[]~') return force_unicode(url) diff --git a/django/utils/http.py b/django/utils/http.py index ec94d62903..f3a3dce58c 100644 --- a/django/utils/http.py +++ b/django/utils/http.py @@ -2,8 +2,14 @@ import calendar import datetime import re import sys -import urllib -import urlparse +try: + from urllib import parse as urllib_parse +except ImportError: # Python 2 + import urllib as urllib_parse + import urlparse + urllib_parse.urlparse = urlparse.urlparse + + from email.utils import formatdate from django.utils.datastructures import MultiValueDict @@ -31,7 +37,7 @@ def urlquote(url, safe='/'): can safely be used as part of an argument to a subsequent iri_to_uri() call without double-quoting occurring. """ - return force_unicode(urllib.quote(smart_str(url), smart_str(safe))) + return force_unicode(urllib_parse.quote(smart_str(url), smart_str(safe))) urlquote = allow_lazy(urlquote, six.text_type) def urlquote_plus(url, safe=''): @@ -41,7 +47,7 @@ def urlquote_plus(url, safe=''): returned string can safely be used as part of an argument to a subsequent iri_to_uri() call without double-quoting occurring. """ - return force_unicode(urllib.quote_plus(smart_str(url), smart_str(safe))) + return force_unicode(urllib_parse.quote_plus(smart_str(url), smart_str(safe))) urlquote_plus = allow_lazy(urlquote_plus, six.text_type) def urlunquote(quoted_url): @@ -49,7 +55,7 @@ def urlunquote(quoted_url): A wrapper for Python's urllib.unquote() function that can operate on the result of django.utils.http.urlquote(). """ - return force_unicode(urllib.unquote(smart_str(quoted_url))) + return force_unicode(urllib_parse.unquote(smart_str(quoted_url))) urlunquote = allow_lazy(urlunquote, six.text_type) def urlunquote_plus(quoted_url): @@ -57,7 +63,7 @@ def urlunquote_plus(quoted_url): A wrapper for Python's urllib.unquote_plus() function that can operate on the result of django.utils.http.urlquote_plus(). """ - return force_unicode(urllib.unquote_plus(smart_str(quoted_url))) + return force_unicode(urllib_parse.unquote_plus(smart_str(quoted_url))) urlunquote_plus = allow_lazy(urlunquote_plus, six.text_type) def urlencode(query, doseq=0): @@ -70,7 +76,7 @@ def urlencode(query, doseq=0): query = query.lists() elif hasattr(query, 'items'): query = query.items() - return urllib.urlencode( + return urllib_parse.urlencode( [(smart_str(k), [smart_str(i) for i in v] if isinstance(v, (list,tuple)) else smart_str(v)) for k, v in query], @@ -212,5 +218,5 @@ def same_origin(url1, url2): """ Checks if two URLs are 'same-origin' """ - p1, p2 = urlparse.urlparse(url1), urlparse.urlparse(url2) + p1, p2 = urllib_parse.urlparse(url1), urllib_parse.urlparse(url2) return (p1.scheme, p1.hostname, p1.port) == (p2.scheme, p2.hostname, p2.port) |
