summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAymeric Augustin <aymeric.augustin@m4x.org>2013-09-05 14:38:59 -0500
committerClaude Paroz <claude@2xlibre.net>2013-09-10 21:29:31 +0200
commitf9f792eb04dfd48d93682070583f473166b490ae (patch)
treec66c3d474e808d664b3b24b94fdc3d5344295c4f
parent960f5bc75901f76e9b4b356f98b22b494fb47611 (diff)
[1.6.x] Took advantage of django.utils.six.moves.urllib.*.
Backport of 6a6428a36 from master.
-rw-r--r--django/contrib/auth/decorators.py5
-rw-r--r--django/contrib/auth/tests/test_views.py5
-rw-r--r--django/contrib/auth/views.py6
-rw-r--r--django/contrib/comments/views/utils.py5
-rw-r--r--django/contrib/sitemaps/__init__.py7
-rw-r--r--django/contrib/staticfiles/handlers.py9
-rw-r--r--django/contrib/staticfiles/storage.py6
-rw-r--r--django/contrib/staticfiles/views.py5
-rw-r--r--django/core/cache/__init__.py5
-rw-r--r--django/core/files/storage.py5
-rw-r--r--django/core/management/templates.py5
-rw-r--r--django/core/servers/basehttp.py7
-rw-r--r--django/core/validators.py6
-rw-r--r--django/forms/fields.py5
-rw-r--r--django/forms/widgets.py5
-rw-r--r--django/templatetags/static.py6
-rw-r--r--django/test/client.py6
-rw-r--r--django/test/testcases.py5
-rw-r--r--django/utils/encoding.py5
-rw-r--r--django/utils/feedgenerator.py5
-rw-r--r--django/utils/html.py6
-rw-r--r--django/utils/http.py23
-rw-r--r--django/views/static.py5
-rw-r--r--docs/topics/python3.txt9
-rw-r--r--tests/admin_views/tests.py11
-rw-r--r--tests/file_storage/tests.py7
-rw-r--r--tests/requests/tests.py4
-rw-r--r--tests/servers/tests.py6
-rw-r--r--tests/template_tests/tests.py5
-rw-r--r--tests/test_client/views.py5
30 files changed, 49 insertions, 145 deletions
diff --git a/django/contrib/auth/decorators.py b/django/contrib/auth/decorators.py
index 11518193e7..19f7028bb2 100644
--- a/django/contrib/auth/decorators.py
+++ b/django/contrib/auth/decorators.py
@@ -1,13 +1,10 @@
-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
from django.core.exceptions import PermissionDenied
from django.utils.decorators import available_attrs
from django.utils.encoding import force_str
+from django.utils.six.moves.urllib.parse import urlparse
from django.shortcuts import resolve_url
diff --git a/django/contrib/auth/tests/test_views.py b/django/contrib/auth/tests/test_views.py
index 42ceea36d3..9c0772190d 100644
--- a/django/contrib/auth/tests/test_views.py
+++ b/django/contrib/auth/tests/test_views.py
@@ -1,10 +1,6 @@
import itertools
import os
import re
-try:
- from urllib.parse import urlparse, ParseResult
-except ImportError: # Python 2
- from urlparse import urlparse, ParseResult
from django.conf import global_settings, settings
from django.contrib.sites.models import Site, RequestSite
@@ -15,6 +11,7 @@ from django.core.urlresolvers import reverse, NoReverseMatch
from django.http import QueryDict, HttpRequest
from django.utils.encoding import force_text
from django.utils.http import int_to_base36, urlsafe_base64_decode, urlquote
+from django.utils.six.moves.urllib.parse import urlparse, ParseResult
from django.utils._os import upath
from django.test import TestCase
from django.test.utils import override_settings, patch_logger
diff --git a/django/contrib/auth/views.py b/django/contrib/auth/views.py
index e9affb33cd..c55f243d98 100644
--- a/django/contrib/auth/views.py
+++ b/django/contrib/auth/views.py
@@ -1,14 +1,10 @@
-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
from django.http import HttpResponseRedirect, QueryDict
from django.template.response import TemplateResponse
from django.utils.http import base36_to_int, is_safe_url, urlsafe_base64_decode, urlsafe_base64_encode
from django.utils.translation import ugettext as _
+from django.utils.six.moves.urllib.parse import urlparse, urlunparse
from django.shortcuts import resolve_url
from django.utils.encoding import force_bytes, force_text
from django.views.decorators.debug import sensitive_post_parameters
diff --git a/django/contrib/comments/views/utils.py b/django/contrib/comments/views/utils.py
index da70272282..3705b7e0bf 100644
--- a/django/contrib/comments/views/utils.py
+++ b/django/contrib/comments/views/utils.py
@@ -3,10 +3,6 @@ A few bits of helper functions for comment views.
"""
import textwrap
-try:
- from urllib.parse import urlencode
-except ImportError: # Python 2
- from urllib import urlencode
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response, resolve_url
@@ -14,6 +10,7 @@ from django.template import RequestContext
from django.core.exceptions import ObjectDoesNotExist
from django.contrib import comments
from django.utils.http import is_safe_url
+from django.utils.six.moves.urllib.parse import urlencode
def next_redirect(request, fallback, **get_kwargs):
"""
diff --git a/django/contrib/sitemaps/__init__.py b/django/contrib/sitemaps/__init__.py
index 1acae8296c..72c1932c19 100644
--- a/django/contrib/sitemaps/__init__.py
+++ b/django/contrib/sitemaps/__init__.py
@@ -1,11 +1,8 @@
from django.contrib.sites.models import Site
from django.core import urlresolvers, paginator
from django.core.exceptions import ImproperlyConfigured
-try:
- from urllib.parse import urlencode
- from urllib.request import urlopen
-except ImportError: # Python 2
- from urllib import urlencode, urlopen
+from django.utils.six.moves.urllib.parse import urlencode
+from django.utils.six.moves.urllib.request import urlopen
PING_URL = "http://www.google.com/webmasters/tools/ping"
diff --git a/django/contrib/staticfiles/handlers.py b/django/contrib/staticfiles/handlers.py
index 5174586ba1..e6db3dcc1f 100644
--- a/django/contrib/staticfiles/handlers.py
+++ b/django/contrib/staticfiles/handlers.py
@@ -1,13 +1,8 @@
-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.base import get_path_info
from django.core.handlers.wsgi import WSGIHandler
+from django.utils.six.moves.urllib.parse import urlparse
+from django.utils.six.moves.urllib.request import url2pathname
from django.contrib.staticfiles import utils
from django.contrib.staticfiles.views import serve
diff --git a/django/contrib/staticfiles/storage.py b/django/contrib/staticfiles/storage.py
index cd4ca516d5..899e1582b8 100644
--- a/django/contrib/staticfiles/storage.py
+++ b/django/contrib/staticfiles/storage.py
@@ -3,11 +3,6 @@ import hashlib
import os
import posixpath
import re
-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,
@@ -19,6 +14,7 @@ from django.utils.datastructures import SortedDict
from django.utils.encoding import force_bytes, force_text
from django.utils.functional import LazyObject
from django.utils.importlib import import_module
+from django.utils.six.moves.urllib.parse import unquote, urlsplit, urlunsplit, urldefrag
from django.utils._os import upath
from django.contrib.staticfiles.utils import check_settings, matches_patterns
diff --git a/django/contrib/staticfiles/views.py b/django/contrib/staticfiles/views.py
index f5c42fedf8..a7f9b0d314 100644
--- a/django/contrib/staticfiles/views.py
+++ b/django/contrib/staticfiles/views.py
@@ -5,14 +5,11 @@ development, and SHOULD NOT be used in a production setting.
"""
import os
import posixpath
-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
from django.http import Http404
+from django.utils.six.moves.urllib.parse import unquote
from django.views import static
from django.contrib.staticfiles import finders
diff --git a/django/core/cache/__init__.py b/django/core/cache/__init__.py
index 1242372dcf..ea3a68ff0e 100644
--- a/django/core/cache/__init__.py
+++ b/django/core/cache/__init__.py
@@ -14,10 +14,6 @@ cache class.
See docs/topics/cache.txt for information on the public API.
"""
-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
@@ -26,6 +22,7 @@ from django.core.cache.backends.base import (
from django.core.exceptions import ImproperlyConfigured
from django.utils import importlib
from django.utils.module_loading import import_by_path
+from django.utils.six.moves.urllib.parse import parse_qsl
__all__ = [
diff --git a/django/core/files/storage.py b/django/core/files/storage.py
index 5d301a317c..80123781f6 100644
--- a/django/core/files/storage.py
+++ b/django/core/files/storage.py
@@ -1,9 +1,5 @@
import os
import errno
-try:
- from urllib.parse import urljoin
-except ImportError: # Python 2
- from urlparse import urljoin
import itertools
from datetime import datetime
@@ -14,6 +10,7 @@ from django.core.files.move import file_move_safe
from django.utils.encoding import force_text, filepath_to_uri
from django.utils.functional import LazyObject
from django.utils.module_loading import import_by_path
+from django.utils.six.moves.urllib.parse import urljoin
from django.utils.text import get_valid_filename
from django.utils._os import safe_join, abspathu
diff --git a/django/core/management/templates.py b/django/core/management/templates.py
index 1de508d749..164fd071e5 100644
--- a/django/core/management/templates.py
+++ b/django/core/management/templates.py
@@ -8,10 +8,6 @@ import shutil
import stat
import sys
import tempfile
-try:
- from urllib.request import urlretrieve
-except ImportError: # Python 2
- from urllib import urlretrieve
from optparse import make_option
from os import path
@@ -19,6 +15,7 @@ from os import path
import django
from django.template import Template, Context
from django.utils import archive
+from django.utils.six.moves.urllib.request import urlretrieve
from django.utils._os import rmtree_errorhandler
from django.core.management.base import BaseCommand, CommandError
from django.core.management.utils import handle_extensions
diff --git a/django/core/servers/basehttp.py b/django/core/servers/basehttp.py
index 387da6b3cd..6287b1a4f9 100644
--- a/django/core/servers/basehttp.py
+++ b/django/core/servers/basehttp.py
@@ -13,11 +13,6 @@ from io import BytesIO
import socket
import sys
import traceback
-try:
- from urllib.parse import urljoin
-except ImportError: # Python 2
- from urlparse import urljoin
-from django.utils.six.moves import socketserver
from wsgiref import simple_server
from wsgiref.util import FileWrapper # for backwards compatibility
@@ -25,6 +20,8 @@ from django.core.management.color import color_style
from django.core.wsgi import get_wsgi_application
from django.utils.module_loading import import_by_path
from django.utils import six
+from django.utils.six.moves.urllib.parse import urljoin
+from django.utils.six.moves import socketserver
__all__ = ('WSGIServer', 'WSGIRequestHandler', 'MAX_SOCKET_CHUNK_SIZE')
diff --git a/django/core/validators.py b/django/core/validators.py
index aa417ed099..78716ccd40 100644
--- a/django/core/validators.py
+++ b/django/core/validators.py
@@ -1,16 +1,14 @@
from __future__ import unicode_literals
import re
-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 _, ungettext_lazy
from django.utils.encoding import force_text
from django.utils.ipv6 import is_valid_ipv6_address
from django.utils import six
+from django.utils.six.moves.urllib.parse import urlsplit, urlunsplit
+
# These values, if given to validate(), will trigger the self.required check.
EMPTY_VALUES = (None, '', [], (), {})
diff --git a/django/forms/fields.py b/django/forms/fields.py
index 76ee9b91d3..d9243f142a 100644
--- a/django/forms/fields.py
+++ b/django/forms/fields.py
@@ -9,10 +9,6 @@ import datetime
import os
import re
import sys
-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
@@ -29,6 +25,7 @@ from django.utils import formats
from django.utils.encoding import smart_text, force_str, force_text
from django.utils.ipv6 import clean_ipv6_address
from django.utils import six
+from django.utils.six.moves.urllib.parse import urlsplit, urlunsplit
from django.utils.translation import ugettext_lazy as _, ungettext_lazy
# Provide this import for backwards compatibility.
diff --git a/django/forms/widgets.py b/django/forms/widgets.py
index d74710bcba..cf68bac3f4 100644
--- a/django/forms/widgets.py
+++ b/django/forms/widgets.py
@@ -6,10 +6,6 @@ from __future__ import absolute_import, unicode_literals
import copy
from itertools import chain
-try:
- from urllib.parse import urljoin
-except ImportError: # Python 2
- from urlparse import urljoin
import warnings
from django.conf import settings
@@ -20,6 +16,7 @@ from django.utils.translation import ugettext_lazy
from django.utils.encoding import force_text, python_2_unicode_compatible
from django.utils.safestring import mark_safe
from django.utils import datetime_safe, formats, six
+from django.utils.six.moves.urllib.parse import urljoin
__all__ = (
'Media', 'MediaDefiningClass', 'Widget', 'TextInput',
diff --git a/django/templatetags/static.py b/django/templatetags/static.py
index 68437e7e60..e3a23fbc5a 100644
--- a/django/templatetags/static.py
+++ b/django/templatetags/static.py
@@ -1,11 +1,7 @@
-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
+from django.utils.six.moves.urllib.parse import urljoin
register = template.Library()
diff --git a/django/test/client.py b/django/test/client.py
index fb9a9e7a02..b4bdde6931 100644
--- a/django/test/client.py
+++ b/django/test/client.py
@@ -6,11 +6,6 @@ import re
import mimetypes
from copy import copy
from io import BytesIO
-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, logout, get_user_model
@@ -28,6 +23,7 @@ from django.utils.http import urlencode
from django.utils.importlib import import_module
from django.utils.itercompat import is_iterable
from django.utils import six
+from django.utils.six.moves.urllib.parse import unquote, urlparse, urlsplit
from django.test.utils import ContextList
__all__ = ('Client', 'RequestFactory', 'encode_file', 'encode_multipart')
diff --git a/django/test/testcases.py b/django/test/testcases.py
index 984d21ce18..b70fea69e2 100644
--- a/django/test/testcases.py
+++ b/django/test/testcases.py
@@ -8,10 +8,6 @@ import json
import os
import re
import sys
-try:
- from urllib.parse import urlsplit, urlunsplit
-except ImportError: # Python 2
- from urlparse import urlsplit, urlunsplit
import select
import socket
import threading
@@ -39,6 +35,7 @@ from django.test.utils import (CaptureQueriesContext, ContextList,
override_settings, compare_xml)
from django.utils import six, unittest as ut2
from django.utils.encoding import force_text
+from django.utils.six.moves.urllib.parse import urlsplit, urlunsplit
from django.utils.unittest import skipIf # Imported here for backward compatibility
from django.utils.unittest.util import safe_repr
from django.views.static import serve
diff --git a/django/utils/encoding.py b/django/utils/encoding.py
index 116178c4c2..ad06336c4d 100644
--- a/django/utils/encoding.py
+++ b/django/utils/encoding.py
@@ -4,14 +4,11 @@ import codecs
import datetime
from decimal import Decimal
import locale
-try:
- from urllib.parse import quote
-except ImportError: # Python 2
- from urllib import quote
import warnings
from django.utils.functional import Promise
from django.utils import six
+from django.utils.six.moves.urllib.parse import quote
class DjangoUnicodeDecodeError(UnicodeDecodeError):
def __init__(self, obj, *args):
diff --git a/django/utils/feedgenerator.py b/django/utils/feedgenerator.py
index ed9ddda6fe..fee8e17865 100644
--- a/django/utils/feedgenerator.py
+++ b/django/utils/feedgenerator.py
@@ -24,15 +24,12 @@ http://web.archive.org/web/20110718035220/http://diveintomark.org/archives/2004/
from __future__ import unicode_literals
import datetime
-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_text, iri_to_uri
from django.utils import datetime_safe
from django.utils import six
from django.utils.six import StringIO
+from django.utils.six.moves.urllib.parse import urlparse
from django.utils.timezone import is_aware
def rfc2822_date(date):
diff --git a/django/utils/html.py b/django/utils/html.py
index 89e790d96f..b55a2234b5 100644
--- a/django/utils/html.py
+++ b/django/utils/html.py
@@ -3,16 +3,12 @@
from __future__ import unicode_literals
import re
-try:
- from urllib.parse import quote, unquote, urlsplit, urlunsplit
-except ImportError: # Python 2
- from urllib import quote, unquote
- from urlparse import urlsplit, urlunsplit
from django.utils.safestring import SafeData, mark_safe
from django.utils.encoding import force_text, force_str
from django.utils.functional import allow_lazy
from django.utils import six
+from django.utils.six.moves.urllib.parse import quote, unquote, urlsplit, urlunsplit
from django.utils.text import normalize_newlines
from .html_parser import HTMLParser, HTMLParseError
diff --git a/django/utils/http.py b/django/utils/http.py
index 85af230dd2..571a179830 100644
--- a/django/utils/http.py
+++ b/django/utils/http.py
@@ -5,12 +5,6 @@ import calendar
import datetime
import re
import sys
-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 binascii import Error as BinasciiError
from email.utils import formatdate
@@ -19,6 +13,9 @@ from django.utils.datastructures import MultiValueDict
from django.utils.encoding import force_str, force_text
from django.utils.functional import allow_lazy
from django.utils import six
+from django.utils.six.moves.urllib.parse import (
+ quote, quote_plus, unquote, unquote_plus, urlparse,
+ urlencode as original_urlencode)
ETAG_MATCH = re.compile(r'(?:W/)?"((?:\\.|[^"])*)"')
@@ -40,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_text(urllib_parse.quote(force_str(url), force_str(safe)))
+ return force_text(quote(force_str(url), force_str(safe)))
urlquote = allow_lazy(urlquote, six.text_type)
def urlquote_plus(url, safe=''):
@@ -50,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_text(urllib_parse.quote_plus(force_str(url), force_str(safe)))
+ return force_text(quote_plus(force_str(url), force_str(safe)))
urlquote_plus = allow_lazy(urlquote_plus, six.text_type)
def urlunquote(quoted_url):
@@ -58,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_text(urllib_parse.unquote(force_str(quoted_url)))
+ return force_text(unquote(force_str(quoted_url)))
urlunquote = allow_lazy(urlunquote, six.text_type)
def urlunquote_plus(quoted_url):
@@ -66,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_text(urllib_parse.unquote_plus(force_str(quoted_url)))
+ return force_text(unquote_plus(force_str(quoted_url)))
urlunquote_plus = allow_lazy(urlunquote_plus, six.text_type)
def urlencode(query, doseq=0):
@@ -79,7 +76,7 @@ def urlencode(query, doseq=0):
query = query.lists()
elif hasattr(query, 'items'):
query = query.items()
- return urllib_parse.urlencode(
+ return original_urlencode(
[(force_str(k),
[force_str(i) for i in v] if isinstance(v, (list,tuple)) else force_str(v))
for k, v in query],
@@ -244,7 +241,7 @@ def same_origin(url1, url2):
"""
Checks if two URLs are 'same-origin'
"""
- p1, p2 = urllib_parse.urlparse(url1), urllib_parse.urlparse(url2)
+ p1, p2 = urlparse(url1), urlparse(url2)
try:
return (p1.scheme, p1.hostname, p1.port) == (p2.scheme, p2.hostname, p2.port)
except ValueError:
@@ -259,6 +256,6 @@ def is_safe_url(url, host=None):
"""
if not url:
return False
- url_info = urllib_parse.urlparse(url)
+ url_info = urlparse(url)
return (not url_info.netloc or url_info.netloc == host) and \
(not url_info.scheme or url_info.scheme in ['http', 'https'])
diff --git a/django/views/static.py b/django/views/static.py
index 12af130796..c1baacd63a 100644
--- a/django/views/static.py
+++ b/django/views/static.py
@@ -9,15 +9,12 @@ import os
import stat
import posixpath
import re
-try:
- from urllib.parse import unquote
-except ImportError: # Python 2
- from urllib import unquote
from django.http import (CompatibleStreamingHttpResponse, Http404,
HttpResponse, HttpResponseRedirect, HttpResponseNotModified)
from django.template import loader, Template, Context, TemplateDoesNotExist
from django.utils.http import http_date, parse_http_date
+from django.utils.six.moves.urllib.parse import unquote
from django.utils.translation import ugettext as _, ugettext_noop
def serve(request, path, document_root=None, show_indexes=False):
diff --git a/docs/topics/python3.txt b/docs/topics/python3.txt
index 8f2306962b..372de1009d 100644
--- a/docs/topics/python3.txt
+++ b/docs/topics/python3.txt
@@ -367,15 +367,6 @@ Moved modules
Some modules were renamed in Python 3. The :mod:`django.utils.six.moves
<six.moves>` module provides a compatible location to import them.
-The ``urllib``, ``urllib2`` and ``urlparse`` modules were reworked in depth
-and :mod:`django.utils.six.moves <six.moves>` doesn't handle them. Django
-explicitly tries both locations, as follows::
-
- try:
- from urllib.parse import urlparse, urlunparse
- except ImportError: # Python 2
- from urlparse import urlparse, urlunparse
-
PY2
~~~
diff --git a/tests/admin_views/tests.py b/tests/admin_views/tests.py
index 28dea38cfd..2ec5c87949 100644
--- a/tests/admin_views/tests.py
+++ b/tests/admin_views/tests.py
@@ -4,10 +4,6 @@ from __future__ import absolute_import, unicode_literals
import os
import re
import datetime
-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
@@ -30,14 +26,17 @@ from django.forms.util import ErrorList
from django.template.response import TemplateResponse
from django.test import TestCase
from django.test.utils import patch_logger
-from django.utils import formats, translation, unittest
+from django.test.utils import override_settings
+from django.utils import formats
+from django.utils import translation
+from django.utils import unittest
from django.utils.cache import get_max_age
from django.utils.encoding import iri_to_uri, force_bytes
from django.utils.html import escape
from django.utils.http import urlencode, urlquote
+from django.utils.six.moves.urllib.parse import urljoin
from django.utils._os import upath
from django.utils import six
-from django.test.utils import override_settings
# local test models
from .models import (Article, BarAccount, CustomArticle, EmptyModel, FooAccount,
diff --git a/tests/file_storage/tests.py b/tests/file_storage/tests.py
index e6caabf9d9..f92eae3f53 100644
--- a/tests/file_storage/tests.py
+++ b/tests/file_storage/tests.py
@@ -7,10 +7,6 @@ import shutil
import sys
import tempfile
import time
-try:
- from urllib.request import urlopen
-except ImportError: # Python 2
- from urllib2 import urlopen
import zlib
from datetime import datetime, timedelta
from io import BytesIO
@@ -27,10 +23,11 @@ from django.core.files.images import get_image_dimensions
from django.core.files.storage import FileSystemStorage, get_storage_class
from django.core.files.uploadedfile import UploadedFile
from django.test import LiveServerTestCase, SimpleTestCase
+from django.test.utils import override_settings
from django.utils import six
from django.utils import unittest
+from django.utils.six.moves.urllib.request import urlopen
from django.utils._os import upath
-from django.test.utils import override_settings
try:
from django.utils.image import Image
diff --git a/tests/requests/tests.py b/tests/requests/tests.py
index 9e1cf19307..98cda31c1b 100644
--- a/tests/requests/tests.py
+++ b/tests/requests/tests.py
@@ -17,6 +17,7 @@ from django.test.utils import override_settings, str_prefix
from django.utils import six
from django.utils.unittest import skipIf
from django.utils.http import cookie_date, urlencode
+from django.utils.six.moves.urllib.parse import urlencode as original_urlencode
from django.utils.timezone import utc
@@ -493,8 +494,7 @@ class RequestsTests(SimpleTestCase):
"""
Test a POST with non-utf-8 payload encoding.
"""
- from django.utils.http import urllib_parse
- payload = FakePayload(urllib_parse.urlencode({'key': 'España'.encode('latin-1')}))
+ payload = FakePayload(original_urlencode({'key': 'España'.encode('latin-1')}))
request = WSGIRequest({
'REQUEST_METHOD': 'POST',
'CONTENT_LENGTH': len(payload),
diff --git a/tests/servers/tests.py b/tests/servers/tests.py
index be74afe820..caa2ddb084 100644
--- a/tests/servers/tests.py
+++ b/tests/servers/tests.py
@@ -5,16 +5,14 @@ Tests for django.core.servers.
from __future__ import unicode_literals
import os
-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
from django.core.servers.basehttp import WSGIServerException
from django.test.utils import override_settings
from django.utils.http import urlencode
+from django.utils.six.moves.urllib.error import HTTPError
+from django.utils.six.moves.urllib.request import urlopen
from django.utils._os import upath
from .models import Person
diff --git a/tests/template_tests/tests.py b/tests/template_tests/tests.py
index 76712a09a6..b2f35b1a8d 100644
--- a/tests/template_tests/tests.py
+++ b/tests/template_tests/tests.py
@@ -13,10 +13,6 @@ import time
import os
import sys
import traceback
-try:
- from urllib.parse import urljoin
-except ImportError: # Python 2
- from urlparse import urljoin
import warnings
from django import template
@@ -34,6 +30,7 @@ from django.utils._os import upath
from django.utils.translation import activate, deactivate, ugettext as _
from django.utils.safestring import mark_safe
from django.utils import six
+from django.utils.six.moves.urllib.parse import urljoin
from django.utils.tzinfo import LocalTimezone
from i18n import TransRealMixin
diff --git a/tests/test_client/views.py b/tests/test_client/views.py
index 76296cb80d..08cdd8c198 100644
--- a/tests/test_client/views.py
+++ b/tests/test_client/views.py
@@ -1,7 +1,3 @@
-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
@@ -13,6 +9,7 @@ from django.http import HttpResponse, HttpResponseRedirect, HttpResponseNotFound
from django.shortcuts import render_to_response
from django.template import Context, Template
from django.utils.decorators import method_decorator
+from django.utils.six.moves.urllib.parse import urlencode
def get_view(request):
"A simple view that expects a GET request, and returns a rendered template"