Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[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.
  • Loading branch information...
commit 0d914d08a0d7b5a1521f498a8047971fe6cd61e7 1 parent bdca5ea
Aymeric Augustin aaugustin authored
Showing with 181 additions and 96 deletions.
  1. +6 −4 django/contrib/auth/decorators.py
  2. +2 −4 django/contrib/auth/models.py
  3. +5 −5 django/contrib/auth/tests/views.py
  4. +8 −5 django/contrib/auth/views.py
  5. +6 −2 django/contrib/comments/views/utils.py
  6. +2 −4 django/contrib/contenttypes/tests.py
  7. +4 −4 django/contrib/databrowse/plugins/fieldchoices.py
  8. +6 −2 django/contrib/databrowse/plugins/objects.py
  9. +7 −3 django/contrib/sitemaps/__init__.py
  10. +7 −3 django/contrib/staticfiles/handlers.py
  11. +5 −2 django/contrib/staticfiles/storage.py
  12. +5 −2 django/contrib/staticfiles/views.py
  13. +4 −1 django/core/cache/__init__.py
  14. +5 −2 django/core/files/storage.py
  15. +5 −3 django/core/management/templates.py
  16. +7 −4 django/core/servers/basehttp.py
  17. +6 −3 django/core/validators.py
  18. +7 −4 django/forms/fields.py
  19. +4 −1 django/forms/widgets.py
  20. +5 −2 django/http/__init__.py
  21. +5 −1 django/templatetags/static.py
  22. +7 −4 django/test/client.py
  23. +4 −1 django/test/testcases.py
  24. +8 −5 django/utils/encoding.py
  25. +5 −2 django/utils/feedgenerator.py
  26. +8 −5 django/utils/html.py
  27. +14 −8 django/utils/http.py
  28. +5 −2 django/views/static.py
  29. +4 −2 tests/modeltests/test_client/views.py
  30. +5 −2 tests/regressiontests/admin_views/tests.py
  31. +6 −3 tests/regressiontests/servers/tests.py
  32. +4 −1 tests/regressiontests/templates/tests.py
10 django/contrib/auth/decorators.py
View
@@ -1,4 +1,7 @@
-import urlparse
+try:
+ from urllib.parse import urlparse
+except ImportError: # Python 2
+ from urlparse import urlparse
from functools import wraps
from django.conf import settings
from django.contrib.auth import REDIRECT_FIELD_NAME
@@ -21,9 +24,8 @@ def _wrapped_view(request, *args, **kwargs):
path = request.build_absolute_uri()
# If the login url is the same scheme and net location then just
# use the path as the "next" url.
- login_scheme, login_netloc = urlparse.urlparse(login_url or
- settings.LOGIN_URL)[:2]
- current_scheme, current_netloc = urlparse.urlparse(path)[:2]
+ login_scheme, login_netloc = urlparse(login_url or settings.LOGIN_URL)[:2]
+ current_scheme, current_netloc = urlparse(path)[:2]
if ((not login_scheme or login_scheme == current_scheme) and
(not login_netloc or login_netloc == current_netloc)):
path = request.get_full_path()
6 django/contrib/auth/models.py
View
@@ -1,13 +1,11 @@
from __future__ import unicode_literals
-import urllib
-
from django.core.exceptions import ImproperlyConfigured
from django.core.mail import send_mail
from django.db import models
from django.db.models.manager import EmptyManager
from django.utils.crypto import get_random_string
-from django.utils.encoding import smart_str
+from django.utils.http import urlquote
from django.utils import six
from django.utils.translation import ugettext_lazy as _
from django.utils import timezone
@@ -268,7 +266,7 @@ def natural_key(self):
return (self.username,)
def get_absolute_url(self):
- return "/users/%s/" % urllib.quote(smart_str(self.username))
+ return "/users/%s/" % urlquote(self.username)
def is_anonymous(self):
"""
10 django/contrib/auth/tests/views.py
View
@@ -1,6 +1,5 @@
import os
import re
-import urllib
from django.conf import settings
from django.contrib.sites.models import Site, RequestSite
@@ -10,6 +9,7 @@
from django.http import QueryDict
from django.utils.encoding import force_unicode
from django.utils.html import escape
+from django.utils.http import urlquote
from django.test import TestCase
from django.test.utils import override_settings
@@ -256,7 +256,7 @@ def test_security_check(self, password='password'):
nasty_url = '%(url)s?%(next)s=%(bad_url)s' % {
'url': login_url,
'next': REDIRECT_FIELD_NAME,
- 'bad_url': urllib.quote(bad_url),
+ 'bad_url': urlquote(bad_url),
}
response = self.client.post(nasty_url, {
'username': 'testclient',
@@ -277,7 +277,7 @@ def test_security_check(self, password='password'):
safe_url = '%(url)s?%(next)s=%(good_url)s' % {
'url': login_url,
'next': REDIRECT_FIELD_NAME,
- 'good_url': urllib.quote(good_url),
+ 'good_url': urlquote(good_url),
}
response = self.client.post(safe_url, {
'username': 'testclient',
@@ -412,7 +412,7 @@ def test_security_check(self, password='password'):
nasty_url = '%(url)s?%(next)s=%(bad_url)s' % {
'url': logout_url,
'next': REDIRECT_FIELD_NAME,
- 'bad_url': urllib.quote(bad_url),
+ 'bad_url': urlquote(bad_url),
}
self.login()
response = self.client.get(nasty_url)
@@ -432,7 +432,7 @@ def test_security_check(self, password='password'):
safe_url = '%(url)s?%(next)s=%(good_url)s' % {
'url': logout_url,
'next': REDIRECT_FIELD_NAME,
- 'good_url': urllib.quote(good_url),
+ 'good_url': urlquote(good_url),
}
self.login()
response = self.client.get(safe_url)
13 django/contrib/auth/views.py
View
@@ -1,4 +1,7 @@
-import urlparse
+try:
+ from urllib.parse import urlparse, urlunparse
+except ImportError: # Python 2
+ from urlparse import urlparse, urlunparse
from django.conf import settings
from django.core.urlresolvers import reverse
@@ -34,7 +37,7 @@ def login(request, template_name='registration/login.html',
if request.method == "POST":
form = authentication_form(data=request.POST)
if form.is_valid():
- netloc = urlparse.urlparse(redirect_to)[1]
+ netloc = urlparse(redirect_to)[1]
# Use default setting if redirect_to is empty
if not redirect_to:
@@ -80,7 +83,7 @@ def logout(request, next_page=None,
auth_logout(request)
redirect_to = request.REQUEST.get(redirect_field_name, '')
if redirect_to:
- netloc = urlparse.urlparse(redirect_to)[1]
+ netloc = urlparse(redirect_to)[1]
# Security check -- don't allow redirection to a different host.
if not (netloc and netloc != request.get_host()):
return HttpResponseRedirect(redirect_to)
@@ -116,13 +119,13 @@ def redirect_to_login(next, login_url=None,
if not login_url:
login_url = settings.LOGIN_URL
- login_url_parts = list(urlparse.urlparse(login_url))
+ login_url_parts = list(urlparse(login_url))
if redirect_field_name:
querystring = QueryDict(login_url_parts[4], mutable=True)
querystring[redirect_field_name] = next
login_url_parts[4] = querystring.urlencode(safe='/')
- return HttpResponseRedirect(urlparse.urlunparse(login_url_parts))
+ return HttpResponseRedirect(urlunparse(login_url_parts))
# 4 views for password reset:
# - password_reset sends the mail
8 django/contrib/comments/views/utils.py
View
@@ -2,8 +2,12 @@
A few bits of helper functions for comment views.
"""
-import urllib
import textwrap
+try:
+ from urllib.parse import urlencode
+except ImportError: # Python 2
+ from urllib import urlencode
+
from django.http import HttpResponseRedirect
from django.core import urlresolvers
from django.shortcuts import render_to_response
@@ -33,7 +37,7 @@ def next_redirect(data, default, default_view, **get_kwargs):
anchor = ''
joiner = ('?' in next) and '&' or '?'
- next += joiner + urllib.urlencode(get_kwargs) + anchor
+ next += joiner + urlencode(get_kwargs) + anchor
return HttpResponseRedirect(next)
def confirmation_view(template, doc="Display a confirmation view."):
6 django/contrib/contenttypes/tests.py
View
@@ -1,14 +1,12 @@
from __future__ import unicode_literals
-import urllib
-
from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.views import shortcut
from django.contrib.sites.models import Site
from django.http import HttpRequest, Http404
from django.test import TestCase
-from django.utils.encoding import smart_str
+from django.utils.http import urlquote
from django.utils import six
@@ -36,7 +34,7 @@ class FooWithUrl(FooWithoutUrl):
"""
def get_absolute_url(self):
- return "/users/%s/" % urllib.quote(smart_str(self.name))
+ return "/users/%s/" % urlquote(self.name)
class FooWithBrokenAbsoluteUrl(FooWithoutUrl):
"""
8 django/contrib/databrowse/plugins/fieldchoices.py
View
@@ -6,9 +6,10 @@
from django.contrib.databrowse.sites import DatabrowsePlugin
from django.shortcuts import render_to_response
from django.utils.html import format_html, format_html_join
+from django.utils.http import urlquote
from django.utils.text import capfirst
-from django.utils.encoding import smart_str, force_unicode
-import urllib
+from django.utils.encoding import force_unicode
+
class FieldChoicePlugin(DatabrowsePlugin):
def __init__(self, field_filter=None):
@@ -38,11 +39,10 @@ def model_index_html(self, request, model, site):
def urls(self, plugin_name, easy_instance_field):
if easy_instance_field.field in self.field_dict(easy_instance_field.model.model).values():
- field_value = smart_str(easy_instance_field.raw_value)
return ['%s%s/%s/%s/' % (
easy_instance_field.model.url(),
plugin_name, easy_instance_field.field.name,
- urllib.quote(field_value, safe=''))]
+ urlquote(easy_instance_field.raw_value, safe=''))]
def model_view(self, request, model_databrowse, url):
self.model, self.site = model_databrowse.model, model_databrowse.site
8 django/contrib/databrowse/plugins/objects.py
View
@@ -1,14 +1,18 @@
+try:
+ from urllib.parse import urljoin
+except ImportError: # Python 2
+ from urlparse import urljoin
+
from django import http
from django.contrib.databrowse.datastructures import EasyModel
from django.contrib.databrowse.sites import DatabrowsePlugin
from django.shortcuts import render_to_response
-import urlparse
class ObjectDetailPlugin(DatabrowsePlugin):
def model_view(self, request, model_databrowse, url):
# If the object ID wasn't provided, redirect to the model page, which is one level up.
if url is None:
- return http.HttpResponseRedirect(urlparse.urljoin(request.path, '../'))
+ return http.HttpResponseRedirect(urljoin(request.path, '../'))
easy_model = EasyModel(model_databrowse.site, model_databrowse.model)
obj = easy_model.object_by_pk(url)
return render_to_response('databrowse/object_detail.html', {'object': obj, 'root_url': model_databrowse.site.root_url})
10 django/contrib/sitemaps/__init__.py
View
@@ -1,7 +1,11 @@
from django.contrib.sites.models import Site
from django.core import urlresolvers, paginator
from django.core.exceptions import ImproperlyConfigured
-import urllib
+try:
+ from urllib.parse import urlencode
+ from urllib.request import urlopen
+except ImportError: # Python 2
+ from urllib import urlencode, urlopen
PING_URL = "http://www.google.com/webmasters/tools/ping"
@@ -32,8 +36,8 @@ def ping_google(sitemap_url=None, ping_url=PING_URL):
from django.contrib.sites.models import Site
current_site = Site.objects.get_current()
url = "http://%s%s" % (current_site.domain, sitemap_url)
- params = urllib.urlencode({'sitemap':url})
- urllib.urlopen("%s?%s" % (ping_url, params))
+ params = urlencode({'sitemap':url})
+ urlopen("%s?%s" % (ping_url, params))
class Sitemap(object):
# This limit is defined by Google. See the index documentation at
10 django/contrib/staticfiles/handlers.py
View
@@ -1,5 +1,9 @@
-import urllib
-from urlparse import urlparse
+try:
+ from urllib.parse import urlparse
+ from urllib.request import url2pathname
+except ImportError: # Python 2
+ from urllib import url2pathname
+ from urlparse import urlparse
from django.conf import settings
from django.core.handlers.wsgi import WSGIHandler
@@ -42,7 +46,7 @@ def file_path(self, url):
Returns the relative path to the media file on disk for the given URL.
"""
relative_url = url[len(self.base_url[2]):]
- return urllib.url2pathname(relative_url)
+ return url2pathname(relative_url)
def serve(self, request):
"""
7 django/contrib/staticfiles/storage.py
View
@@ -3,8 +3,11 @@
import os
import posixpath
import re
-from urllib import unquote
-from urlparse import urlsplit, urlunsplit, urldefrag
+try:
+ from urllib.parse import unquote, urlsplit, urlunsplit, urldefrag
+except ImportError: # Python 2
+ from urllib import unquote
+ from urlparse import urlsplit, urlunsplit, urldefrag
from django.conf import settings
from django.core.cache import (get_cache, InvalidCacheBackendError,
7 django/contrib/staticfiles/views.py
View
@@ -5,7 +5,10 @@
"""
import os
import posixpath
-import urllib
+try:
+ from urllib.parse import unquote
+except ImportError: # Python 2
+ from urllib import unquote
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
@@ -31,7 +34,7 @@ def serve(request, path, document_root=None, insecure=False, **kwargs):
raise ImproperlyConfigured("The staticfiles view can only be used in "
"debug mode or if the the --insecure "
"option of 'runserver' is used")
- normalized_path = posixpath.normpath(urllib.unquote(path)).lstrip('/')
+ normalized_path = posixpath.normpath(unquote(path)).lstrip('/')
absolute_path = finders.find(normalized_path)
if not absolute_path:
if path.endswith('/') or path == '':
5 django/core/cache/__init__.py
View
@@ -14,7 +14,10 @@
See docs/topics/cache.txt for information on the public API.
"""
-from urlparse import parse_qsl
+try:
+ from urllib.parse import parse_qsl
+except ImportError: # Python 2
+ from urlparse import parse_qsl
from django.conf import settings
from django.core import signals
7 django/core/files/storage.py
View
@@ -1,6 +1,9 @@
import os
import errno
-import urlparse
+try:
+ from urllib.parse import urljoin
+except ImportError: # Python 2
+ from urlparse import urljoin
import itertools
from datetime import datetime
@@ -252,7 +255,7 @@ def size(self, name):
def url(self, name):
if self.base_url is None:
raise ValueError("This file is not accessible via a URL.")
- return urlparse.urljoin(self.base_url, filepath_to_uri(name))
+ return urljoin(self.base_url, filepath_to_uri(name))
def accessed_time(self, name):
return datetime.fromtimestamp(os.path.getatime(self.path(name)))
8 django/core/management/templates.py
View
@@ -8,7 +8,10 @@
import stat
import sys
import tempfile
-import urllib
+try:
+ from urllib.request import urlretrieve
+except ImportError: # Python 2
+ from urllib import urlretrieve
from optparse import make_option
from os import path
@@ -227,8 +230,7 @@ def cleanup_url(url):
if self.verbosity >= 2:
self.stdout.write("Downloading %s\n" % display_url)
try:
- the_path, info = urllib.urlretrieve(url,
- path.join(tempdir, filename))
+ the_path, info = urlretrieve(url, path.join(tempdir, filename))
except IOError as e:
raise CommandError("couldn't download URL %s to %s: %s" %
(url, filename, e))
11 django/core/servers/basehttp.py
View
@@ -11,8 +11,11 @@
import socket
import sys
import traceback
-import urllib
-import urlparse
+try:
+ from urllib.parse import unquote, urljoin
+except ImportError: # Python 2
+ from urllib import unquote
+ from urlparse import urljoin
from SocketServer import ThreadingMixIn
from wsgiref import simple_server
from wsgiref.util import FileWrapper # for backwards compatibility
@@ -127,7 +130,7 @@ class WSGIRequestHandler(simple_server.WSGIRequestHandler, object):
def __init__(self, *args, **kwargs):
from django.conf import settings
- self.admin_static_prefix = urlparse.urljoin(settings.STATIC_URL, 'admin/')
+ self.admin_static_prefix = urljoin(settings.STATIC_URL, 'admin/')
# We set self.path to avoid crashes in log_message() on unsupported
# requests (like "OPTIONS").
self.path = ''
@@ -143,7 +146,7 @@ def get_environ(self):
else:
path,query = self.path,''
- env['PATH_INFO'] = urllib.unquote(path)
+ env['PATH_INFO'] = unquote(path)
env['QUERY_STRING'] = query
env['REMOTE_ADDR'] = self.client_address[0]
env['CONTENT_TYPE'] = self.headers.get('content-type', 'text/plain')
9 django/core/validators.py
View
@@ -1,7 +1,10 @@
from __future__ import unicode_literals
import re
-import urlparse
+try:
+ from urllib.parse import urlsplit, urlunsplit
+except ImportError: # Python 2
+ from urlparse import urlsplit, urlunsplit
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _
@@ -52,12 +55,12 @@ def __call__(self, value):
# Trivial case failed. Try for possible IDN domain
if value:
value = smart_unicode(value)
- scheme, netloc, path, query, fragment = urlparse.urlsplit(value)
+ scheme, netloc, path, query, fragment = urlsplit(value)
try:
netloc = netloc.encode('idna') # IDN -> ACE
except UnicodeError: # invalid domain part
raise e
- url = urlparse.urlunsplit((scheme, netloc, path, query, fragment))
+ url = urlunsplit((scheme, netloc, path, query, fragment))
super(URLValidator, self).__call__(url)
else:
raise
11 django/forms/fields.py
View
@@ -8,7 +8,10 @@
import datetime
import os
import re
-import urlparse
+try:
+ from urllib.parse import urlsplit, urlunsplit
+except ImportError: # Python 2
+ from urlparse import urlsplit, urlunsplit
from decimal import Decimal, DecimalException
from io import BytesIO
@@ -599,7 +602,7 @@ def split_url(url):
``ValidationError`` exception for certain).
"""
try:
- return list(urlparse.urlsplit(url))
+ return list(urlsplit(url))
except ValueError:
# urlparse.urlsplit can raise a ValueError with some
# misformatted URLs.
@@ -618,11 +621,11 @@ def split_url(url):
url_fields[2] = ''
# Rebuild the url_fields list, since the domain segment may now
# contain the path too.
- url_fields = split_url(urlparse.urlunsplit(url_fields))
+ url_fields = split_url(urlunsplit(url_fields))
if not url_fields[2]:
# the path portion may need to be added before query params
url_fields[2] = '/'
- value = urlparse.urlunsplit(url_fields)
+ value = urlunsplit(url_fields)
return value
class BooleanField(Field):
5 django/forms/widgets.py
View
@@ -7,7 +7,10 @@
import copy
import datetime
from itertools import chain
-from urlparse import urljoin
+try:
+ from urllib.parse import urljoin
+except ImportError: # Python 2
+ from urlparse import urljoin
from django.conf import settings
from django.forms.util import flatatt, to_current_timezone
7 django/http/__init__.py
View
@@ -10,8 +10,11 @@
from io import BytesIO
from pprint import pformat
-from urllib import urlencode, quote
-from urlparse import urljoin, parse_qsl
+try:
+ from urllib.parse import quote, parse_qsl, urlencode, urljoin
+except ImportError: # Python 2
+ from urllib import quote, urlencode
+ from urlparse import parse_qsl, urljoin
import Cookie
# Some versions of Python 2.7 and later won't need this encoding bug fix:
6 django/templatetags/static.py
View
@@ -1,4 +1,8 @@
-from urlparse import urljoin
+try:
+ from urllib.parse import urljoin
+except ImportError: # Python 2
+ from urlparse import urljoin
+
from django import template
from django.template.base import Node
from django.utils.encoding import iri_to_uri
11 django/test/client.py
View
@@ -1,11 +1,14 @@
-import urllib
import sys
import os
import re
import mimetypes
from copy import copy
from io import BytesIO
-from urlparse import urlparse, urlsplit
+try:
+ from urllib.parse import unquote, urlparse, urlsplit
+except ImportError: # Python 2
+ from urllib import unquote
+ from urlparse import urlparse, urlsplit
from django.conf import settings
from django.contrib.auth import authenticate, login
@@ -222,9 +225,9 @@ def _encode_data(self, data, content_type, ):
def _get_path(self, parsed):
# If there are parameters, add them
if parsed[3]:
- return urllib.unquote(parsed[2] + ";" + parsed[3])
+ return unquote(parsed[2] + ";" + parsed[3])
else:
- return urllib.unquote(parsed[2])
+ return unquote(parsed[2])
def get(self, path, data={}, **extra):
"Construct a GET request."
5 django/test/testcases.py
View
@@ -7,7 +7,10 @@
import sys
from copy import copy
from functools import wraps
-from urlparse import urlsplit, urlunsplit
+try:
+ from urllib.parse import urlsplit, urlunsplit
+except ImportError: # Python 2
+ from urlparse import urlsplit, urlunsplit
from xml.dom.minidom import parseString, Node
import select
import socket
13 django/utils/encoding.py
View
@@ -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
7 django/utils/feedgenerator.py
View
@@ -24,7 +24,10 @@
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')
13 django/utils/html.py
View
@@ -4,8 +4,11 @@
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 @@ def fix_ampersands(value):
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)
22 django/utils/http.py
View
@@ -2,8 +2,14 @@
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)
7 django/views/static.py
View
@@ -9,7 +9,10 @@
import stat
import posixpath
import re
-import urllib
+try:
+ from urllib.parse import unquote
+except ImportError: # Python 2
+ from urllib import unquote
from django.http import Http404, HttpResponse, HttpResponseRedirect, HttpResponseNotModified
from django.template import loader, Template, Context, TemplateDoesNotExist
@@ -30,7 +33,7 @@ def serve(request, path, document_root=None, show_indexes=False):
but if you'd like to override it, you can create a template called
``static/directory_index.html``.
"""
- path = posixpath.normpath(urllib.unquote(path))
+ path = posixpath.normpath(unquote(path))
path = path.lstrip('/')
newpath = ''
for part in path.split('/'):
6 tests/modeltests/test_client/views.py
View
@@ -1,3 +1,7 @@
+try:
+ from urllib.parse import urlencode
+except ImportError: # Python 2
+ from urllib import urlencode
from xml.dom.minidom import parseString
from django.contrib.auth.decorators import login_required, permission_required
@@ -9,7 +13,6 @@
from django.template import Context, Template
from django.utils.decorators import method_decorator
-
def get_view(request):
"A simple view that expects a GET request, and returns a rendered template"
t = Template('This is a test. {{ var }} is the value.', name='GET Template')
@@ -58,7 +61,6 @@ def raw_post_view(request):
def redirect_view(request):
"A view that redirects all requests to the GET view"
if request.GET:
- from urllib import urlencode
query = '?' + urlencode(request.GET, True)
else:
query = ''
7 tests/regressiontests/admin_views/tests.py
View
@@ -4,7 +4,10 @@
import os
import re
import datetime
-import urlparse
+try:
+ from urllib.parse import urljoin
+except ImportError: # Python 2
+ from urlparse import urljoin
from django.conf import settings, global_settings
from django.core import mail
@@ -3199,7 +3202,7 @@ def test_limit_choices_to(self):
popup_url = m.groups()[0].replace("&", "&")
# Handle relative links
- popup_url = urlparse.urljoin(response.request['PATH_INFO'], popup_url)
+ popup_url = urljoin(response.request['PATH_INFO'], popup_url)
# Get the popup
response2 = self.client.get(popup_url)
self.assertContains(response2, "Spain")
9 tests/regressiontests/servers/tests.py
View
@@ -2,7 +2,10 @@
Tests for django.core.servers.
"""
import os
-import urllib2
+try:
+ from urllib.request import urlopen, HTTPError
+except ImportError: # Python 2
+ from urllib2 import urlopen, HTTPError
from django.core.exceptions import ImproperlyConfigured
from django.test import LiveServerTestCase
@@ -39,7 +42,7 @@ def tearDownClass(cls):
super(LiveServerBase, cls).tearDownClass()
def urlopen(self, url):
- return urllib2.urlopen(self.live_server_url + url)
+ return urlopen(self.live_server_url + url)
class LiveServerAddress(LiveServerBase):
@@ -102,7 +105,7 @@ def test_404(self):
"""
try:
self.urlopen('/')
- except urllib2.HTTPError as err:
+ except HTTPError as err:
self.assertEqual(err.code, 404, 'Expected 404 response')
else:
self.fail('Expected 404 response')
5 tests/regressiontests/templates/tests.py
View
@@ -13,7 +13,10 @@
import os
import sys
import traceback
-from urlparse import urljoin
+try:
+ from urllib.parse import urljoin
+except ImportError: # Python 2
+ from urlparse import urljoin
from django import template
from django.template import base as template_base, RequestContext, Template, Context
Please sign in to comment.
Something went wrong with that request. Please try again.