Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Took advantage of django.utils.six.moves.urllib.*.

  • Loading branch information...
commit 6a6428a36f221446b17eaf4876e92d3db1781962 1 parent ed9cd4f
@aaugustin aaugustin authored
Showing with 49 additions and 151 deletions.
  1. +1 −4 django/contrib/auth/decorators.py
  2. +1 −4 django/contrib/auth/tests/test_views.py
  3. +1 −5 django/contrib/auth/views.py
  4. +1 −4 django/contrib/comments/views/utils.py
  5. +2 −5 django/contrib/sitemaps/__init__.py
  6. +2 −7 django/contrib/staticfiles/handlers.py
  7. +1 −5 django/contrib/staticfiles/storage.py
  8. +1 −4 django/contrib/staticfiles/views.py
  9. +1 −4 django/core/cache/__init__.py
  10. +1 −4 django/core/files/storage.py
  11. +1 −4 django/core/management/templates.py
  12. +2 −5 django/core/servers/basehttp.py
  13. +2 −4 django/core/validators.py
  14. +1 −4 django/forms/fields.py
  15. +1 −4 django/forms/widgets.py
  16. +1 −5 django/templatetags/static.py
  17. +1 −5 django/test/client.py
  18. +3 −7 django/test/testcases.py
  19. +1 −4 django/utils/encoding.py
  20. +1 −4 django/utils/feedgenerator.py
  21. +1 −5 django/utils/html.py
  22. +10 −13 django/utils/http.py
  23. +1 −4 django/views/static.py
  24. +0 −9 docs/topics/python3.txt
  25. +2 −5 tests/admin_views/tests.py
  26. +2 −5 tests/file_storage/tests.py
  27. +2 −2 tests/requests/tests.py
  28. +2 −4 tests/servers/tests.py
  29. +1 −4 tests/staticfiles_tests/test_liveserver.py
  30. +1 −4 tests/template_tests/tests.py
  31. +1 −4 tests/test_client/views.py
View
5 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
View
5 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.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
View
6 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
View
5 django/contrib/comments/views/utils.py
@@ -3,10 +3,6 @@
"""
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.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):
"""
View
7 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"
View
9 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
View
6 django/contrib/staticfiles/storage.py
@@ -5,11 +5,6 @@
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.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
View
5 django/contrib/staticfiles/views.py
@@ -5,13 +5,10 @@
"""
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
View
5 django/core/cache/__init__.py
@@ -15,10 +15,6 @@
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 @@
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__ = [
View
5 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.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
View
5 django/core/management/templates.py
@@ -8,10 +8,6 @@
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 @@
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
View
7 django/core/servers/basehttp.py
@@ -13,11 +13,6 @@
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.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')
View
6 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, '', [], (), {})
View
5 django/forms/fields.py
@@ -9,10 +9,6 @@
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.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.
View
5 django/forms/widgets.py
@@ -6,10 +6,6 @@
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.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',
View
6 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()
View
6 django/test/client.py
@@ -7,11 +7,6 @@
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.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')
View
10 django/test/testcases.py
@@ -13,12 +13,6 @@
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.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
View
5 django/utils/encoding.py
@@ -4,13 +4,10 @@
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):
View
5 django/utils/feedgenerator.py
@@ -24,15 +24,12 @@
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):
View
6 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
View
23 django/utils/http.py
@@ -5,12 +5,6 @@
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.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'])
View
5 django/views/static.py
@@ -9,15 +9,12 @@
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):
View
9 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
~~~
View
7 tests/admin_views/tests.py
@@ -5,10 +5,6 @@
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.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,
View
7 tests/file_storage/tests.py
@@ -8,10 +8,6 @@
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.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.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
View
4 tests/requests/tests.py
@@ -18,6 +18,7 @@
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 @@ def test_alternate_charset_POST(self):
"""
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),
View
6 tests/servers/tests.py
@@ -5,16 +5,14 @@
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
View
5 tests/staticfiles_tests/test_liveserver.py
@@ -5,13 +5,10 @@
"""
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
View
5 tests/template_tests/tests.py
@@ -13,10 +13,6 @@
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.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
View
5 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.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"
Please sign in to comment.
Something went wrong with that request. Please try again.