From 6a6428a36f221446b17eaf4876e92d3db1781962 Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Thu, 5 Sep 2013 14:38:59 -0500 Subject: [PATCH] Took advantage of django.utils.six.moves.urllib.*. --- django/contrib/auth/decorators.py | 5 +---- django/contrib/auth/tests/test_views.py | 5 +---- django/contrib/auth/views.py | 6 +----- django/contrib/comments/views/utils.py | 5 +---- django/contrib/sitemaps/__init__.py | 7 ++----- django/contrib/staticfiles/handlers.py | 9 ++------- django/contrib/staticfiles/storage.py | 6 +----- django/contrib/staticfiles/views.py | 5 +---- django/core/cache/__init__.py | 5 +---- django/core/files/storage.py | 5 +---- django/core/management/templates.py | 5 +---- django/core/servers/basehttp.py | 7 ++----- django/core/validators.py | 6 ++---- django/forms/fields.py | 5 +---- django/forms/widgets.py | 5 +---- django/templatetags/static.py | 6 +----- django/test/client.py | 6 +----- django/test/testcases.py | 10 +++------- django/utils/encoding.py | 5 +---- django/utils/feedgenerator.py | 5 +---- django/utils/html.py | 6 +----- django/utils/http.py | 23 ++++++++++------------ django/views/static.py | 5 +---- docs/topics/python3.txt | 9 --------- tests/admin_views/tests.py | 7 ++----- tests/file_storage/tests.py | 9 +++------ tests/requests/tests.py | 4 ++-- tests/servers/tests.py | 6 ++---- tests/staticfiles_tests/test_liveserver.py | 5 +---- tests/template_tests/tests.py | 5 +---- tests/test_client/views.py | 5 +---- 31 files changed, 50 insertions(+), 152 deletions(-) diff --git a/django/contrib/auth/decorators.py b/django/contrib/auth/decorators.py index 24e31144b1..4935213d0b 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 7839b0b9f9..0ee36e0082 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 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 d852106a4e..d5f15138c4 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 is_safe_url, urlsafe_base64_decode 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.views.decorators.debug import sensitive_post_parameters from django.views.decorators.cache import never_cache 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 627813a33e..996e719865 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 1242afe411..a527379feb 100644 --- a/django/contrib/staticfiles/storage.py +++ b/django/contrib/staticfiles/storage.py @@ -5,11 +5,6 @@ from importlib import import_module 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.core.files.base import ContentFile from django.core.files.storage import FileSystemStorage, get_storage_class from django.utils.encoding import force_bytes, force_text from django.utils.functional import LazyObject +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 efba1690c0..246a4b1aa4 100644 --- a/django/contrib/staticfiles/views.py +++ b/django/contrib/staticfiles/views.py @@ -5,13 +5,10 @@ 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.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 e1d2eb455a..2721a791fa 100644 --- a/django/core/cache/__init__.py +++ b/django/core/cache/__init__.py @@ -15,10 +15,6 @@ cache class. See docs/topics/cache.txt for information on the public API. """ import importlib -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 ( InvalidCacheBackendError, CacheKeyWarning, BaseCache) from django.core.exceptions import ImproperlyConfigured 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 5e587da2da..c1e654beeb 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 8abb1b3513..c06352a964 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 aa216e617b..26f8d312e4 100644 --- a/django/forms/widgets.py +++ b/django/forms/widgets.py @@ -6,10 +6,6 @@ from __future__ 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 3c58eae4b5..4e4a600548 100644 --- a/django/test/client.py +++ b/django/test/client.py @@ -7,11 +7,6 @@ import mimetypes from copy import copy from importlib import import_module 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.encoding import force_bytes, force_str from django.utils.http import urlencode 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 c33d2922c2..29d3a88149 100644 --- a/django/test/testcases.py +++ b/django/test/testcases.py @@ -13,12 +13,6 @@ import threading import unittest from unittest import skipIf # Imported here for backward compatibility from unittest.util import safe_repr -try: - from urllib.parse import urlsplit, urlunsplit, urlparse, unquote - from urllib.request import url2pathname -except ImportError: # Python 2 - from urlparse import urlsplit, urlunsplit, urlparse - from urllib import url2pathname, unquote from django.conf import settings from django.core import mail @@ -40,8 +34,10 @@ from django.test.html import HTMLParseError, parse_html from django.test.signals import template_rendered from django.test.utils import (CaptureQueriesContext, ContextList, override_settings, compare_xml) -from django.utils import six from django.utils.encoding import force_text +from django.utils import six +from django.utils.six.moves.urllib.parse import urlsplit, urlunsplit, urlparse, unquote +from django.utils.six.moves.urllib.request import url2pathname from django.views.static import serve diff --git a/django/utils/encoding.py b/django/utils/encoding.py index bb7c38fea8..d31c27fe80 100644 --- a/django/utils/encoding.py +++ b/django/utils/encoding.py @@ -4,13 +4,10 @@ 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 +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 3aa6b2cb23..e632845a84 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 46750d9cad..76fc23713d 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 a751dea3c6..fb6c4b31ca 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], @@ -243,7 +240,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: @@ -258,6 +255,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 470999bb9d..8884b7aa0e 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 (Http404, HttpResponse, HttpResponseRedirect, HttpResponseNotModified, StreamingHttpResponse) 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 ` module provides a compatible location to import them. -The ``urllib``, ``urllib2`` and ``urlparse`` modules were reworked in depth -and :mod:`django.utils.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 d7b6570f10..091ce084c8 100644 --- a/tests/admin_views/tests.py +++ b/tests/admin_views/tests.py @@ -5,10 +5,6 @@ import os import re import datetime import unittest -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,15 +26,16 @@ 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.test.utils import override_settings from django.utils import formats from django.utils import translation 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 c09d0ef239..bd9510e698 100644 --- a/tests/file_storage/tests.py +++ b/tests/file_storage/tests.py @@ -8,10 +8,6 @@ import sys import tempfile import time import unittest -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 @@ -28,9 +24,10 @@ 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.utils import six -from django.utils._os import upath from django.test.utils import override_settings +from django.utils import six +from django.utils.six.moves.urllib.request import urlopen +from django.utils._os import upath try: from django.utils.image import Image diff --git a/tests/requests/tests.py b/tests/requests/tests.py index 5bd3e5141f..b5edc09deb 100644 --- a/tests/requests/tests.py +++ b/tests/requests/tests.py @@ -18,6 +18,7 @@ from django.test.client import FakePayload from django.test.utils import override_settings, str_prefix from django.utils import six 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 @@ -279,8 +280,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 86b8a4c6c9..f833570862 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/staticfiles_tests/test_liveserver.py b/tests/staticfiles_tests/test_liveserver.py index 80333aae1f..71f4bb89a1 100644 --- a/tests/staticfiles_tests/test_liveserver.py +++ b/tests/staticfiles_tests/test_liveserver.py @@ -5,13 +5,10 @@ django.test.LiveServerTestCase. """ import os -try: - from urllib.request import urlopen -except ImportError: # Python 2 - from urllib2 import urlopen from django.core.exceptions import ImproperlyConfigured from django.test.utils import override_settings +from django.utils.six.moves.urllib.request import urlopen from django.utils._os import upath from django.contrib.staticfiles.testing import StaticLiveServerCase diff --git a/tests/template_tests/tests.py b/tests/template_tests/tests.py index 876fb448f5..5897a3e83a 100644 --- a/tests/template_tests/tests.py +++ b/tests/template_tests/tests.py @@ -13,10 +13,6 @@ import os import sys import traceback import unittest -try: - from urllib.parse import urljoin -except ImportError: # Python 2 - from urlparse import urljoin import warnings from django import template @@ -33,6 +29,7 @@ from django.utils._os import upath from django.utils.translation import activate, deactivate from django.utils.safestring import mark_safe from django.utils import six +from django.utils.six.moves.urllib.parse import urljoin 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"