From ca07fda2efea24cb43423b884fa4648d44e52963 Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Fri, 20 Jul 2012 16:16:57 +0200 Subject: [PATCH] [py3] Switched to Python 3-compatible imports. xrange/range will be dealt with in a separate commit due to the huge number of changes. --- django/contrib/auth/tests/basic.py | 6 ++--- django/contrib/auth/tests/management.py | 3 +-- django/contrib/gis/db/models/sql/compiler.py | 2 +- django/contrib/gis/gdal/tests/test_geom.py | 2 +- django/contrib/gis/geos/tests/test_geos.py | 3 ++- django/contrib/gis/utils/ogrinspect.py | 2 +- django/contrib/sessions/backends/base.py | 2 +- .../sessions/backends/signed_cookies.py | 2 +- django/core/cache/backends/db.py | 2 +- django/core/cache/backends/filebased.py | 2 +- django/core/cache/backends/locmem.py | 2 +- django/core/servers/basehttp.py | 4 ++-- django/db/backends/__init__.py | 2 +- django/db/models/base.py | 2 +- django/db/models/sql/compiler.py | 2 +- django/http/__init__.py | 22 +++++++++---------- django/test/_doctest.py | 2 +- django/test/html.py | 3 +-- django/utils/autoreload.py | 2 +- django/utils/html_parser.py | 18 ++++++++------- django/utils/itercompat.py | 6 ++--- django/utils/six.py | 5 +++++ django/utils/text.py | 4 ++-- django/utils/translation/trans_real.py | 2 +- 24 files changed, 54 insertions(+), 48 deletions(-) diff --git a/django/contrib/auth/tests/basic.py b/django/contrib/auth/tests/basic.py index 4c2e8cd8aa..21acb2004f 100644 --- a/django/contrib/auth/tests/basic.py +++ b/django/contrib/auth/tests/basic.py @@ -1,8 +1,8 @@ -from django.test import TestCase -from django.utils.unittest import skipUnless from django.contrib.auth.models import User, AnonymousUser from django.core.management import call_command -from StringIO import StringIO +from django.test import TestCase +from django.utils.six import StringIO +from django.utils.unittest import skipUnless try: import crypt as crypt_module diff --git a/django/contrib/auth/tests/management.py b/django/contrib/auth/tests/management.py index 8d1f7c7965..c98b7491c8 100644 --- a/django/contrib/auth/tests/management.py +++ b/django/contrib/auth/tests/management.py @@ -1,11 +1,10 @@ from __future__ import unicode_literals -from StringIO import StringIO - from django.contrib.auth import models, management from django.contrib.auth.management.commands import changepassword from django.core.management.base import CommandError from django.test import TestCase +from django.utils.six import StringIO class GetDefaultUsernameTestCase(TestCase): diff --git a/django/contrib/gis/db/models/sql/compiler.py b/django/contrib/gis/db/models/sql/compiler.py index ebaee60bd0..d016357f1b 100644 --- a/django/contrib/gis/db/models/sql/compiler.py +++ b/django/contrib/gis/db/models/sql/compiler.py @@ -1,4 +1,4 @@ -from future_builtins import zip +from django.utils.six.moves import zip from django.db.backends.util import truncate_name, typecast_timestamp from django.db.models.sql import compiler diff --git a/django/contrib/gis/gdal/tests/test_geom.py b/django/contrib/gis/gdal/tests/test_geom.py index 20e25946b0..e5c550b0d0 100644 --- a/django/contrib/gis/gdal/tests/test_geom.py +++ b/django/contrib/gis/gdal/tests/test_geom.py @@ -1,6 +1,6 @@ from binascii import b2a_hex try: - import cPickle as pickle + from django.utils.six.moves import cPickle as pickle except ImportError: import pickle diff --git a/django/contrib/gis/geos/tests/test_geos.py b/django/contrib/gis/geos/tests/test_geos.py index b1d00d5241..102c6ba55a 100644 --- a/django/contrib/gis/geos/tests/test_geos.py +++ b/django/contrib/gis/geos/tests/test_geos.py @@ -952,7 +952,8 @@ class GEOSTest(unittest.TestCase, TestDataMixin): def test_pickle(self): "Testing pickling and unpickling support." # Using both pickle and cPickle -- just 'cause. - import pickle, cPickle + from django.utils.six.moves import cPickle + import pickle # Creating a list of test geometries for pickling, # and setting the SRID on some of them. diff --git a/django/contrib/gis/utils/ogrinspect.py b/django/contrib/gis/utils/ogrinspect.py index f87bb24c7f..f8977059d9 100644 --- a/django/contrib/gis/utils/ogrinspect.py +++ b/django/contrib/gis/utils/ogrinspect.py @@ -5,7 +5,7 @@ models for GeoDjango and/or mapping dictionaries for use with the Author: Travis Pinney, Dane Springmeyer, & Justin Bronn """ -from future_builtins import zip +from django.utils.six.moves import zip # Requires GDAL to use. from django.contrib.gis.gdal import DataSource from django.contrib.gis.gdal.field import OFTDate, OFTDateTime, OFTInteger, OFTReal, OFTString, OFTTime diff --git a/django/contrib/sessions/backends/base.py b/django/contrib/sessions/backends/base.py index 5a637e24d2..153cde9830 100644 --- a/django/contrib/sessions/backends/base.py +++ b/django/contrib/sessions/backends/base.py @@ -2,7 +2,7 @@ import base64 import time from datetime import datetime, timedelta try: - import cPickle as pickle + from django.utils.six.moves import cPickle as pickle except ImportError: import pickle diff --git a/django/contrib/sessions/backends/signed_cookies.py b/django/contrib/sessions/backends/signed_cookies.py index 2a0f261441..41ba7af634 100644 --- a/django/contrib/sessions/backends/signed_cookies.py +++ b/django/contrib/sessions/backends/signed_cookies.py @@ -1,5 +1,5 @@ try: - import cPickle as pickle + from django.utils.six.moves import cPickle as pickle except ImportError: import pickle diff --git a/django/core/cache/backends/db.py b/django/core/cache/backends/db.py index 1ac6ef5d9b..f60b4e0cd1 100644 --- a/django/core/cache/backends/db.py +++ b/django/core/cache/backends/db.py @@ -4,7 +4,7 @@ import time from datetime import datetime try: - import cPickle as pickle + from django.utils.six.moves import cPickle as pickle except ImportError: import pickle diff --git a/django/core/cache/backends/filebased.py b/django/core/cache/backends/filebased.py index 7f9f7175be..1170996a76 100644 --- a/django/core/cache/backends/filebased.py +++ b/django/core/cache/backends/filebased.py @@ -5,7 +5,7 @@ import os import shutil import time try: - import cPickle as pickle + from django.utils.six.moves import cPickle as pickle except ImportError: import pickle diff --git a/django/core/cache/backends/locmem.py b/django/core/cache/backends/locmem.py index 9196b3b42b..76667e9609 100644 --- a/django/core/cache/backends/locmem.py +++ b/django/core/cache/backends/locmem.py @@ -2,7 +2,7 @@ import time try: - import cPickle as pickle + from django.utils.six.moves import cPickle as pickle except ImportError: import pickle diff --git a/django/core/servers/basehttp.py b/django/core/servers/basehttp.py index f67b105397..2db4e5ef6a 100644 --- a/django/core/servers/basehttp.py +++ b/django/core/servers/basehttp.py @@ -16,7 +16,7 @@ try: except ImportError: # Python 2 from urllib import unquote from urlparse import urljoin -from SocketServer import ThreadingMixIn +from django.utils.six.moves import socketserver from wsgiref import simple_server from wsgiref.util import FileWrapper # for backwards compatibility @@ -200,7 +200,7 @@ class WSGIRequestHandler(simple_server.WSGIRequestHandler, object): def run(addr, port, wsgi_handler, ipv6=False, threading=False): server_address = (addr, port) if threading: - httpd_cls = type('WSGIServer', (ThreadingMixIn, WSGIServer), {}) + httpd_cls = type('WSGIServer', (socketserver.ThreadingMixIn, WSGIServer), {}) else: httpd_cls = WSGIServer httpd = httpd_cls(server_address, WSGIRequestHandler, ipv6=ipv6) diff --git a/django/db/backends/__init__.py b/django/db/backends/__init__.py index b416343f88..a896f5fd08 100644 --- a/django/db/backends/__init__.py +++ b/django/db/backends/__init__.py @@ -3,7 +3,7 @@ from django.db.utils import DatabaseError try: import thread except ImportError: - import dummy_thread as thread + from django.utils.six.moves import _dummy_thread as thread from contextlib import contextmanager from django.conf import settings diff --git a/django/db/models/base.py b/django/db/models/base.py index 8c448b2f39..567fb53217 100644 --- a/django/db/models/base.py +++ b/django/db/models/base.py @@ -3,7 +3,7 @@ from __future__ import unicode_literals import copy import sys from functools import update_wrapper -from future_builtins import zip +from django.utils.six.moves import zip import django.db.models.manager # Imported to register signal handler. from django.conf import settings diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py index d44cdfe4a4..7a0afa349d 100644 --- a/django/db/models/sql/compiler.py +++ b/django/db/models/sql/compiler.py @@ -1,4 +1,4 @@ -from future_builtins import zip +from django.utils.six.moves import zip from django.core.exceptions import FieldError from django.db import transaction diff --git a/django/http/__init__.py b/django/http/__init__.py index da97506c8c..6f9d70eebd 100644 --- a/django/http/__init__.py +++ b/django/http/__init__.py @@ -16,23 +16,23 @@ except ImportError: # Python 2 from urllib import quote, urlencode from urlparse import parse_qsl, urljoin -import Cookie +from django.utils.six.moves import http_cookies # Some versions of Python 2.7 and later won't need this encoding bug fix: -_cookie_encodes_correctly = Cookie.SimpleCookie().value_encode(';') == (';', '"\\073"') +_cookie_encodes_correctly = http_cookies.SimpleCookie().value_encode(';') == (';', '"\\073"') # See ticket #13007, http://bugs.python.org/issue2193 and http://trac.edgewall.org/ticket/2256 -_tc = Cookie.SimpleCookie() +_tc = http_cookies.SimpleCookie() try: _tc.load(b'foo:bar=1') _cookie_allows_colon_in_names = True -except Cookie.CookieError: +except http_cookies.CookieError: _cookie_allows_colon_in_names = False if _cookie_encodes_correctly and _cookie_allows_colon_in_names: - SimpleCookie = Cookie.SimpleCookie + SimpleCookie = http_cookies.SimpleCookie else: - Morsel = Cookie.Morsel + Morsel = http_cookies.Morsel - class SimpleCookie(Cookie.SimpleCookie): + class SimpleCookie(http_cookies.SimpleCookie): if not _cookie_encodes_correctly: def value_encode(self, val): # Some browsers do not support quoted-string from RFC 2109, @@ -73,9 +73,9 @@ else: M = self.get(key, Morsel()) M.set(key, real_value, coded_value) dict.__setitem__(self, key, M) - except Cookie.CookieError: + except http_cookies.CookieError: self.bad_cookies.add(key) - dict.__setitem__(self, key, Cookie.Morsel()) + dict.__setitem__(self, key, http_cookies.Morsel()) from django.conf import settings @@ -495,11 +495,11 @@ class QueryDict(MultiValueDict): def parse_cookie(cookie): if cookie == '': return {} - if not isinstance(cookie, Cookie.BaseCookie): + if not isinstance(cookie, http_cookies.BaseCookie): try: c = SimpleCookie() c.load(cookie) - except Cookie.CookieError: + except http_cookies.CookieError: # Invalid cookie return {} else: diff --git a/django/test/_doctest.py b/django/test/_doctest.py index 75f16e202a..316c785f33 100644 --- a/django/test/_doctest.py +++ b/django/test/_doctest.py @@ -103,9 +103,9 @@ import __future__ import sys, traceback, inspect, linecache, os, re import unittest, difflib, pdb, tempfile import warnings -from StringIO import StringIO from django.utils import six +from django.utils.six import StringIO if sys.platform.startswith('java'): # On Jython, isclass() reports some modules as classes. Patch it. diff --git a/django/test/html.py b/django/test/html.py index 8d577d91fd..2a1421bf17 100644 --- a/django/test/html.py +++ b/django/test/html.py @@ -5,9 +5,8 @@ Comparing two html documents. from __future__ import unicode_literals import re -from HTMLParser import HTMLParseError from django.utils.encoding import force_unicode -from django.utils.html_parser import HTMLParser +from django.utils.html_parser import HTMLParser, HTMLParseError from django.utils import six diff --git a/django/utils/autoreload.py b/django/utils/autoreload.py index 85d9907856..b6c055383c 100644 --- a/django/utils/autoreload.py +++ b/django/utils/autoreload.py @@ -33,7 +33,7 @@ import os, sys, time, signal try: import thread except ImportError: - import dummy_thread as thread + from django.utils.six.moves import _dummy_thread as thread # This import does nothing, but it's necessary to avoid some race conditions # in the threading module. See http://code.djangoproject.com/ticket/2330 . diff --git a/django/utils/html_parser.py b/django/utils/html_parser.py index 98f6545c41..ee56c01aec 100644 --- a/django/utils/html_parser.py +++ b/django/utils/html_parser.py @@ -1,26 +1,28 @@ -import HTMLParser as _HTMLParser +from django.utils.six.moves import html_parser as _html_parser import re tagfind = re.compile('([a-zA-Z][-.a-zA-Z0-9:_]*)(?:\s|/(?!>))*') -class HTMLParser(_HTMLParser.HTMLParser): +HTMLParseError = _html_parser.HTMLParseError + +class HTMLParser(_html_parser.HTMLParser): """ Patched version of stdlib's HTMLParser with patch from: http://bugs.python.org/issue670664 """ def __init__(self): - _HTMLParser.HTMLParser.__init__(self) + _html_parser.HTMLParser.__init__(self) self.cdata_tag = None def set_cdata_mode(self, tag): try: - self.interesting = _HTMLParser.interesting_cdata + self.interesting = _html_parser.interesting_cdata except AttributeError: self.interesting = re.compile(r'' % tag.lower(), re.I) self.cdata_tag = tag.lower() def clear_cdata_mode(self): - self.interesting = _HTMLParser.interesting_normal + self.interesting = _html_parser.interesting_normal self.cdata_tag = None # Internal -- handle starttag, return end or -1 if not terminated @@ -40,7 +42,7 @@ class HTMLParser(_HTMLParser.HTMLParser): self.lasttag = tag = match.group(1).lower() while k < endpos: - m = _HTMLParser.attrfind.match(rawdata, k) + m = _html_parser.attrfind.match(rawdata, k) if not m: break attrname, rest, attrvalue = m.group(1, 2, 3) @@ -78,11 +80,11 @@ class HTMLParser(_HTMLParser.HTMLParser): def parse_endtag(self, i): rawdata = self.rawdata assert rawdata[i:i + 2] == " + match = _html_parser.endendtag.search(rawdata, i + 1) # > if not match: return -1 j = match.end() - match = _HTMLParser.endtagfind.match(rawdata, i) # + match = _html_parser.endtagfind.match(rawdata, i) # if not match: if self.cdata_tag is not None: # *** add *** self.handle_data(rawdata[i:j]) # *** add *** diff --git a/django/utils/itercompat.py b/django/utils/itercompat.py index 2f016b1c3f..aa329c152e 100644 --- a/django/utils/itercompat.py +++ b/django/utils/itercompat.py @@ -4,7 +4,7 @@ Where possible, we try to use the system-native version and only fall back to these implementations if necessary. """ -import __builtin__ +from django.utils.six.moves import builtins import itertools import warnings @@ -25,9 +25,9 @@ def product(*args, **kwds): def all(iterable): warnings.warn("django.utils.itercompat.all is deprecated; use the native version instead", DeprecationWarning) - return __builtin__.all(iterable) + return builtins.all(iterable) def any(iterable): warnings.warn("django.utils.itercompat.any is deprecated; use the native version instead", DeprecationWarning) - return __builtin__.any(iterable) + return builtins.any(iterable) diff --git a/django/utils/six.py b/django/utils/six.py index b1d58e5668..c74f9fa7df 100644 --- a/django/utils/six.py +++ b/django/utils/six.py @@ -351,3 +351,8 @@ _add_doc(reraise, """Reraise an exception.""") def with_metaclass(meta, base=object): """Create a base class with a metaclass.""" return meta("NewBase", (base,), {}) + + +### Additional customizations for Django ### + +add_move(MovedModule("_dummy_thread", "dummy_thread")) diff --git a/django/utils/text.py b/django/utils/text.py index 9f773ad41b..43056aa634 100644 --- a/django/utils/text.py +++ b/django/utils/text.py @@ -4,7 +4,7 @@ import re import unicodedata import warnings from gzip import GzipFile -from htmlentitydefs import name2codepoint +from django.utils.six.moves import html_entities from io import BytesIO from django.utils.encoding import force_unicode @@ -349,7 +349,7 @@ def _replace_entity(match): return match.group(0) else: try: - return unichr(name2codepoint[text]) + return unichr(html_entities.name2codepoint[text]) except (ValueError, KeyError): return match.group(0) diff --git a/django/utils/translation/trans_real.py b/django/utils/translation/trans_real.py index 0cd13fd6f5..9ebcbf5441 100644 --- a/django/utils/translation/trans_real.py +++ b/django/utils/translation/trans_real.py @@ -6,11 +6,11 @@ import os import re import sys import gettext as gettext_module -from io import StringIO from threading import local from django.utils.importlib import import_module from django.utils.safestring import mark_safe, SafeData +from django.utils.six import StringIO # Translations are cached in a dictionary for every language+app tuple.