[py3] Added Python 3 compatibility for xrange.

This commit is contained in:
Aymeric Augustin 2012-07-20 18:53:11 +02:00
parent ca07fda2ef
commit a84d79f572
32 changed files with 77 additions and 53 deletions

View File

@ -46,6 +46,7 @@ from django.contrib.gis.gdal.layer import Layer
from django.contrib.gis.gdal.prototypes import ds as capi from django.contrib.gis.gdal.prototypes import ds as capi
from django.utils import six from django.utils import six
from django.utils.six.moves import xrange
# For more information, see the OGR C API source code: # For more information, see the OGR C API source code:
# http://www.gdal.org/ogr/ogr__api_8h.html # http://www.gdal.org/ogr/ogr__api_8h.html

View File

@ -8,6 +8,7 @@ from django.contrib.gis.gdal.geometries import OGRGeometry, OGRGeomType
from django.contrib.gis.gdal.prototypes import ds as capi, geom as geom_api from django.contrib.gis.gdal.prototypes import ds as capi, geom as geom_api
from django.utils import six from django.utils import six
from django.utils.six.moves import xrange
# For more information, see the OGR C API source code: # For more information, see the OGR C API source code:
# http://www.gdal.org/ogr/ogr__api_8h.html # http://www.gdal.org/ogr/ogr__api_8h.html

View File

@ -58,6 +58,7 @@ from django.contrib.gis.gdal.prototypes import geom as capi, srs as srs_api
from django.contrib.gis.geometry.regex import hex_regex, wkt_regex, json_regex from django.contrib.gis.geometry.regex import hex_regex, wkt_regex, json_regex
from django.utils import six from django.utils import six
from django.utils.six.moves import xrange
# For more information, see the OGR C API source code: # For more information, see the OGR C API source code:
# http://www.gdal.org/ogr/ogr__api_8h.html # http://www.gdal.org/ogr/ogr__api_8h.html

View File

@ -15,6 +15,7 @@ from django.contrib.gis.gdal.srs import SpatialReference
from django.contrib.gis.gdal.prototypes import ds as capi, geom as geom_api, srs as srs_api from django.contrib.gis.gdal.prototypes import ds as capi, geom as geom_api, srs as srs_api
from django.utils import six from django.utils import six
from django.utils.six.moves import xrange
# For more information, see the OGR C API source code: # For more information, see the OGR C API source code:
# http://www.gdal.org/ogr/ogr__api_8h.html # http://www.gdal.org/ogr/ogr__api_8h.html

View File

@ -7,6 +7,7 @@ except ImportError:
from django.contrib.gis.gdal import (OGRGeometry, OGRGeomType, OGRException, from django.contrib.gis.gdal import (OGRGeometry, OGRGeomType, OGRException,
OGRIndexError, SpatialReference, CoordTransform, GDAL_VERSION) OGRIndexError, SpatialReference, CoordTransform, GDAL_VERSION)
from django.contrib.gis.geometry.test_data import TestDataMixin from django.contrib.gis.geometry.test_data import TestDataMixin
from django.utils.six.moves import xrange
from django.utils import unittest from django.utils import unittest
class OGRGeomTest(unittest.TestCase, TestDataMixin): class OGRGeomTest(unittest.TestCase, TestDataMixin):

View File

@ -10,6 +10,7 @@ from django.contrib.gis.geos.linestring import LineString, LinearRing
from django.contrib.gis.geos.point import Point from django.contrib.gis.geos.point import Point
from django.contrib.gis.geos.polygon import Polygon from django.contrib.gis.geos.polygon import Polygon
from django.contrib.gis.geos import prototypes as capi from django.contrib.gis.geos import prototypes as capi
from django.utils.six.moves import xrange
class GeometryCollection(GEOSGeometry): class GeometryCollection(GEOSGeometry):
_typeid = 7 _typeid = 7

View File

@ -8,6 +8,7 @@ from django.contrib.gis.geos.base import GEOSBase, numpy
from django.contrib.gis.geos.error import GEOSException, GEOSIndexError from django.contrib.gis.geos.error import GEOSException, GEOSIndexError
from django.contrib.gis.geos.libgeos import CS_PTR from django.contrib.gis.geos.libgeos import CS_PTR
from django.contrib.gis.geos import prototypes as capi from django.contrib.gis.geos import prototypes as capi
from django.utils.six.moves import xrange
class GEOSCoordSeq(GEOSBase): class GEOSCoordSeq(GEOSBase):
"The internal representation of a list of coordinates inside a Geometry." "The internal representation of a list of coordinates inside a Geometry."

View File

@ -4,6 +4,7 @@ from django.contrib.gis.geos.error import GEOSException
from django.contrib.gis.geos.geometry import GEOSGeometry from django.contrib.gis.geos.geometry import GEOSGeometry
from django.contrib.gis.geos.point import Point from django.contrib.gis.geos.point import Point
from django.contrib.gis.geos import prototypes as capi from django.contrib.gis.geos import prototypes as capi
from django.utils.six.moves import xrange
class LineString(GEOSGeometry): class LineString(GEOSGeometry):
_init_func = capi.create_linestring _init_func = capi.create_linestring

View File

@ -10,6 +10,7 @@ Author: Aryeh Leib Taurog.
""" """
from django.utils.functional import total_ordering from django.utils.functional import total_ordering
from django.utils import six from django.utils import six
from django.utils.six.moves import xrange
@total_ordering @total_ordering
class ListMixin(object): class ListMixin(object):

View File

@ -3,6 +3,7 @@ from django.contrib.gis.geos.error import GEOSException
from django.contrib.gis.geos.geometry import GEOSGeometry from django.contrib.gis.geos.geometry import GEOSGeometry
from django.contrib.gis.geos import prototypes as capi from django.contrib.gis.geos import prototypes as capi
from django.utils import six from django.utils import six
from django.utils.six.moves import xrange
class Point(GEOSGeometry): class Point(GEOSGeometry):
_minlength = 2 _minlength = 2

View File

@ -4,6 +4,7 @@ from django.contrib.gis.geos.libgeos import get_pointer_arr, GEOM_PTR
from django.contrib.gis.geos.linestring import LinearRing from django.contrib.gis.geos.linestring import LinearRing
from django.contrib.gis.geos import prototypes as capi from django.contrib.gis.geos import prototypes as capi
from django.utils import six from django.utils import six
from django.utils.six.moves import xrange
class Polygon(GEOSGeometry): class Polygon(GEOSGeometry):
_minlength = 1 _minlength = 1

View File

@ -7,6 +7,7 @@ from django.contrib.gis.geos.libgeos import GEOM_PTR, GEOS_PREPARE
from django.contrib.gis.geos.prototypes.errcheck import check_dbl, check_string from django.contrib.gis.geos.prototypes.errcheck import check_dbl, check_string
from django.contrib.gis.geos.prototypes.geom import geos_char_p from django.contrib.gis.geos.prototypes.geom import geos_char_p
from django.contrib.gis.geos.prototypes.threadsafe import GEOSFunc from django.contrib.gis.geos.prototypes.threadsafe import GEOSFunc
from django.utils.six.moves import xrange
__all__ = ['geos_area', 'geos_distance', 'geos_length'] __all__ = ['geos_area', 'geos_distance', 'geos_length']

View File

@ -9,6 +9,7 @@ from django.contrib.gis.geos.libgeos import GEOS_PREPARE
from django.contrib.gis.geometry.test_data import TestDataMixin from django.contrib.gis.geometry.test_data import TestDataMixin
from django.utils import six from django.utils import six
from django.utils.six.moves import xrange
from django.utils import unittest from django.utils import unittest

View File

@ -2,6 +2,7 @@ from django.conf import settings
from django.template.loader import render_to_string from django.template.loader import render_to_string
from django.utils.html import format_html from django.utils.html import format_html
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
from django.utils.six.moves import xrange
from django.contrib.gis.maps.google.overlays import GPolygon, GPolyline, GMarker from django.contrib.gis.maps.google.overlays import GPolygon, GPolyline, GMarker

View File

@ -1,5 +1,6 @@
from django.contrib.gis.geos import GEOSGeometry, LinearRing, Polygon, Point from django.contrib.gis.geos import GEOSGeometry, LinearRing, Polygon, Point
from django.contrib.gis.maps.google.gmap import GoogleMapException from django.contrib.gis.maps.google.gmap import GoogleMapException
from django.utils.six.moves import xrange
from math import pi, sin, log, exp, atan from math import pi, sin, log, exp, atan
# Constants used for degree to radian conversion, and vice-versa. # Constants used for degree to radian conversion, and vice-versa.

View File

@ -148,7 +148,7 @@ class MXRFCField(RegexField):
if len(rfc) == 11: if len(rfc) == 11:
rfc = '-' + rfc rfc = '-' + rfc
sum_ = sum(i * chars.index(c) for i, c in zip(reversed(xrange(14)), rfc)) sum_ = sum(i * chars.index(c) for i, c in zip(reversed(range(14)), rfc))
checksum = 11 - sum_ % 11 checksum = 11 - sum_ % 11
if checksum == 10: if checksum == 10:
@ -215,7 +215,7 @@ class MXCURPField(RegexField):
def _checksum(self, value): def _checksum(self, value):
chars = '0123456789ABCDEFGHIJKLMN&OPQRSTUVWXYZ' chars = '0123456789ABCDEFGHIJKLMN&OPQRSTUVWXYZ'
s = sum(i * chars.index(c) for i, c in zip(reversed(xrange(19)), value)) s = sum(i * chars.index(c) for i, c in zip(reversed(range(19)), value))
checksum = 10 - s % 10 checksum = 10 - s % 10
if checksum == 10: if checksum == 10:

View File

@ -80,10 +80,10 @@ class TRIdentificationNumberField(Field):
raise ValidationError(self.error_messages['invalid']) raise ValidationError(self.error_messages['invalid'])
if int(value[0]) == 0: if int(value[0]) == 0:
raise ValidationError(self.error_messages['invalid']) raise ValidationError(self.error_messages['invalid'])
chksum = (sum([int(value[i]) for i in xrange(0,9,2)])*7- chksum = (sum([int(value[i]) for i in range(0, 9, 2)]) * 7 -
sum([int(value[i]) for i in xrange(1,9,2)])) % 10 sum([int(value[i]) for i in range(1, 9, 2)])) % 10
if chksum != int(value[9]) or \ if chksum != int(value[9]) or \
(sum([int(value[i]) for i in xrange(10)]) % 10) != int(value[10]): (sum([int(value[i]) for i in range(10)]) % 10) != int(value[10]):
raise ValidationError(self.error_messages['invalid']) raise ValidationError(self.error_messages['invalid'])
return value return value

View File

@ -152,7 +152,7 @@ class BaseTest(TestCase):
cycle. cycle.
""" """
data = { data = {
'messages': ['Test message %d' % x for x in xrange(10)], 'messages': ['Test message %d' % x for x in range(10)],
} }
show_url = reverse('django.contrib.messages.tests.urls.show') show_url = reverse('django.contrib.messages.tests.urls.show')
for level in ('debug', 'info', 'success', 'warning', 'error'): for level in ('debug', 'info', 'success', 'warning', 'error'):
@ -170,7 +170,7 @@ class BaseTest(TestCase):
@override_settings(MESSAGE_LEVEL=constants.DEBUG) @override_settings(MESSAGE_LEVEL=constants.DEBUG)
def test_with_template_response(self): def test_with_template_response(self):
data = { data = {
'messages': ['Test message %d' % x for x in xrange(10)], 'messages': ['Test message %d' % x for x in range(10)],
} }
show_url = reverse('django.contrib.messages.tests.urls.show_template_response') show_url = reverse('django.contrib.messages.tests.urls.show_template_response')
for level in self.levels.keys(): for level in self.levels.keys():
@ -194,7 +194,7 @@ class BaseTest(TestCase):
before a GET. before a GET.
""" """
data = { data = {
'messages': ['Test message %d' % x for x in xrange(10)], 'messages': ['Test message %d' % x for x in range(10)],
} }
show_url = reverse('django.contrib.messages.tests.urls.show') show_url = reverse('django.contrib.messages.tests.urls.show')
messages = [] messages = []
@ -226,7 +226,7 @@ class BaseTest(TestCase):
when one attempts to store a message. when one attempts to store a message.
""" """
data = { data = {
'messages': ['Test message %d' % x for x in xrange(10)], 'messages': ['Test message %d' % x for x in range(10)],
} }
show_url = reverse('django.contrib.messages.tests.urls.show') show_url = reverse('django.contrib.messages.tests.urls.show')
for level in ('debug', 'info', 'success', 'warning', 'error'): for level in ('debug', 'info', 'success', 'warning', 'error'):
@ -251,7 +251,7 @@ class BaseTest(TestCase):
raised if 'fail_silently' = True raised if 'fail_silently' = True
""" """
data = { data = {
'messages': ['Test message %d' % x for x in xrange(10)], 'messages': ['Test message %d' % x for x in range(10)],
'fail_silently': True, 'fail_silently': True,
} }
show_url = reverse('django.contrib.messages.tests.urls.show') show_url = reverse('django.contrib.messages.tests.urls.show')

View File

@ -123,7 +123,7 @@ class CookieTest(BaseTest):
{ {
'message': Message(constants.INFO, 'Test message'), 'message': Message(constants.INFO, 'Test message'),
'message_list': [Message(constants.INFO, 'message %s') \ 'message_list': [Message(constants.INFO, 'message %s') \
for x in xrange(5)] + [{'another-message': \ for x in range(5)] + [{'another-message': \
Message(constants.ERROR, 'error')}], Message(constants.ERROR, 'error')}],
}, },
Message(constants.INFO, 'message %s'), Message(constants.INFO, 'message %s'),

View File

@ -1,5 +1,6 @@
from django.contrib.sessions.backends.base import SessionBase, CreateError from django.contrib.sessions.backends.base import SessionBase, CreateError
from django.core.cache import cache from django.core.cache import cache
from django.utils.six.moves import xrange
KEY_PREFIX = "django.contrib.sessions.cache" KEY_PREFIX = "django.contrib.sessions.cache"

View File

@ -12,6 +12,7 @@ from django.utils import tree
from django.db.models.fields import Field from django.db.models.fields import Field
from django.db.models.sql.datastructures import EmptyResultSet from django.db.models.sql.datastructures import EmptyResultSet
from django.db.models.sql.aggregates import Aggregate from django.db.models.sql.aggregates import Aggregate
from django.utils.six.moves import xrange
# Connection types # Connection types
AND = 'AND' AND = 'AND'

View File

@ -2,6 +2,7 @@ import weakref
import threading import threading
from django.dispatch import saferef from django.dispatch import saferef
from django.utils.six.moves import xrange
WEAKREF_TYPES = (weakref.ReferenceType, saferef.BoundMethodWeakref) WEAKREF_TYPES = (weakref.ReferenceType, saferef.BoundMethodWeakref)

View File

@ -8,6 +8,7 @@ from django.forms.widgets import Media, HiddenInput
from django.utils.encoding import StrAndUnicode from django.utils.encoding import StrAndUnicode
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
from django.utils import six from django.utils import six
from django.utils.six.moves import xrange
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _

View File

@ -24,6 +24,7 @@ except NotImplementedError:
from django.conf import settings from django.conf import settings
from django.utils.encoding import smart_str from django.utils.encoding import smart_str
from django.utils.six.moves import xrange
_trans_5c = b"".join([chr(x ^ 0x5C) for x in xrange(256)]) _trans_5c = b"".join([chr(x ^ 0x5C) for x in xrange(256)])

View File

@ -6,7 +6,7 @@ def _resolve_name(name, package, level):
if not hasattr(package, 'rindex'): if not hasattr(package, 'rindex'):
raise ValueError("'package' not set to a string") raise ValueError("'package' not set to a string")
dot = len(package) dot = len(package)
for x in xrange(level, 1, -1): for x in range(level, 1, -1):
try: try:
dot = package.rindex('.', 0, dot) dot = package.rindex('.', 0, dot)
except ValueError: except ValueError:

View File

@ -2,6 +2,7 @@
# Copyright 2007 Google Inc. http://code.google.com/p/ipaddr-py/ # Copyright 2007 Google Inc. http://code.google.com/p/ipaddr-py/
# Licensed under the Apache License, Version 2.0 (the "License"). # Licensed under the Apache License, Version 2.0 (the "License").
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.utils.six.moves import xrange
def clean_ipv6_address(ip_str, unpack_ipv4=False, def clean_ipv6_address(ip_str, unpack_ipv4=False,
error_message="This is not a valid IPv6 address"): error_message="This is not a valid IPv6 address"):

View File

@ -2,6 +2,7 @@ from __future__ import absolute_import
from django.db import models, IntegrityError from django.db import models, IntegrityError
from django.test import TestCase, skipUnlessDBFeature, skipIfDBFeature from django.test import TestCase, skipUnlessDBFeature, skipIfDBFeature
from django.utils.six.moves import xrange
from .models import (R, RChild, S, T, U, A, M, MR, MRNull, from .models import (R, RChild, S, T, U, A, M, MR, MRNull,
create_a, get_default_r, User, Avatar, HiddenUser, HiddenUserProfile) create_a, get_default_r, User, Avatar, HiddenUser, HiddenUserProfile)

View File

@ -88,7 +88,7 @@ class ManyToOneNullTests(TestCase):
def test_clear_efficiency(self): def test_clear_efficiency(self):
r = Reporter.objects.create() r = Reporter.objects.create()
for _ in xrange(3): for _ in range(3):
r.article_set.create() r.article_set.create()
with self.assertNumQueries(1): with self.assertNumQueries(1):
r.article_set.clear() r.article_set.clear()

View File

@ -17,6 +17,7 @@ from django.test import (TestCase, skipUnlessDBFeature, skipIfDBFeature,
TransactionTestCase) TransactionTestCase)
from django.test.utils import override_settings from django.test.utils import override_settings
from django.utils import six from django.utils import six
from django.utils.six.moves import xrange
from django.utils import unittest from django.utils import unittest
from . import models from . import models
@ -531,7 +532,7 @@ class ThreadTests(TestCase):
from django.db import connection from django.db import connection
connection.cursor() connection.cursor()
connections_set.add(connection.connection) connections_set.add(connection.connection)
for x in xrange(2): for x in range(2):
t = threading.Thread(target=runner) t = threading.Thread(target=runner)
t.start() t.start()
t.join() t.join()
@ -558,7 +559,7 @@ class ThreadTests(TestCase):
# main thread. # main thread.
conn.allow_thread_sharing = True conn.allow_thread_sharing = True
connections_set.add(conn) connections_set.add(conn)
for x in xrange(2): for x in range(2):
t = threading.Thread(target=runner) t = threading.Thread(target=runner)
t.start() t.start()
t.join() t.join()

View File

@ -1,7 +1,7 @@
from django.dispatch.saferef import safeRef from django.dispatch.saferef import safeRef
from django.utils.six.moves import xrange
from django.utils import unittest from django.utils import unittest
class Test1(object): class Test1(object):
def x(self): def x(self):
pass pass

View File

@ -15,6 +15,7 @@ from django.middleware.http import ConditionalGetMiddleware
from django.middleware.gzip import GZipMiddleware from django.middleware.gzip import GZipMiddleware
from django.test import TestCase, RequestFactory from django.test import TestCase, RequestFactory
from django.test.utils import override_settings from django.test.utils import override_settings
from django.utils.six.moves import xrange
class CommonMiddlewareTest(TestCase): class CommonMiddlewareTest(TestCase):
def setUp(self): def setUp(self):

View File

@ -98,7 +98,7 @@ class SortedDictTests(SimpleTestCase):
self.assertEqual(l - len(self.d1), 1) self.assertEqual(l - len(self.d1), 1)
def test_dict_equality(self): def test_dict_equality(self):
d = SortedDict((i, i) for i in xrange(3)) d = SortedDict((i, i) for i in range(3))
self.assertEqual(d, {0: 0, 1: 1, 2: 2}) self.assertEqual(d, {0: 0, 1: 1, 2: 2})
def test_tuple_init(self): def test_tuple_init(self):