Skip to content

Commit 204ef5d

Browse files
committed
Replace six.moves.xrange with six.moves.range
PiperOrigin-RevId: 204824655
1 parent ce449b4 commit 204ef5d

21 files changed

Lines changed: 72 additions & 71 deletions

dm_control/mujoco/engine_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
import mock
3838
import numpy as np
3939
from six.moves import cPickle
40-
from six.moves import xrange # pylint: disable=redefined-builtin
40+
from six.moves import range
4141

4242
MODEL_PATH = assets.get_path('cartpole.xml')
4343
MODEL_WITH_ASSETS = assets.get_contents('model_with_assets.xml')
@@ -285,7 +285,7 @@ def testNanControl(self):
285285
('_pickle_and_unpickle', lambda x: cPickle.loads(cPickle.dumps(x))),
286286
)
287287
def testCopyOrPicklePhysics(self, func):
288-
for _ in xrange(10):
288+
for _ in range(10):
289289
self._physics.step()
290290
physics2 = func(self._physics)
291291
self.assertNotEqual(physics2.model.ptr, self._physics.model.ptr)
@@ -296,7 +296,7 @@ def testCopyOrPicklePhysics(self, func):
296296
data_attr_to_compare = ('time', 'energy', 'qpos', 'xpos')
297297
self._assert_attributes_equal(
298298
physics2.data, self._physics.data, data_attr_to_compare)
299-
for _ in xrange(10):
299+
for _ in range(10):
300300
self._physics.step()
301301
physics2.step()
302302
self._assert_attributes_equal(

dm_control/mujoco/render_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
from dm_control import render
3030
from dm_control.mujoco.testing import decorators
3131
from dm_control.mujoco.testing import image_utils
32-
from six.moves import xrange # pylint: disable=redefined-builtin
33-
from six.moves import zip # pylint: disable=redefined-builtin
32+
from six.moves import range
33+
from six.moves import zip
3434

3535

3636
DEBUG_IMAGE_DIR = os.environ.get('TEST_UNDECLARED_OUTPUTS_DIR',
@@ -77,7 +77,7 @@ def test_render_multiple_physics_per_thread(self):
7777
def test_repeatedly_create_and_destroy_rendering_contexts(self):
7878
# Tests for errors that may occur due to per-thread GL resource leakage.
7979
physics = mujoco.Physics.from_xml_string('<mujoco/>')
80-
for _ in xrange(500):
80+
for _ in range(500):
8181
physics._make_rendering_contexts()
8282

8383
if __name__ == '__main__':

dm_control/mujoco/testing/decorators.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
# Internal dependencies.
2727

2828
import six
29-
from six.moves import xrange # pylint: disable=redefined-builtin
29+
from six.moves import range
3030

3131

3232
def run_threaded(num_threads=4, calls_per_thread=10):
@@ -49,14 +49,14 @@ def decorated_method(self, *args, **kwargs):
4949
exceptions = []
5050
def worker():
5151
try:
52-
for _ in xrange(calls_per_thread):
52+
for _ in range(calls_per_thread):
5353
test_method(self, *args, **kwargs)
5454
except: # pylint: disable=bare-except
5555
# Appending to Python list is thread-safe:
5656
# http://effbot.org/pyfaq/what-kinds-of-global-value-mutation-are-thread-safe.htm
5757
exceptions.append(sys.exc_info())
5858
threads = [threading.Thread(target=worker, name='thread_{}'.format(i))
59-
for i in xrange(num_threads)]
59+
for i in range(num_threads)]
6060
for thread in threads:
6161
thread.start()
6262
for thread in threads:

dm_control/mujoco/testing/decorators_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
from dm_control.mujoco.testing import decorators
2727
import mock
28-
from six.moves import xrange # pylint: disable=redefined-builtin
28+
from six.moves import range
2929

3030

3131
class RunThreadedTest(absltest.TestCase):
@@ -34,7 +34,7 @@ class RunThreadedTest(absltest.TestCase):
3434
def test_number_of_threads(self, mock_threading):
3535
num_threads = 5
3636

37-
mock_threads = [mock.MagicMock() for _ in xrange(num_threads)]
37+
mock_threads = [mock.MagicMock() for _ in range(num_threads)]
3838
for thread in mock_threads:
3939
thread.start = mock.MagicMock()
4040
thread.join = mock.MagicMock()

dm_control/mujoco/testing/image_utils.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
import numpy as np
3131
from PIL import Image
3232
import six
33-
from six.moves import xrange # pylint: disable=redefined-builtin
34-
from six.moves import zip # pylint: disable=redefined-builtin
33+
from six.moves import range
34+
from six.moves import zip
3535

3636

3737
class ImagesNotClose(AssertionError):
@@ -86,8 +86,8 @@ def iter_render(self):
8686
random_state = np.random.RandomState(self._seed)
8787
physics = mujoco.Physics.from_xml_string(self._xml_string)
8888
action_spec = mujoco.action_spec(physics)
89-
for _ in xrange(self._num_frames):
90-
for _ in xrange(self._steps_per_frame):
89+
for _ in range(self._num_frames):
90+
for _ in range(self._steps_per_frame):
9191
actions = random_state.uniform(action_spec.minimum, action_spec.maximum)
9292
physics.set_control(actions)
9393
physics.step()
@@ -111,7 +111,7 @@ def save(self):
111111
_save_pixels(pixels, path)
112112

113113
def _iter_paths(self):
114-
for frame_num in xrange(self._num_frames):
114+
for frame_num in range(self._num_frames):
115115
filename = self._FILENAME_TEMPLATE.format(frame_num=frame_num)
116116
for camera_spec in self._camera_specs:
117117
subdir_name = self._SUBDIR_TEMPLATE.format(
@@ -192,6 +192,7 @@ def decorator(test_method):
192192
method_name = test_method.__name__
193193
@functools.wraps(test_method)
194194
def decorated_method(*args, **kwargs):
195+
"""Call test method, save debugging images if ImagesNotClose is raised."""
195196
try:
196197
test_method(*args, **kwargs)
197198
except ImagesNotClose as e:

dm_control/mujoco/testing/memory_checker_test.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from dm_control.mujoco.testing import memory_checker
3030
from dm_control.mujoco.wrapper.mjbindings import mjlib
3131

32-
from six.moves import xrange # pylint: disable=redefined-builtin
32+
from six.moves import range
3333

3434

3535
class MemoryCheckingContextTest(absltest.TestCase):
@@ -70,7 +70,7 @@ def test_allocate_and_free_inside(self):
7070
with memory_checker.assert_mujoco_memory_freed():
7171
allocated = [
7272
mjlib.mju_malloc(self.bytes_per_array)
73-
for _ in xrange(self.n_arrays)
73+
for _ in range(self.n_arrays)
7474
]
7575
for ptr in allocated:
7676
mjlib.mju_free(ptr)
@@ -79,7 +79,7 @@ def test_allocate_outside_free_inside(self):
7979
"""Allocating outside and freeing inside shouldn't raise any exceptions."""
8080
allocated = [
8181
mjlib.mju_malloc(self.bytes_per_array)
82-
for _ in xrange(self.n_arrays)
82+
for _ in range(self.n_arrays)
8383
]
8484
with memory_checker.assert_mujoco_memory_freed():
8585
for ptr in allocated:
@@ -94,7 +94,7 @@ def test_allocate_inside_free_outside(self):
9494
with memory_checker.assert_mujoco_memory_freed():
9595
allocated = [
9696
mjlib.mju_malloc(self.bytes_per_array)
97-
for _ in xrange(self.n_arrays)
97+
for _ in range(self.n_arrays)
9898
]
9999
for ptr in allocated:
100100
mjlib.mju_free(ptr)

dm_control/mujoco/thread_safety_test.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from dm_control.mujoco.testing import assets
3030
from dm_control.mujoco.testing import decorators
3131

32-
from six.moves import xrange # pylint: disable=redefined-builtin
32+
from six.moves import range
3333

3434
MODEL = assets.get_contents('cartpole.xml')
3535
NUM_STEPS = 10
@@ -49,25 +49,25 @@ def test_load_and_reload_physics_from_string(self):
4949
@decorators.run_threaded()
5050
def test_load_and_step_physics(self):
5151
physics = engine.Physics.from_xml_string(MODEL)
52-
for _ in xrange(NUM_STEPS):
52+
for _ in range(NUM_STEPS):
5353
physics.step()
5454

5555
@decorators.run_threaded()
5656
def test_load_and_step_multiple_physics_parallel(self):
5757
physics1 = engine.Physics.from_xml_string(MODEL)
5858
physics2 = engine.Physics.from_xml_string(MODEL)
59-
for _ in xrange(NUM_STEPS):
59+
for _ in range(NUM_STEPS):
6060
physics1.step()
6161
physics2.step()
6262

6363
@decorators.run_threaded()
6464
def test_load_and_step_multiple_physics_sequential(self):
6565
physics1 = engine.Physics.from_xml_string(MODEL)
66-
for _ in xrange(NUM_STEPS):
66+
for _ in range(NUM_STEPS):
6767
physics1.step()
6868
del physics1
6969
physics2 = engine.Physics.from_xml_string(MODEL)
70-
for _ in xrange(NUM_STEPS):
70+
for _ in range(NUM_STEPS):
7171
physics2.step()
7272

7373
@unittest.skipIf(render.DISABLED, render.DISABLED_MESSAGE)
@@ -80,7 +80,7 @@ def test_load_physics_and_render(self):
8080
physics.set_control([1.0])
8181

8282
unique_frames = set()
83-
for _ in xrange(NUM_STEPS):
83+
for _ in range(NUM_STEPS):
8484
physics.step()
8585
frame = physics.render(width=320, height=240, camera_id=0)
8686
unique_frames.add(frame.tostring())
@@ -93,7 +93,7 @@ def test_load_physics_and_render(self):
9393
def test_render_multiple_physics_instances_per_thread_parallel(self):
9494
physics1 = engine.Physics.from_xml_string(MODEL)
9595
physics2 = engine.Physics.from_xml_string(MODEL)
96-
for _ in xrange(NUM_STEPS):
96+
for _ in range(NUM_STEPS):
9797
physics1.step()
9898
physics1.render(width=320, height=240, camera_id=0)
9999
physics2.step()

dm_control/mujoco/wrapper/core_test.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
import mock
3838
import numpy as np
3939
from six.moves import cPickle
40-
from six.moves import xrange # pylint: disable=redefined-builtin
40+
from six.moves import range
4141

4242

4343
HUMANOID_XML_PATH = assets.get_path("humanoid.xml")
@@ -173,8 +173,8 @@ def testStep(self):
173173
t0 = self.data.time
174174
mjlib.mj_step(self.model.ptr, self.data.ptr)
175175
self.assertEqual(self.data.time, t0 + self.model.opt.timestep)
176-
self.assert_(np.all(np.isfinite(self.data.qpos[:])))
177-
self.assert_(np.all(np.isfinite(self.data.qvel[:])))
176+
self.assertTrue(np.all(np.isfinite(self.data.qpos[:])))
177+
self.assertTrue(np.all(np.isfinite(self.data.qvel[:])))
178178

179179
def testMultipleData(self):
180180
data2 = core.MjData(self.model)
@@ -210,13 +210,13 @@ def testCopyOrPickleModel(self, func):
210210
("_copy", lambda x: x.copy()),
211211
("_pickle_unpickle", lambda x: cPickle.loads(cPickle.dumps(x))),)
212212
def testCopyOrPickleData(self, func):
213-
for _ in xrange(10):
213+
for _ in range(10):
214214
mjlib.mj_step(self.model.ptr, self.data.ptr)
215215
data2 = func(self.data)
216216
attr_to_compare = ("time", "energy", "qpos", "xpos")
217217
self.assertNotEqual(data2.ptr, self.data.ptr)
218218
self._assert_attributes_equal(data2, self.data, attr_to_compare)
219-
for _ in xrange(10):
219+
for _ in range(10):
220220
mjlib.mj_step(self.model.ptr, self.data.ptr)
221221
mjlib.mj_step(data2.model.ptr, data2.ptr)
222222
self._assert_attributes_equal(data2, self.data, attr_to_compare)
@@ -225,13 +225,13 @@ def testCopyOrPickleData(self, func):
225225
("_copy", lambda x: x.copy()),
226226
("_pickle_unpickle", lambda x: cPickle.loads(cPickle.dumps(x))),)
227227
def testCopyOrPickleStructs(self, func):
228-
for _ in xrange(10):
228+
for _ in range(10):
229229
mjlib.mj_step(self.model.ptr, self.data.ptr)
230230
data2 = func(self.data)
231231
self.assertNotEqual(data2.ptr, self.data.ptr)
232232
for name in ["warning", "timer", "solver"]:
233233
self._assert_structs_equal(getattr(self.data, name), getattr(data2, name))
234-
for _ in xrange(10):
234+
for _ in range(10):
235235
mjlib.mj_step(self.model.ptr, self.data.ptr)
236236
mjlib.mj_step(data2.model.ptr, data2.ptr)
237237
for expected, actual in zip(self.data.timer, data2.timer):
@@ -358,7 +358,7 @@ def testDisableFlags(self):
358358
"""
359359
model = core.MjModel.from_xml_string(xml_string)
360360
data = core.MjData(model)
361-
for _ in xrange(100): # Let the simulation settle for a while.
361+
for _ in range(100): # Let the simulation settle for a while.
362362
mjlib.mj_step(model.ptr, data.ptr)
363363

364364
# With gravity and contact enabled, the cube should be stationary and the
@@ -376,7 +376,7 @@ def testDisableFlags(self):
376376
# If we disable contacts but not gravity then the cube should fall through
377377
# the floor.
378378
with model.disable(enums.mjtDisableBit.mjDSBL_CONTACT):
379-
for _ in xrange(10):
379+
for _ in range(10):
380380
mjlib.mj_step(model.ptr, data.ptr)
381381
self.assertLess(data.qvel[0], -0.1)
382382

@@ -400,7 +400,7 @@ def testDisableFlagsExceptions(self):
400400
lambda _: core.MjvScene(),
401401
"mjv_freeScene"))
402402
def testFree(self, constructor, destructor_name):
403-
for _ in xrange(5):
403+
for _ in range(5):
404404
destructor = getattr(mjlib, destructor_name)
405405
with mock.patch.object(
406406
core.mjlib, destructor_name, wraps=destructor) as mock_destructor:
@@ -421,7 +421,7 @@ def testFree(self, constructor, destructor_name):
421421

422422
@absltest.unittest.skipIf(render.DISABLED, render.DISABLED_MESSAGE)
423423
def testFreeMjrContext(self):
424-
for _ in xrange(5):
424+
for _ in range(5):
425425
renderer = render.Renderer(640, 480)
426426
with mock.patch.object(core.mjlib, "mjr_freeContext",
427427
wraps=mjlib.mjr_freeContext) as mock_destructor:
@@ -519,7 +519,7 @@ def setUp(self):
519519
self.data = core.MjData(self.model)
520520

521521
def _take_steps(self, n=5):
522-
for _ in xrange(n):
522+
for _ in range(n):
523523
mjlib.mj_step(self.model.ptr, self.data.ptr)
524524

525525

dm_control/mujoco/wrapper/util_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from dm_control.mujoco.wrapper import core
2929
from dm_control.mujoco.wrapper import util
3030

31-
from six.moves import xrange # pylint: disable=redefined-builtin
31+
from six.moves import range
3232

3333
_NUM_CALLS = 10000
3434
_RSS_GROWTH_TOLERANCE = 300 # Bytes
@@ -44,7 +44,7 @@ def test_buf_to_npy_no_memory_leak(self):
4444

4545
# This uses high water marks to find memory leaks in native code.
4646
old_max = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
47-
for _ in xrange(_NUM_CALLS):
47+
for _ in range(_NUM_CALLS):
4848
buf = util.buf_to_npy(src, shape)
4949
del buf
5050
new_max = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss

dm_control/render/executor/render_executor_test.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from absl.testing import parameterized
2828
from dm_control.render import executor
2929
import mock
30-
from six.moves import xrange # pylint: disable=redefined-builtin
30+
from six.moves import range
3131

3232

3333
def enforce_timeout(timeout):
@@ -88,15 +88,15 @@ def test_multithreaded(self, executor_type):
8888
def fill_list(thread_idx):
8989
def assign_value(i):
9090
shared_list[i] = thread_idx
91-
for _ in xrange(1000):
91+
for _ in range(1000):
9292
with render_executor.execution_context() as ctx:
93-
for i in xrange(list_length):
93+
for i in range(list_length):
9494
ctx.call(assign_value, i)
9595
# Other threads should be prevented from calling `assign_value` while
9696
# this thread is inside the `execution_context`.
9797
self.assertEqual(shared_list, [thread_idx] * list_length)
9898

99-
threads = [threading.Thread(target=fill_list, args=(i,)) for i in xrange(9)]
99+
threads = [threading.Thread(target=fill_list, args=(i,)) for i in range(9)]
100100
for thread in threads:
101101
thread.start()
102102
for thread in threads:

0 commit comments

Comments
 (0)