changeset 49285:56f98406831b

py3: remove xrange() compatibility code Some code used its own xrange() compatibility code instead of pycompat.xrange().
author Manuel Jacob <me@manueljacob.de>
date Sun, 29 May 2022 15:32:43 +0200
parents d44e3c45f0e4
children b2e05a8e15d1
files tests/bruterebase.py tests/fsmonitor-run-tests.py tests/run-tests.py tests/seq.py tests/test-ancestor.py tests/test-atomictempfile.py tests/test-fastannotate-revmap.py tests/test-filecache.py tests/test-manifest.py tests/test-pathencode.py tests/test-remotefilelog-cacheprocess.t tests/test-ui-verbosity.py
diffstat 12 files changed, 40 insertions(+), 75 deletions(-) [+]
line wrap: on
line diff
--- a/tests/bruterebase.py	Sun May 29 15:17:27 2022 +0200
+++ b/tests/bruterebase.py	Sun May 29 15:32:43 2022 +0200
@@ -14,11 +14,6 @@
 
 from hgext import rebase
 
-try:
-    xrange
-except NameError:
-    xrange = range
-
 cmdtable = {}
 command = registrar.command(cmdtable)
 
@@ -41,7 +36,7 @@
                 result += b"'"
             return result
 
-        for i in xrange(1, 2 ** len(srevs)):
+        for i in range(1, 2 ** len(srevs)):
             subset = [rev for j, rev in enumerate(srevs) if i & (1 << j) != 0]
             spec = revsetlang.formatspec(b'%ld', subset)
             tr = repo.transaction(b'rebase')
@@ -58,7 +53,7 @@
                     # short summary about new nodes
                     cl = repo.changelog
                     descs = []
-                    for rev in xrange(repolen, len(repo)):
+                    for rev in range(repolen, len(repo)):
                         desc = b'%s:' % getdesc(rev)
                         for prev in cl.parentrevs(rev):
                             if prev > -1:
--- a/tests/fsmonitor-run-tests.py	Sun May 29 15:17:27 2022 +0200
+++ b/tests/fsmonitor-run-tests.py	Sun May 29 15:32:43 2022 +0200
@@ -26,7 +26,6 @@
 
 if sys.version_info > (3, 5, 0):
     PYTHON3 = True
-    xrange = range  # we use xrange in one place, and we'd rather not use range
 
     def _sys2bytes(p):
         return p.encode('utf-8')
--- a/tests/run-tests.py	Sun May 29 15:17:27 2022 +0200
+++ b/tests/run-tests.py	Sun May 29 15:32:43 2022 +0200
@@ -151,9 +151,6 @@
 origenviron = os.environ.copy()
 
 
-xrange = range  # we use xrange in one place, and we'd rather not use range
-
-
 def _sys2bytes(p):
     if p is None:
         return p
@@ -1422,7 +1419,7 @@
         env['HGTEST_TIMEOUT_DEFAULT'] = formated_timeout
         env['HGTEST_TIMEOUT'] = _bytes2sys(b"%d" % self._timeout)
         # This number should match portneeded in _getport
-        for port in xrange(3):
+        for port in range(3):
             # This list should be parallel to _portmap in _getreplacements
             defineport(port)
         env["HGRCPATH"] = _bytes2sys(os.path.join(self._threadtmp, b'.hgrc'))
@@ -2503,7 +2500,7 @@
 
                     if ignored:
                         continue
-            for _ in xrange(self._runs_per_test):
+            for _ in range(self._runs_per_test):
                 tests.append(get())
 
         runtests = list(tests)
@@ -2552,7 +2549,7 @@
                 with iolock:
                     sys.stdout.write(d + '  ')
                     sys.stdout.flush()
-                for x in xrange(10):
+                for x in range(10):
                     if channels:
                         time.sleep(0.1)
                 count += 1
@@ -3502,10 +3499,10 @@
         if port is None:
             portneeded = 3
             # above 100 tries we just give up and let test reports failure
-            for tries in xrange(100):
+            for tries in range(100):
                 allfree = True
                 port = self.options.port + self._portoffset
-                for idx in xrange(portneeded):
+                for idx in range(portneeded):
                     if not checkportisavailable(port + idx):
                         allfree = False
                         break
--- a/tests/seq.py	Sun May 29 15:17:27 2022 +0200
+++ b/tests/seq.py	Sun May 29 15:32:43 2022 +0200
@@ -19,9 +19,6 @@
 except ImportError:
     pass
 
-if sys.version_info[0] >= 3:
-    xrange = range
-
 start = 1
 if len(sys.argv) > 2:
     start = int(sys.argv[1])
@@ -32,5 +29,5 @@
 
 stop = int(sys.argv[-1]) + 1
 
-for i in xrange(start, stop, step):
+for i in range(start, stop, step):
     print(i)
--- a/tests/test-ancestor.py	Sun May 29 15:17:27 2022 +0200
+++ b/tests/test-ancestor.py	Sun May 29 15:32:43 2022 +0200
@@ -18,7 +18,6 @@
 
 if pycompat.ispy3:
     long = int
-    xrange = range
 
 
 def buildgraph(rng, nodes=100, rootprob=0.05, mergeprob=0.2, prevprob=0.7):
@@ -30,7 +29,7 @@
     return value is a graph represented as an adjacency list.
     """
     graph = [None] * nodes
-    for i in xrange(nodes):
+    for i in range(nodes):
         if i == 0 or rng.random() < rootprob:
             graph[i] = [nullrev]
         elif i == 1:
@@ -53,7 +52,7 @@
 
 def buildancestorsets(graph):
     ancs = [None] * len(graph)
-    for i in xrange(len(graph)):
+    for i in range(len(graph)):
         ancs[i] = {i}
         if graph[i] == [nullrev]:
             continue
@@ -114,11 +113,11 @@
         nerrs[0] += 1
         gerrs[0] += 1
 
-    for g in xrange(graphcount):
+    for g in range(graphcount):
         graph = buildgraph(rng)
         ancs = buildancestorsets(graph)
         gerrs = [0]
-        for _ in xrange(testcount):
+        for _ in range(testcount):
             # start from nullrev to include it as a possibility
             graphnodes = range(nullrev, len(graph))
             bases = samplerevs(graphnodes)
@@ -128,7 +127,7 @@
             # reference slow algorithm
             naiveinc = naiveincrementalmissingancestors(ancs, bases)
             seq = []
-            for _ in xrange(inccount):
+            for _ in range(inccount):
                 if rng.random() < 0.2:
                     newbases = samplerevs(graphnodes)
                     seq.append(('addbases', newbases))
@@ -215,7 +214,7 @@
     """
     for i, (bases, revs) in enumerate(
         (
-            ({1, 2, 3, 4, 7}, set(xrange(10))),
+            ({1, 2, 3, 4, 7}, set(range(10))),
             ({10}, set({11, 12, 13, 14})),
             ({7}, set({1, 2, 3, 4, 5})),
         )
--- a/tests/test-atomictempfile.py	Sun May 29 15:17:27 2022 +0200
+++ b/tests/test-atomictempfile.py	Sun May 29 15:32:43 2022 +0200
@@ -6,15 +6,11 @@
 import unittest
 
 from mercurial import (
-    pycompat,
     util,
 )
 
 atomictempfile = util.atomictempfile
 
-if pycompat.ispy3:
-    xrange = range
-
 
 class testatomictempfile(unittest.TestCase):
     def setUp(self):
@@ -68,7 +64,7 @@
 
         # try some times, because reproduction of ambiguity depends on
         # "filesystem time"
-        for i in xrange(5):
+        for i in range(5):
             atomicwrite(False)
             oldstat = os.stat(self._filename)
             if oldstat[stat.ST_CTIME] != oldstat[stat.ST_MTIME]:
@@ -79,7 +75,7 @@
 
             # repeat atomic write with checkambig=True, to examine
             # whether st_mtime is advanced multiple times as expected
-            for j in xrange(repetition):
+            for j in range(repetition):
                 atomicwrite(True)
             newstat = os.stat(self._filename)
             if oldstat[stat.ST_CTIME] != newstat[stat.ST_CTIME]:
--- a/tests/test-fastannotate-revmap.py	Sun May 29 15:17:27 2022 +0200
+++ b/tests/test-fastannotate-revmap.py	Sun May 29 15:32:43 2022 +0200
@@ -8,9 +8,6 @@
 
 from hgext.fastannotate import error, revmap
 
-if pycompat.ispy3:
-    xrange = range
-
 
 def genhsh(i):
     return pycompat.bytechr(i) + b'\0' * 19
@@ -33,7 +30,7 @@
 
     rm = revmap.revmap(path)
     ensure(rm.maxrev == 0)
-    for i in xrange(5):
+    for i in range(5):
         ensure(rm.rev2hsh(i) is None)
     ensure(rm.hsh2rev(b'\0' * 20) is None)
 
@@ -51,11 +48,11 @@
         b'a',
         b'a',
     ]
-    for i in xrange(1, 5):
+    for i in range(1, 5):
         ensure(rm.append(genhsh(i), sidebranch=(i & 1), path=paths[i]) == i)
 
     ensure(rm.maxrev == 4)
-    for i in xrange(1, 5):
+    for i in range(1, 5):
         ensure(rm.hsh2rev(genhsh(i)) == i)
         ensure(rm.rev2hsh(i) == genhsh(i))
 
@@ -63,13 +60,13 @@
     rm.flush()
     rm = revmap.revmap(path)
     ensure(rm.maxrev == 4)
-    for i in xrange(1, 5):
+    for i in range(1, 5):
         ensure(rm.hsh2rev(genhsh(i)) == i)
         ensure(rm.rev2hsh(i) == genhsh(i))
         ensure(bool(rm.rev2flag(i) & revmap.sidebranchflag) == bool(i & 1))
 
     # append without calling save() explicitly
-    for i in xrange(5, 12):
+    for i in range(5, 12):
         ensure(
             rm.append(genhsh(i), sidebranch=(i & 1), path=paths[i], flush=True)
             == i
@@ -78,7 +75,7 @@
     # re-load and verify
     rm = revmap.revmap(path)
     ensure(rm.maxrev == 11)
-    for i in xrange(1, 12):
+    for i in range(1, 12):
         ensure(rm.hsh2rev(genhsh(i)) == i)
         ensure(rm.rev2hsh(i) == genhsh(i))
         ensure(rm.rev2path(i) == paths[i] or paths[i - 1])
@@ -148,7 +145,7 @@
 def testcopyfrom():
     path = gettemppath()
     rm = revmap.revmap(path)
-    for i in xrange(1, 10):
+    for i in range(1, 10):
         ensure(
             rm.append(genhsh(i), sidebranch=(i & 1), path=(b'%d' % (i // 3)))
             == i
@@ -185,21 +182,21 @@
     path = gettemppath()
 
     rm = revmap.revmap(path)
-    for i in xrange(1, 5):
+    for i in range(1, 5):
         ensure(rm.append(genhsh(i), sidebranch=(i & 1)) == i)
 
-    for i in xrange(1, 5):
+    for i in range(1, 5):
         ensure(((genhsh(i), None) in rm) == ((i & 1) == 0))
         ensure((fakefctx(genhsh(i)) in rm) == ((i & 1) == 0))
-    for i in xrange(5, 10):
+    for i in range(5, 10):
         ensure(fakefctx(genhsh(i)) not in rm)
         ensure((genhsh(i), None) not in rm)
 
     # "contains" checks paths
     rm = revmap.revmap()
-    for i in xrange(1, 5):
+    for i in range(1, 5):
         ensure(rm.append(genhsh(i), path=(b'%d' % (i // 2))) == i)
-    for i in xrange(1, 5):
+    for i in range(1, 5):
         ensure(fakefctx(genhsh(i), path=(b'%d' % (i // 2))) in rm)
         ensure(fakefctx(genhsh(i), path=b'a') not in rm)
 
@@ -209,7 +206,7 @@
     ensure(revmap.getlastnode(path) is None)
     rm = revmap.revmap(path)
     ensure(revmap.getlastnode(path) is None)
-    for i in xrange(1, 10):
+    for i in range(1, 10):
         hsh = genhsh(i)
         rm.append(hsh, path=(b'%d' % (i // 2)), flush=True)
         ensure(revmap.getlastnode(path) == hsh)
--- a/tests/test-filecache.py	Sun May 29 15:17:27 2022 +0200
+++ b/tests/test-filecache.py	Sun May 29 15:32:43 2022 +0200
@@ -31,9 +31,6 @@
     vfs as vfsmod,
 )
 
-if pycompat.ispy3:
-    xrange = range
-
 
 class fakerepo:
     def __init__(self):
@@ -214,7 +211,7 @@
 
     # try some times, because reproduction of ambiguity depends on
     # "filesystem time"
-    for i in xrange(5):
+    for i in range(5):
         fp = open(filename, 'w')
         fp.write('FOO')
         fp.close()
@@ -228,7 +225,7 @@
 
         # repeat changing via checkambigatclosing, to examine whether
         # st_mtime is advanced multiple times as expected
-        for i in xrange(repetition):
+        for i in range(repetition):
             # explicit closing
             fp = vfsmod.checkambigatclosing(open(filename, 'a'))
             fp.write('FOO')
--- a/tests/test-manifest.py	Sun May 29 15:17:27 2022 +0200
+++ b/tests/test-manifest.py	Sun May 29 15:32:43 2022 +0200
@@ -59,14 +59,12 @@
 HUGE_MANIFEST_ENTRIES = 200001
 
 izip = getattr(itertools, 'izip', zip)
-if 'xrange' not in globals():
-    xrange = range
 
 A_HUGE_MANIFEST = b''.join(
     sorted(
         b'file%d\0%s%s\n' % (i, h, f)
         for i, h, f in izip(
-            xrange(200001),
+            range(200001),
             itertools.cycle((HASH_1, HASH_2)),
             itertools.cycle((b'', b'x', b'l')),
         )
--- a/tests/test-pathencode.py	Sun May 29 15:17:27 2022 +0200
+++ b/tests/test-pathencode.py	Sun May 29 15:32:43 2022 +0200
@@ -19,11 +19,6 @@
     store,
 )
 
-try:
-    xrange
-except NameError:
-    xrange = range
-
 validchars = set(map(pycompat.bytechr, range(0, 256)))
 alphanum = range(ord('A'), ord('Z'))
 
@@ -32,8 +27,8 @@
 
 winreserved = (
     b'aux con prn nul'.split()
-    + [b'com%d' % i for i in xrange(1, 10)]
-    + [b'lpt%d' % i for i in xrange(1, 10)]
+    + [b'com%d' % i for i in range(1, 10)]
+    + [b'lpt%d' % i for i in range(1, 10)]
 )
 
 
@@ -43,8 +38,8 @@
     combos = set()
 
     for r in names:
-        for i in xrange(len(r) + 1):
-            for c in itertools.combinations(xrange(len(r)), i):
+        for i in range(len(r) + 1):
+            for c in itertools.combinations(range(len(r)), i):
                 d = r
                 for j in c:
                     d = b''.join((d[:j], d[j : j + 1].upper(), d[j + 1 :]))
@@ -211,7 +206,7 @@
 
     return (
         b'data/'
-        + b'/'.join(makepart(rng, k) for _ in xrange(j))
+        + b'/'.join(makepart(rng, k) for _ in range(j))
         + rng.choice([b'.d', b'.i'])
     )
 
@@ -222,7 +217,7 @@
     mink, maxk = 1, 4096
 
     def steps():
-        for i in xrange(count):
+        for i in range(count):
             yield mink + int(round(math.sqrt((maxk - mink) * float(i) / count)))
 
     for k in steps():
--- a/tests/test-remotefilelog-cacheprocess.t	Sun May 29 15:17:27 2022 +0200
+++ b/tests/test-remotefilelog-cacheprocess.t	Sun May 29 15:32:43 2022 +0200
@@ -18,8 +18,6 @@
   > import os
   > import shutil
   > import sys
-  > if sys.version_info[0] > 2:
-  >     xrange = range
   > f = open('$TESTTMP/cachelog.log', 'w')
   > srccache = os.path.join('$TESTTMP', 'oldhgcache')
   > def log(message):
@@ -36,7 +34,7 @@
   >             count = int(sys.stdin.readline())
   >             log('client wants %r blobs\n' % count)
   >             wants = []
-  >             for _ in xrange(count):
+  >             for _ in range(count):
   >                 key = sys.stdin.readline()[:-1]
   >                 wants.append(key)
   >                 if '\0' in key:
--- a/tests/test-ui-verbosity.py	Sun May 29 15:17:27 2022 +0200
+++ b/tests/test-ui-verbosity.py	Sun May 29 15:32:43 2022 +0200
@@ -4,9 +4,6 @@
     ui as uimod,
 )
 
-if pycompat.ispy3:
-    xrange = range
-
 hgrc = os.environ['HGRCPATH']
 f = open(hgrc)
 basehgrc = f.read()
@@ -15,7 +12,7 @@
 print('      hgrc settings    command line options      final result   ')
 print('    quiet verbo debug   quiet verbo debug      quiet verbo debug')
 
-for i in xrange(64):
+for i in range(64):
     hgrc_quiet = bool(i & 1 << 0)
     hgrc_verbose = bool(i & 1 << 1)
     hgrc_debug = bool(i & 1 << 2)