Replace xrange() with six.moves.range()

Patch generated by the xrange operation of the sixer tool:
https://pypi.python.org/pypi/sixer

Manual changes:

* Fix indentation for pep8 checks
* Fix TestGreenthreadSafeIterator.test_access_is_serialized of
  test.unit.common.test_utils:
  replace range(1, 11) with list(range(1, 11))
* Fix UnsafeXrange docstring, revert change

Change-Id: Icb7e26135c5e57b5302b8bfe066b33cafe69fe4d
This commit is contained in:
Victor Stinner 2015-05-25 18:28:02 +02:00 committed by Christian Schwede
parent 0009a43eb4
commit e5c962a28c
35 changed files with 170 additions and 141 deletions

View File

@ -478,7 +478,7 @@ class AccountBroker(DatabaseBroker):
row = curs_row.fetchone()
if row:
row = list(row)
for i in xrange(5):
for i in range(5):
if record[i] is None and row[i] is not None:
record[i] = row[i]
if row[1] > record[1]: # Keep newest put_timestamp

View File

@ -16,6 +16,7 @@
from eventlet import sleep, Timeout
from eventlet.green import httplib, socket, urllib2
import json
from six.moves import range
import struct
from sys import exc_info
import zlib
@ -171,7 +172,7 @@ class InternalClient(object):
headers = dict(headers)
headers['user-agent'] = self.user_agent
resp = exc_type = exc_value = exc_traceback = None
for attempt in xrange(self.request_tries):
for attempt in range(self.request_tries):
req = Request.blank(
path, environ={'REQUEST_METHOD': method}, headers=headers)
if body_file is not None:

View File

@ -54,6 +54,7 @@ from hashlib import md5
from eventlet.green import socket
from eventlet.pools import Pool
from eventlet import Timeout
from six.moves import range
from swift.common.utils import json
@ -140,7 +141,7 @@ class MemcacheRing(object):
self._errors = dict(((serv, []) for serv in servers))
self._error_limited = dict(((serv, 0) for serv in servers))
for server in sorted(servers):
for i in xrange(NODE_WEIGHT):
for i in range(NODE_WEIGHT):
self._ring[md5hash('%s-%s' % (server, i))] = server
self._tries = tries if tries <= len(servers) else len(servers)
self._sorted = sorted(self._ring)

View File

@ -27,6 +27,8 @@ maximum lookup depth. If a match is found, the environment's Host header is
rewritten and the request is passed further down the WSGI chain.
"""
from six.moves import range
import socket
from swift import gettext_ as _
@ -122,7 +124,7 @@ class CNAMELookupMiddleware(object):
if self.memcache is None:
self.memcache = cache_from_env(env)
error = True
for tries in xrange(self.lookup_depth):
for tries in range(self.lookup_depth):
found_domain = None
if self.memcache:
memcache_key = ''.join(['cname-', a_domain])

View File

@ -146,6 +146,8 @@ the manifest and the segments it's referring to) in the container and account
metadata which can be used for stats purposes.
"""
from six.moves import range
from cStringIO import StringIO
from datetime import datetime
import mimetypes
@ -205,7 +207,7 @@ class SloPutContext(WSGIContext):
def handle_slo_put(self, req, start_response):
app_resp = self._app_call(req.environ)
for i in xrange(len(self._response_headers)):
for i in range(len(self._response_headers)):
if self._response_headers[i][0].lower() == 'etag':
self._response_headers[i] = ('Etag', self.slo_etag)
break

View File

@ -25,6 +25,7 @@ from copy import deepcopy
from array import array
from collections import defaultdict
from six.moves import range
from time import time
from swift.common import exceptions
@ -534,7 +535,7 @@ class RingBuilder(object):
if stats:
# dev_usage[dev_id] will equal the number of partitions assigned to
# that device.
dev_usage = array('I', (0 for _junk in xrange(dev_len)))
dev_usage = array('I', (0 for _junk in range(dev_len)))
for part2dev in self._replica2part2dev:
for dev_id in part2dev:
dev_usage[dev_id] += 1
@ -607,7 +608,7 @@ class RingBuilder(object):
255 hours ago. This can be used to force a full rebalance on the next
call to rebalance.
"""
for part in xrange(self.parts):
for part in range(self.parts):
self._last_part_moves[part] = 0xff
def get_part_devices(self, part):
@ -713,12 +714,12 @@ class RingBuilder(object):
if len(part2dev) < desired_length:
# Not long enough: needs to be extended and the
# newly-added pieces assigned to devices.
for part in xrange(len(part2dev), desired_length):
for part in range(len(part2dev), desired_length):
to_assign[part].append(replica)
part2dev.append(0)
elif len(part2dev) > desired_length:
# Too long: truncate this mapping.
for part in xrange(desired_length, len(part2dev)):
for part in range(desired_length, len(part2dev)):
dev_losing_part = self.devs[part2dev[part]]
dev_losing_part['parts'] -= 1
removed_replicas += 1
@ -726,10 +727,10 @@ class RingBuilder(object):
else:
# Mapping not present at all: make one up and assign
# all of it.
for part in xrange(desired_length):
for part in range(desired_length):
to_assign[part].append(replica)
self._replica2part2dev.append(
array('H', (0 for _junk in xrange(desired_length))))
array('H', (0 for _junk in range(desired_length))))
return (to_assign.items(), removed_replicas)
@ -738,7 +739,7 @@ class RingBuilder(object):
Initial partition assignment is the same as rebalancing an
existing ring, but with some initial setup beforehand.
"""
self._last_part_moves = array('B', (0 for _junk in xrange(self.parts)))
self._last_part_moves = array('B', (0 for _junk in range(self.parts)))
self._last_part_moves_epoch = int(time())
self._set_parts_wanted()
@ -751,7 +752,7 @@ class RingBuilder(object):
more recently than min_part_hours.
"""
elapsed_hours = int(time() - self._last_part_moves_epoch) / 3600
for part in xrange(self.parts):
for part in range(self.parts):
# The "min(self._last_part_moves[part] + elapsed_hours, 0xff)"
# which was here showed up in profiling, so it got inlined.
last_plus_elapsed = self._last_part_moves[part] + elapsed_hours
@ -830,7 +831,7 @@ class RingBuilder(object):
max_allowed_replicas = self._build_max_replicas_by_tier()
wanted_parts_for_tier = self._get_available_parts()
moved_parts = 0
for part in xrange(self.parts):
for part in range(self.parts):
# Only move one replica at a time if possible.
if part in removed_dev_parts:
continue
@ -922,8 +923,8 @@ class RingBuilder(object):
# pattern (but scaled down) on sequential runs.
this_start = int(float(start) * len(part2dev) / self.parts)
for part in itertools.chain(xrange(this_start, len(part2dev)),
xrange(0, this_start)):
for part in itertools.chain(range(this_start, len(part2dev)),
range(0, this_start)):
if self._last_part_moves[part] < self.min_part_hours:
continue
if part in removed_dev_parts or part in spread_out_parts:
@ -1270,7 +1271,7 @@ class RingBuilder(object):
Generator yielding every (partition, replica) pair in the ring.
"""
for replica, part2dev in enumerate(self._replica2part2dev):
for part in xrange(len(part2dev)):
for part in range(len(part2dev)):
yield (part, replica)
@classmethod

View File

@ -27,6 +27,8 @@ from hashlib import md5
from itertools import chain
from tempfile import NamedTemporaryFile
from six.moves import range
from swift.common.utils import hash_path, validate_configuration, json
from swift.common.ring.utils import tiers_for_dev
@ -68,7 +70,7 @@ class RingData(object):
return ring_dict
partition_count = 1 << (32 - ring_dict['part_shift'])
for x in xrange(ring_dict['replica_count']):
for x in range(ring_dict['replica_count']):
ring_dict['replica2part2dev_id'].append(
array.array('H', gz_file.read(2 * partition_count)))
return ring_dict
@ -361,9 +363,9 @@ class Ring(object):
# Multiple loops for execution speed; the checks and bookkeeping get
# simpler as you go along
hit_all_regions = len(same_regions) == self._num_regions
for handoff_part in chain(xrange(start, parts, inc),
xrange(inc - ((parts - start) % inc),
start, inc)):
for handoff_part in chain(range(start, parts, inc),
range(inc - ((parts - start) % inc),
start, inc)):
if hit_all_regions:
# At this point, there are no regions left untouched, so we
# can stop looking.
@ -386,9 +388,9 @@ class Ring(object):
break
hit_all_zones = len(same_zones) == self._num_zones
for handoff_part in chain(xrange(start, parts, inc),
xrange(inc - ((parts - start) % inc),
start, inc)):
for handoff_part in chain(range(start, parts, inc),
range(inc - ((parts - start) % inc),
start, inc)):
if hit_all_zones:
# Much like we stopped looking for fresh regions before, we
# can now stop looking for fresh zones; there are no more.
@ -409,9 +411,9 @@ class Ring(object):
break
hit_all_ips = len(same_ips) == self._num_ips
for handoff_part in chain(xrange(start, parts, inc),
xrange(inc - ((parts - start) % inc),
start, inc)):
for handoff_part in chain(range(start, parts, inc),
range(inc - ((parts - start) % inc),
start, inc)):
if hit_all_ips:
# We've exhausted the pool of unused backends, so stop
# looking.
@ -430,9 +432,9 @@ class Ring(object):
break
hit_all_devs = len(used) == self._num_devs
for handoff_part in chain(xrange(start, parts, inc),
xrange(inc - ((parts - start) % inc),
start, inc)):
for handoff_part in chain(range(start, parts, inc),
range(inc - ((parts - start) % inc),
start, inc)):
if hit_all_devs:
# We've used every device we have, so let's stop looking for
# unused devices now.

View File

@ -64,6 +64,7 @@ import netifaces
import codecs
utf8_decoder = codecs.getdecoder('utf-8')
utf8_encoder = codecs.getencoder('utf-8')
from six.moves import range
from swift import gettext_ as _
import swift.common.exceptions
@ -2953,7 +2954,7 @@ class ThreadPool(object):
_raw_rpipe, self.wpipe = os.pipe()
self.rpipe = greenio.GreenPipe(_raw_rpipe, 'rb', bufsize=0)
for _junk in xrange(nthreads):
for _junk in range(nthreads):
thr = stdlib_threading.Thread(
target=self._worker,
args=(self._run_queue, self._result_queue))

View File

@ -21,6 +21,7 @@ from uuid import uuid4
import time
import cPickle as pickle
from six.moves import range
import sqlite3
from swift.common.utils import Timestamp
@ -698,7 +699,7 @@ class ContainerBroker(DatabaseBroker):
# Get created_at times for objects in item_list that already exist.
# We must chunk it up to avoid sqlite's limit of 999 args.
created_at = {}
for offset in xrange(0, len(item_list), SQLITE_ARG_LIMIT):
for offset in range(0, len(item_list), SQLITE_ARG_LIMIT):
chunk = [rec['name'] for rec in
item_list[offset:offset + SQLITE_ARG_LIMIT]]
created_at.update(

View File

@ -68,7 +68,7 @@ class ResponseError(Exception):
def listing_empty(method):
for i in xrange(6):
for i in range(6):
if len(method()) == 0:
return True

View File

@ -21,6 +21,7 @@ from uuid import uuid4
from nose import SkipTest
from string import letters
from six.moves import range
from swift.common.middleware.acl import format_acl
from test.functional import check_response, retry, requires_acls, \
@ -790,13 +791,13 @@ class TestAccount(unittest.TestCase):
resp = retry(post, headers)
headers = {}
for x in xrange(self.max_meta_count):
for x in range(self.max_meta_count):
headers['X-Account-Meta-%d' % x] = 'v'
resp = retry(post, headers)
resp.read()
self.assertEqual(resp.status, 204)
headers = {}
for x in xrange(self.max_meta_count + 1):
for x in range(self.max_meta_count + 1):
headers['X-Account-Meta-%d' % x] = 'v'
resp = retry(post, headers)
resp.read()

View File

@ -24,6 +24,8 @@ from test.functional import check_response, retry, requires_acls, \
load_constraint, requires_policies
import test.functional as tf
from six.moves import range
class TestContainer(unittest.TestCase):
@ -319,7 +321,7 @@ class TestContainer(unittest.TestCase):
name = uuid4().hex
headers = {}
for x in xrange(self.max_meta_count):
for x in range(self.max_meta_count):
headers['X-Container-Meta-%d' % x] = 'v'
resp = retry(put, name, headers)
resp.read()
@ -329,7 +331,7 @@ class TestContainer(unittest.TestCase):
self.assertEqual(resp.status, 204)
name = uuid4().hex
headers = {}
for x in xrange(self.max_meta_count + 1):
for x in range(self.max_meta_count + 1):
headers['X-Container-Meta-%d' % x] = 'v'
resp = retry(put, name, headers)
resp.read()
@ -412,13 +414,13 @@ class TestContainer(unittest.TestCase):
return check_response(conn)
headers = {}
for x in xrange(self.max_meta_count):
for x in range(self.max_meta_count):
headers['X-Container-Meta-%d' % x] = 'v'
resp = retry(post, headers)
resp.read()
self.assertEqual(resp.status, 204)
headers = {}
for x in xrange(self.max_meta_count + 1):
for x in range(self.max_meta_count + 1):
headers['X-Container-Meta-%d' % x] = 'v'
resp = retry(post, headers)
resp.read()

View File

@ -20,6 +20,7 @@ import unittest
from nose import SkipTest
from uuid import uuid4
from six.moves import range
from test.functional import check_response, retry, requires_acls, \
requires_policies
@ -746,7 +747,7 @@ class TestObject(unittest.TestCase):
parsed.path, self.container, str(objnum)), segments1[objnum],
{'X-Auth-Token': token})
return check_response(conn)
for objnum in xrange(len(segments1)):
for objnum in range(len(segments1)):
resp = retry(put, objnum)
resp.read()
self.assertEqual(resp.status, 201)
@ -809,7 +810,7 @@ class TestObject(unittest.TestCase):
parsed.path, self.container, str(objnum)), segments2[objnum],
{'X-Auth-Token': token})
return check_response(conn)
for objnum in xrange(len(segments2)):
for objnum in range(len(segments2)):
resp = retry(put, objnum)
resp.read()
self.assertEqual(resp.status, 201)
@ -891,7 +892,7 @@ class TestObject(unittest.TestCase):
parsed.path, acontainer, str(objnum)), segments3[objnum],
{'X-Auth-Token': token})
return check_response(conn)
for objnum in xrange(len(segments3)):
for objnum in range(len(segments3)):
resp = retry(put, objnum)
resp.read()
self.assertEqual(resp.status, 201)
@ -966,7 +967,7 @@ class TestObject(unittest.TestCase):
parsed.path, acontainer, str(objnum)), '',
{'X-Auth-Token': token})
return check_response(conn)
for objnum in xrange(len(segments3)):
for objnum in range(len(segments3)):
resp = retry(delete, objnum)
resp.read()
self.assertEqual(resp.status, 204)
@ -977,7 +978,7 @@ class TestObject(unittest.TestCase):
parsed.path, self.container, str(objnum)), '',
{'X-Auth-Token': token})
return check_response(conn)
for objnum in xrange(len(segments2)):
for objnum in range(len(segments2)):
resp = retry(delete, objnum)
resp.read()
self.assertEqual(resp.status, 204)
@ -988,7 +989,7 @@ class TestObject(unittest.TestCase):
parsed.path, self.container, str(objnum)), '',
{'X-Auth-Token': token})
return check_response(conn)
for objnum in xrange(len(segments1)):
for objnum in range(len(segments1)):
resp = retry(delete, objnum)
resp.read()
self.assertEqual(resp.status, 204)

View File

@ -55,7 +55,7 @@ class Utils(object):
u'\u3705\u1803\u0902\uF112\uD210\uB30E\u940C\u850B'\
u'\u5608\u3706\u1804\u0903\u03A9\u2603'
return ''.join([random.choice(utf8_chars)
for x in xrange(length)]).encode('utf-8')
for x in range(length)]).encode('utf-8')
create_name = create_ascii_name
@ -393,14 +393,14 @@ class TestContainer(Base):
cont = self.env.account.container(Utils.create_name())
self.assert_(cont.create())
files = sorted([Utils.create_name() for x in xrange(10)])
files = sorted([Utils.create_name() for x in range(10)])
for f in files:
file_item = cont.file(f)
self.assert_(file_item.write_random())
for i in xrange(len(files)):
for i in range(len(files)):
f = files[i]
for j in xrange(1, len(files) - i):
for j in range(1, len(files) - i):
self.assert_(cont.files(parms={'limit': j, 'marker': f}) ==
files[i + 1: i + j + 1])
self.assert_(cont.files(parms={'marker': f}) == files[i + 1:])

View File

@ -36,7 +36,7 @@ def get_data_file_path(obj_dir):
files = []
# We might need to try a few times if a request hasn't yet settled. For
# instance, a PUT can return success when just 2 of 3 nodes has completed.
for attempt in xrange(RETRIES + 1):
for attempt in range(RETRIES + 1):
try:
files = sorted(listdir(obj_dir), reverse=True)
break

View File

@ -19,6 +19,7 @@ import os
import copy
import logging
import errno
from six.moves import range
import sys
from contextlib import contextmanager, closing
from collections import defaultdict, Iterable
@ -227,8 +228,8 @@ class FakeRing(Ring):
def get_more_nodes(self, part):
# replicas^2 is the true cap
for x in xrange(self.replicas, min(self.replicas + self.max_more_nodes,
self.replicas * self.replicas)):
for x in range(self.replicas, min(self.replicas + self.max_more_nodes,
self.replicas * self.replicas)):
yield {'ip': '10.0.0.%s' % x,
'replication_ip': '10.0.0.%s' % x,
'port': self._base_port + x,

View File

@ -385,17 +385,17 @@ class TestAccountBroker(unittest.TestCase):
# Test AccountBroker.list_containers_iter
broker = AccountBroker(':memory:', account='a')
broker.initialize(Timestamp('1').internal)
for cont1 in xrange(4):
for cont2 in xrange(125):
for cont1 in range(4):
for cont2 in range(125):
broker.put_container('%d-%04d' % (cont1, cont2),
Timestamp(time()).internal, 0, 0, 0,
POLICIES.default.idx)
for cont in xrange(125):
for cont in range(125):
broker.put_container('2-0051-%04d' % cont,
Timestamp(time()).internal, 0, 0, 0,
POLICIES.default.idx)
for cont in xrange(125):
for cont in range(125):
broker.put_container('3-%04d-0049' % cont,
Timestamp(time()).internal, 0, 0, 0,
POLICIES.default.idx)

View File

@ -947,7 +947,7 @@ class TestAccountController(unittest.TestCase):
req = Request.blank('/sda1/p/a', environ={'REQUEST_METHOD': 'PUT',
'HTTP_X_TIMESTAMP': '0'})
req.get_response(self.controller)
for c in xrange(5):
for c in range(5):
req = Request.blank(
'/sda1/p/a/c%d' % c,
environ={'REQUEST_METHOD': 'PUT'},
@ -972,7 +972,7 @@ class TestAccountController(unittest.TestCase):
req = Request.blank('/sda1/p/a', environ={'REQUEST_METHOD': 'PUT',
'HTTP_X_TIMESTAMP': '0'})
req.get_response(self.controller)
for c in xrange(5):
for c in range(5):
req = Request.blank(
'/sda1/p/a/c%d' % c,
environ={'REQUEST_METHOD': 'PUT'},
@ -1002,7 +1002,7 @@ class TestAccountController(unittest.TestCase):
req = Request.blank('/sda1/p/a', environ={'REQUEST_METHOD': 'PUT',
'HTTP_X_TIMESTAMP': '0'})
req.get_response(self.controller)
for c in xrange(5):
for c in range(5):
req = Request.blank(
'/sda1/p/a/c%d' % c,
environ={'REQUEST_METHOD': 'PUT'},

View File

@ -713,7 +713,7 @@ class TestDelete(unittest.TestCase):
with patch.object(self.bulk, 'max_deletes_per_request', 9):
with patch.object(self.bulk, 'max_path_length', 1):
req_body = '\n'.join([str(i) for i in xrange(10)])
req_body = '\n'.join([str(i) for i in range(10)])
req = Request.blank('/delete_works/AUTH_Acc', body=req_body)
self.assertRaises(
HTTPException, self.bulk.get_objs_to_delete, req)

View File

@ -14,6 +14,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from six.moves import range
import hashlib
import time
import unittest
@ -1374,7 +1376,7 @@ class TestSloGetManifest(SloTestCase):
def test_recursion_limit(self):
# man1 points to obj1 and man2, man2 points to obj2 and man3...
for i in xrange(20):
for i in range(20):
self.app.register('GET', '/v1/AUTH_test/gettest/obj%d' % i,
swob.HTTPOk, {'Content-Type': 'text/plain',
'Etag': md5hex('body%02d' % i)},
@ -1391,7 +1393,7 @@ class TestSloGetManifest(SloTestCase):
'Etag': 'man%d' % i},
manifest_json)
for i in xrange(19, 0, -1):
for i in range(19, 0, -1):
manifest_data = [
{'name': '/gettest/obj%d' % i,
'hash': md5hex('body%02d' % i),
@ -1429,7 +1431,7 @@ class TestSloGetManifest(SloTestCase):
def test_sub_slo_recursion(self):
# man1 points to man2 and obj1, man2 points to man3 and obj2...
for i in xrange(11):
for i in range(11):
self.app.register('GET', '/v1/AUTH_test/gettest/obj%d' % i,
swob.HTTPOk, {'Content-Type': 'text/plain',
'Content-Length': '6',
@ -1452,7 +1454,7 @@ class TestSloGetManifest(SloTestCase):
'Etag': md5hex('body%2d' % i)},
None)
for i in xrange(9, 0, -1):
for i in range(9, 0, -1):
manifest_data = [
{'name': '/gettest/man%d' % (i + 1),
'hash': 'man%d' % (i + 1),
@ -1486,7 +1488,7 @@ class TestSloGetManifest(SloTestCase):
def test_sub_slo_recursion_limit(self):
# man1 points to man2 and obj1, man2 points to man3 and obj2...
for i in xrange(12):
for i in range(12):
self.app.register('GET', '/v1/AUTH_test/gettest/obj%d' % i,
swob.HTTPOk,
{'Content-Type': 'text/plain',
@ -1509,7 +1511,7 @@ class TestSloGetManifest(SloTestCase):
'Etag': md5hex('body%2d' % i)},
None)
for i in xrange(11, 0, -1):
for i in range(11, 0, -1):
manifest_data = [
{'name': '/gettest/man%d' % (i + 1),
'hash': 'man%d' % (i + 1),

View File

@ -26,6 +26,8 @@ from math import ceil
from tempfile import mkdtemp
from shutil import rmtree
from six.moves import range
from swift.common import exceptions
from swift.common import ring
from swift.common.ring.builder import MAX_BALANCE
@ -373,9 +375,9 @@ class TestRingBuilder(unittest.TestCase):
rb.rebalance()
rb.validate()
for part in xrange(rb.parts):
for part in range(rb.parts):
counts = defaultdict(lambda: defaultdict(int))
for replica in xrange(rb.replicas):
for replica in range(rb.replicas):
dev = rb.devs[rb._replica2part2dev[replica][part]]
counts['region'][dev['region']] += 1
counts['zone'][dev['zone']] += 1
@ -415,9 +417,9 @@ class TestRingBuilder(unittest.TestCase):
rb.rebalance()
rb.validate()
for part in xrange(rb.parts):
for part in range(rb.parts):
counts = defaultdict(lambda: defaultdict(int))
for replica in xrange(rb.replicas):
for replica in range(rb.replicas):
dev = rb.devs[rb._replica2part2dev[replica][part]]
counts['zone'][dev['zone']] += 1
counts['dev_id'][dev['id']] += 1
@ -452,9 +454,9 @@ class TestRingBuilder(unittest.TestCase):
rb.rebalance()
rb.validate()
for part in xrange(rb.parts):
for part in range(rb.parts):
counts = defaultdict(lambda: defaultdict(int))
for replica in xrange(rb.replicas):
for replica in range(rb.replicas):
dev = rb.devs[rb._replica2part2dev[replica][part]]
counts['zone'][dev['zone']] += 1
counts['dev_id'][dev['id']] += 1
@ -489,9 +491,9 @@ class TestRingBuilder(unittest.TestCase):
rb.rebalance()
rb.validate()
for part in xrange(rb.parts):
for part in range(rb.parts):
counts = defaultdict(lambda: defaultdict(int))
for replica in xrange(rb.replicas):
for replica in range(rb.replicas):
dev = rb.devs[rb._replica2part2dev[replica][part]]
counts['zone'][dev['zone']] += 1
counts['dev_id'][dev['id']] += 1
@ -528,15 +530,15 @@ class TestRingBuilder(unittest.TestCase):
rb.add_dev({'id': 5, 'region': 0, 'zone': 2, 'weight': 1,
'ip': '127.0.0.1', 'port': 10000, 'device': 'sdf'})
for _ in xrange(5):
for _ in range(5):
rb.pretend_min_part_hours_passed()
rb.rebalance()
rb.validate()
for part in xrange(rb.parts):
for part in range(rb.parts):
counts = dict(zone=defaultdict(int),
dev_id=defaultdict(int))
for replica in xrange(rb.replicas):
for replica in range(rb.replicas):
dev = rb.devs[rb._replica2part2dev[replica][part]]
counts['zone'][dev['zone']] += 1
counts['dev_id'][dev['id']] += 1
@ -561,9 +563,9 @@ class TestRingBuilder(unittest.TestCase):
rb.rebalance()
rb.validate()
for part in xrange(rb.parts):
for part in range(rb.parts):
devs = set()
for replica in xrange(rb.replicas):
for replica in range(rb.replicas):
devs.add(rb._replica2part2dev[replica][part])
if len(devs) != 3:
@ -587,9 +589,9 @@ class TestRingBuilder(unittest.TestCase):
rb.rebalance()
rb.validate()
for part in xrange(rb.parts):
for part in range(rb.parts):
devs = set()
for replica in xrange(rb.replicas):
for replica in range(rb.replicas):
devs.add(rb._replica2part2dev[replica][part])
if len(devs) != 2:
@ -616,9 +618,9 @@ class TestRingBuilder(unittest.TestCase):
rb.rebalance()
rb.validate()
for part in xrange(rb.parts):
for part in range(rb.parts):
zones = set()
for replica in xrange(rb.replicas):
for replica in range(rb.replicas):
zones.add(rb.devs[rb._replica2part2dev[replica][part]]['zone'])
if len(zones) != 3:

View File

@ -25,6 +25,8 @@ from tempfile import mkdtemp
from shutil import rmtree
from time import sleep, time
from six.moves import range
from swift.common import ring, utils
@ -74,7 +76,7 @@ class TestRingData(unittest.TestCase):
{'id': 1, 'zone': 1, 'ip': '10.1.1.1', 'port': 7000}],
30)
ring_fname = os.path.join(self.testdir, 'foo.ring.gz')
for p in xrange(pickle.HIGHEST_PROTOCOL):
for p in range(pickle.HIGHEST_PROTOCOL):
with closing(GzipFile(ring_fname, 'wb')) as f:
pickle.dump(rd, f, protocol=p)
meta_only = ring.RingData.load(ring_fname, metadata_only=True)
@ -490,9 +492,9 @@ class TestRing(TestRingBase):
19, 55]
rb = ring.RingBuilder(8, 3, 1)
next_dev_id = 0
for zone in xrange(1, 10):
for server in xrange(1, 5):
for device in xrange(1, 4):
for zone in range(1, 10):
for server in range(1, 5):
for device in range(1, 4):
rb.add_dev({'id': next_dev_id,
'ip': '1.2.%d.%d' % (zone, server),
'port': 1234 + device,
@ -518,7 +520,7 @@ class TestRing(TestRingBase):
# The first handoff nodes for each partition in the ring
devs = []
for part in xrange(r.partition_count):
for part in range(r.partition_count):
devs.append(next(r.get_more_nodes(part))['id'])
self.assertEquals(devs, exp_first_handoffs)
@ -554,9 +556,9 @@ class TestRing(TestRingBase):
self.assertEquals(seen_zones, set(range(1, 10)))
devs = []
for part in xrange(r.partition_count):
for part in range(r.partition_count):
devs.append(next(r.get_more_nodes(part))['id'])
for part in xrange(r.partition_count):
for part in range(r.partition_count):
self.assertEquals(
devs[part], exp_first_handoffs[part],
'handoff for partitition %d is now device id %d' % (
@ -603,9 +605,9 @@ class TestRing(TestRingBase):
self.assertEquals(seen_zones, set(range(1, 10)))
devs = []
for part in xrange(r.partition_count):
for part in range(r.partition_count):
devs.append(next(r.get_more_nodes(part))['id'])
for part in xrange(r.partition_count):
for part in range(r.partition_count):
self.assertEquals(
devs[part], exp_first_handoffs[part],
'handoff for partitition %d is now device id %d' % (
@ -684,9 +686,9 @@ class TestRing(TestRingBase):
self.assertEquals(seen_zones, set(range(1, 10)))
devs = []
for part in xrange(r.partition_count):
for part in range(r.partition_count):
devs.append(next(r.get_more_nodes(part))['id'])
for part in xrange(r.partition_count):
for part in range(r.partition_count):
self.assertEquals(
devs[part], exp_first_handoffs[part],
'handoff for partitition %d is now device id %d' % (
@ -727,7 +729,7 @@ class TestRing(TestRingBase):
# Test distribution across regions
rb.set_replicas(3)
for region in xrange(1, 5):
for region in range(1, 5):
rb.add_dev({'id': next_dev_id,
'ip': '1.%d.1.%d' % (region, server), 'port': 1234,
# 108.0 is the weight of all devices created prior to

View File

@ -18,6 +18,7 @@ import mock
import tempfile
import time
from six.moves import range
from test import safe_repr
from test.unit import MockTrue
@ -87,7 +88,7 @@ class TestConstraints(unittest.TestCase):
def test_check_metadata_count(self):
headers = {}
for x in xrange(constraints.MAX_META_COUNT):
for x in range(constraints.MAX_META_COUNT):
headers['X-Object-Meta-%d' % x] = 'v'
self.assertEquals(constraints.check_metadata(Request.blank(
'/', headers=headers), 'object'), None)

View File

@ -31,6 +31,7 @@ import random
from mock import patch, MagicMock
from eventlet.timeout import Timeout
from six.moves import range
import swift.common.db
from swift.common.constraints import \
@ -1136,7 +1137,7 @@ class TestDatabaseBroker(unittest.TestCase):
def test_metadata_with_max_count(self):
metadata = {}
for c in xrange(MAX_META_COUNT):
for c in range(MAX_META_COUNT):
key = 'X-Account-Meta-F{0}'.format(c)
metadata[key] = ('B', normalize_timestamp(1))
key = 'X-Account-Meta-Foo'.format(c)
@ -1148,7 +1149,7 @@ class TestDatabaseBroker(unittest.TestCase):
def test_metadata_raises_exception_over_max_count(self):
metadata = {}
for c in xrange(MAX_META_COUNT + 1):
for c in range(MAX_META_COUNT + 1):
key = 'X-Account-Meta-F{0}'.format(c)
metadata[key] = ('B', normalize_timestamp(1))
message = ''

View File

@ -22,6 +22,7 @@ import zlib
from textwrap import dedent
import os
from six.moves import range
from test.unit import FakeLogger
import eventlet
from eventlet.green import urllib2
@ -38,7 +39,7 @@ def not_sleep(seconds):
def unicode_string(start, length):
return u''.join([unichr(x) for x in xrange(start, start + length)])
return u''.join([unichr(x) for x in range(start, start + length)])
def path_parts():
@ -575,7 +576,7 @@ class TestInternalClient(unittest.TestCase):
exp_items = []
responses = []
for i in xrange(3):
for i in range(3):
data = [
{'name': 'item%02d' % (2 * i)},
{'name': 'item%02d' % (2 * i + 1)}]

View File

@ -1234,12 +1234,12 @@ class TestResponse(unittest.TestCase):
def app_iter_ranges(self, ranges, content_type, boundary, size):
app_iter_ranges_args.append((ranges, content_type, boundary,
size))
for i in xrange(3):
for i in range(3):
yield str(i) + 'fun'
yield boundary
def __iter__(self):
for i in xrange(3):
for i in range(3):
yield str(i) + 'fun'
req = swift.common.swob.Request.blank(

View File

@ -28,6 +28,7 @@ import os
import mock
import random
import re
from six.moves import range
import socket
import stat
import sys
@ -3741,7 +3742,7 @@ class TestRateLimitedIterator(unittest.TestCase):
def test_rate_limiting(self):
def testfunc():
limited_iterator = utils.RateLimitedIterator(xrange(9999), 100)
limited_iterator = utils.RateLimitedIterator(range(9999), 100)
got = []
started_at = time.time()
try:
@ -3760,7 +3761,7 @@ class TestRateLimitedIterator(unittest.TestCase):
def testfunc():
limited_iterator = utils.RateLimitedIterator(
xrange(9999), 100, limit_after=5)
range(9999), 100, limit_after=5)
got = []
started_at = time.time()
try:
@ -3790,7 +3791,7 @@ class TestGreenthreadSafeIterator(unittest.TestCase):
iterable = UnsafeXrange(10)
pile = eventlet.GreenPile(2)
for _ in xrange(2):
for _ in range(2):
pile.spawn(self.increment, iterable)
sorted([resp for resp in pile])
@ -3801,10 +3802,10 @@ class TestGreenthreadSafeIterator(unittest.TestCase):
pile = eventlet.GreenPile(2)
unsafe_iterable = UnsafeXrange(10)
iterable = utils.GreenthreadSafeIterator(unsafe_iterable)
for _ in xrange(2):
for _ in range(2):
pile.spawn(self.increment, iterable)
response = sorted(sum([resp for resp in pile], []))
self.assertEquals(range(1, 11), response)
self.assertEquals(list(range(1, 11)), response)
self.assertTrue(
not unsafe_iterable.concurrent_call, 'concurrent call occurred')
@ -4472,7 +4473,7 @@ class TestGreenAsyncPile(unittest.TestCase):
return tests_ran[0]
tests_ran = [0]
pile = utils.GreenAsyncPile(3)
for x in xrange(3):
for x in range(3):
pile.spawn(run_test)
self.assertEqual(sorted(x for x in pile), [1, 2, 3])
@ -4485,7 +4486,7 @@ class TestGreenAsyncPile(unittest.TestCase):
for order in ((1, 2, 0), (0, 1, 2), (2, 1, 0), (0, 2, 1)):
events = [eventlet.event.Event(), eventlet.event.Event(),
eventlet.event.Event()]
for x in xrange(3):
for x in range(3):
pile.spawn(run_test, x)
for x in order:
events[x].send()

View File

@ -1317,7 +1317,7 @@ class TestPipelineModification(unittest.TestCase):
# This is rather brittle; it'll break if a middleware stores its app
# anywhere other than an attribute named "app", but it works for now.
pipe = []
for _ in xrange(1000):
for _ in range(1000):
pipe.append(app.__class__.__module__)
if not hasattr(app, 'app'):
break

View File

@ -738,17 +738,17 @@ class TestContainerBroker(unittest.TestCase):
# Test ContainerBroker.list_objects_iter
broker = ContainerBroker(':memory:', account='a', container='c')
broker.initialize(Timestamp('1').internal, 0)
for obj1 in xrange(4):
for obj2 in xrange(125):
for obj1 in range(4):
for obj2 in range(125):
broker.put_object('%d/%04d' % (obj1, obj2),
Timestamp(time()).internal, 0, 'text/plain',
'd41d8cd98f00b204e9800998ecf8427e')
for obj in xrange(125):
for obj in range(125):
broker.put_object('2/0051/%04d' % obj,
Timestamp(time()).internal, 0, 'text/plain',
'd41d8cd98f00b204e9800998ecf8427e')
for obj in xrange(125):
for obj in range(125):
broker.put_object('3/%04d/0049' % obj,
Timestamp(time()).internal, 0, 'text/plain',
'd41d8cd98f00b204e9800998ecf8427e')
@ -857,17 +857,17 @@ class TestContainerBroker(unittest.TestCase):
# delimiter that is not a slash
broker = ContainerBroker(':memory:', account='a', container='c')
broker.initialize(Timestamp('1').internal, 0)
for obj1 in xrange(4):
for obj2 in xrange(125):
for obj1 in range(4):
for obj2 in range(125):
broker.put_object('%d:%04d' % (obj1, obj2),
Timestamp(time()).internal, 0, 'text/plain',
'd41d8cd98f00b204e9800998ecf8427e')
for obj in xrange(125):
for obj in range(125):
broker.put_object('2:0051:%04d' % obj,
Timestamp(time()).internal, 0, 'text/plain',
'd41d8cd98f00b204e9800998ecf8427e')
for obj in xrange(125):
for obj in range(125):
broker.put_object('3:%04d:0049' % obj,
Timestamp(time()).internal, 0, 'text/plain',
'd41d8cd98f00b204e9800998ecf8427e')

View File

@ -37,7 +37,7 @@ class FakeRing(object):
def __init__(self):
self.devs = [{'ip': '10.0.0.%s' % x, 'port': 1000 + x, 'device': 'sda'}
for x in xrange(3)]
for x in range(3)]
def get_nodes(self, account, container=None, obj=None):
return 1, list(self.devs)

View File

@ -139,7 +139,7 @@ class TestContainerUpdater(unittest.TestCase):
def spawn_accepts():
events = []
for _junk in xrange(2):
for _junk in range(2):
sock, addr = bindsock.accept()
events.append(spawn(accept, sock, addr, 201))
return events
@ -234,7 +234,7 @@ class TestContainerUpdater(unittest.TestCase):
def spawn_accepts():
events = []
for _junk in xrange(2):
for _junk in range(2):
with Timeout(3):
sock, addr = bindsock.accept()
events.append(spawn(accept, sock, addr))

View File

@ -172,7 +172,7 @@ class TestObjectExpirer(TestCase):
x.swift = InternalClient(containers)
deleted_objects = {}
for i in xrange(3):
for i in range(3):
x.process = i
x.run_once()
self.assertNotEqual(deleted_objects, x.deleted_objects)

View File

@ -28,6 +28,7 @@ from time import time
from distutils.dir_util import mkpath
from eventlet import spawn, Timeout, listen
from six.moves import range
from swift.obj import updater as object_updater
from swift.obj.diskfile import (ASYNCDIR_BASE, get_async_dir, DiskFileManager,
@ -332,7 +333,7 @@ class TestObjectUpdater(unittest.TestCase):
codes = iter(return_codes)
try:
events = []
for x in xrange(len(return_codes)):
for x in range(len(return_codes)):
with Timeout(3):
sock, addr = bindsock.accept()
events.append(

View File

@ -26,6 +26,7 @@ from hashlib import md5
import mock
from eventlet import Timeout
from six.moves import range
import swift
from swift.common import utils, swob
@ -1442,7 +1443,7 @@ class TestECObjController(BaseObjectControllerMixin, unittest.TestCase):
# swap out some with regular fast responses
number_of_fast_responses_needed_to_be_quick_enough = 2
fast_indexes = random.sample(
xrange(self.replicas()),
range(self.replicas()),
number_of_fast_responses_needed_to_be_quick_enough)
for i in fast_indexes:
codes[i] = 201

View File

@ -42,6 +42,7 @@ import random
import mock
from eventlet import sleep, spawn, wsgi, listen, Timeout
from six.moves import range
from swift.common.utils import hash_path, json, storage_directory, \
parse_content_type, iter_multipart_mime_documents, public
@ -3416,7 +3417,7 @@ class TestObjectController(unittest.TestCase):
with save_globals():
limit = constraints.MAX_META_COUNT
headers = dict(
(('X-Object-Meta-' + str(i), 'a') for i in xrange(limit + 1)))
(('X-Object-Meta-' + str(i), 'a') for i in range(limit + 1)))
headers.update({'Content-Type': 'foo/bar'})
set_http_connect(202, 202, 202)
req = Request.blank('/v1/a/c/o', {'REQUEST_METHOD': 'POST'},
@ -3431,7 +3432,7 @@ class TestObjectController(unittest.TestCase):
count = limit / 256 # enough to cause the limit to be reached
headers = dict(
(('X-Object-Meta-' + str(i), 'a' * 256)
for i in xrange(count + 1)))
for i in range(count + 1)))
headers.update({'Content-Type': 'foo/bar'})
set_http_connect(202, 202, 202)
req = Request.blank('/v1/a/c/o', {'REQUEST_METHOD': 'POST'},
@ -3860,7 +3861,7 @@ class TestObjectController(unittest.TestCase):
def test_iter_nodes_with_custom_node_iter(self):
object_ring = self.app.get_object_ring(None)
node_list = [dict(id=n, ip='1.2.3.4', port=n, device='D')
for n in xrange(10)]
for n in range(10)]
with nested(
mock.patch.object(self.app, 'sort_nodes', lambda n: n),
mock.patch.object(self.app, 'request_node_count',
@ -3945,7 +3946,7 @@ class TestObjectController(unittest.TestCase):
node_error_count(controller.app, object_ring.devs[0]), 2)
self.assert_(node_last_error(controller.app, object_ring.devs[0])
is not None)
for _junk in xrange(self.app.error_suppression_limit):
for _junk in range(self.app.error_suppression_limit):
self.assert_status_map(controller.HEAD, (200, 200, 503, 503,
503), 503)
self.assertEquals(
@ -3982,7 +3983,7 @@ class TestObjectController(unittest.TestCase):
node_error_count(controller.app, object_ring.devs[0]), 2)
self.assert_(node_last_error(controller.app, object_ring.devs[0])
is not None)
for _junk in xrange(self.app.error_suppression_limit):
for _junk in range(self.app.error_suppression_limit):
self.assert_status_map(controller.HEAD, (200, 200, 503, 503,
503), 503)
self.assertEquals(
@ -4226,7 +4227,7 @@ class TestObjectController(unittest.TestCase):
set_http_connect(201, 201, 201)
headers = {'Content-Length': '0'}
for x in xrange(constraints.MAX_META_COUNT):
for x in range(constraints.MAX_META_COUNT):
headers['X-Object-Meta-%d' % x] = 'v'
req = Request.blank('/v1/a/c/o', environ={'REQUEST_METHOD': 'PUT'},
headers=headers)
@ -4235,7 +4236,7 @@ class TestObjectController(unittest.TestCase):
self.assertEquals(resp.status_int, 201)
set_http_connect(201, 201, 201)
headers = {'Content-Length': '0'}
for x in xrange(constraints.MAX_META_COUNT + 1):
for x in range(constraints.MAX_META_COUNT + 1):
headers['X-Object-Meta-%d' % x] = 'v'
req = Request.blank('/v1/a/c/o', environ={'REQUEST_METHOD': 'PUT'},
headers=headers)
@ -5359,7 +5360,7 @@ class TestObjectController(unittest.TestCase):
exp = 'HTTP/1.1 201'
self.assertEquals(headers[:len(exp)], exp)
# Create the object versions
for segment in xrange(1, versions_to_create):
for segment in range(1, versions_to_create):
sleep(.01) # guarantee that the timestamp changes
sock = connect_tcp(('localhost', prolis.getsockname()[1]))
fd = sock.makefile()
@ -5445,7 +5446,7 @@ class TestObjectController(unittest.TestCase):
body = fd.read()
self.assertEquals(body, '%05d' % segment)
# Delete the object versions
for segment in xrange(versions_to_create - 1, 0, -1):
for segment in range(versions_to_create - 1, 0, -1):
sock = connect_tcp(('localhost', prolis.getsockname()[1]))
fd = sock.makefile()
fd.write('DELETE /v1/a/%s/%s HTTP/1.1\r\nHost: localhost\r'
@ -5514,7 +5515,7 @@ class TestObjectController(unittest.TestCase):
self.assertEquals(headers[:len(exp)], exp)
# make sure dlo manifest files don't get versioned
for _junk in xrange(1, versions_to_create):
for _junk in range(1, versions_to_create):
sleep(.01) # guarantee that the timestamp changes
sock = connect_tcp(('localhost', prolis.getsockname()[1]))
fd = sock.makefile()
@ -6071,7 +6072,7 @@ class TestObjectController(unittest.TestCase):
self.assertEqual(headers[:len(exp)], exp)
# Remember Request instance count, make sure the GC is run for
# pythons without reference counting.
for i in xrange(4):
for i in range(4):
sleep(0) # let eventlet do its thing
gc.collect()
else:
@ -6094,7 +6095,7 @@ class TestObjectController(unittest.TestCase):
sock.close()
# Make sure the GC is run again for pythons without reference
# counting
for i in xrange(4):
for i in range(4):
sleep(0) # let eventlet do its thing
gc.collect()
else:
@ -7593,7 +7594,7 @@ class TestContainerController(unittest.TestCase):
self.assert_(
node_last_error(controller.app, container_ring.devs[0])
is not None)
for _junk in xrange(self.app.error_suppression_limit):
for _junk in range(self.app.error_suppression_limit):
self.assert_status_map(controller.HEAD,
(200, 503, 503, 503), 503)
self.assertEquals(
@ -7749,7 +7750,7 @@ class TestContainerController(unittest.TestCase):
set_http_connect(201, 201, 201)
headers = {}
for x in xrange(constraints.MAX_META_COUNT):
for x in range(constraints.MAX_META_COUNT):
headers['X-Container-Meta-%d' % x] = 'v'
req = Request.blank('/v1/a/c', environ={'REQUEST_METHOD': method},
headers=headers)
@ -7758,7 +7759,7 @@ class TestContainerController(unittest.TestCase):
self.assertEquals(resp.status_int, 201)
set_http_connect(201, 201, 201)
headers = {}
for x in xrange(constraints.MAX_META_COUNT + 1):
for x in range(constraints.MAX_META_COUNT + 1):
headers['X-Container-Meta-%d' % x] = 'v'
req = Request.blank('/v1/a/c', environ={'REQUEST_METHOD': method},
headers=headers)
@ -8704,7 +8705,7 @@ class TestAccountController(unittest.TestCase):
set_http_connect(201, 201, 201)
headers = {}
for x in xrange(constraints.MAX_META_COUNT):
for x in range(constraints.MAX_META_COUNT):
headers['X-Account-Meta-%d' % x] = 'v'
req = Request.blank('/v1/a/c', environ={'REQUEST_METHOD': method},
headers=headers)
@ -8713,7 +8714,7 @@ class TestAccountController(unittest.TestCase):
self.assertEquals(resp.status_int, 201)
set_http_connect(201, 201, 201)
headers = {}
for x in xrange(constraints.MAX_META_COUNT + 1):
for x in range(constraints.MAX_META_COUNT + 1):
headers['X-Account-Meta-%d' % x] = 'v'
req = Request.blank('/v1/a/c', environ={'REQUEST_METHOD': method},
headers=headers)