[tor-commits] [bridgedb/master] Replace usage of xrange

phw at torproject.org phw at torproject.org
Wed Feb 19 18:26:37 UTC 2020


commit 8d9ebc7b0e807618ae79951ea850396b4788b3d5
Author: Damian Johnson <atagar at torproject.org>
Date:   Fri Jan 10 13:07:22 2020 -0800

    Replace usage of xrange
    
    Python 2.x had two functios to get a numeric range...
    
      * range() returned a list of numbers
      * xrange() returned a generator of numbers
    
    The later was slightly more efficient for loops, so as a rule of thumb 99% of
    the time you wanted xrange().
    
    Python 3.x only has a single range() function which uses xrange()'s behavior.
    If you want the old range()'s behavior (ie. to get a list) you do...
    
      my_list = list(range(from, to))
    
    This fixes the following stacktraces...
    
      Traceback (most recent call last):
        File "/home/atagar/Desktop/tor/bridgedb/bridgedb/test/test_schedule.py", line 188, in test_ScheduledInterval_getInterval_minute
          self._check_getInterval(15, 'minutes', 15*60)
        File "/home/atagar/Desktop/tor/bridgedb/bridgedb/test/test_schedule.py", line 171, in _check_getInterval
          [secs.append(0) for _ in xrange(6-len(secs))]
      builtins.NameError: name 'xrange' is not defined
    
    Test results change as follows:
    
      before: FAILED (skips=1, failures=3, errors=61, successes=242)
      after:  FAILED (skips=1, failures=6, errors=54, successes=246)
---
 bridgedb/Bridges.py                     |  2 +-
 bridgedb/test/email_helpers.py          |  4 ++--
 bridgedb/test/legacy_Tests.py           | 30 +++++++++++++++---------------
 bridgedb/test/test_https_distributor.py |  8 ++++----
 bridgedb/test/test_parse_addr.py        |  4 ++--
 bridgedb/test/test_schedule.py          |  2 +-
 bridgedb/test/util.py                   |  8 ++++----
 7 files changed, 29 insertions(+), 29 deletions(-)

diff --git a/bridgedb/Bridges.py b/bridgedb/Bridges.py
index 3c94fdf..f80f4cd 100644
--- a/bridgedb/Bridges.py
+++ b/bridgedb/Bridges.py
@@ -405,7 +405,7 @@ class FixedBridgeSplitter(object):
             description is ``"IPv6 obfs2 bridges"`` the line would read:
             ``"IPv6 obfs2 bridges ring=3"``.
         """
-        for index, ring in zip(xrange(len(self.rings)), self.rings):
+        for index, ring in zip(range(len(self.rings)), self.rings):
             ring.dumpAssignments(filename, "%s ring=%s" % (description, index))
 
 
diff --git a/bridgedb/test/email_helpers.py b/bridgedb/test/email_helpers.py
index 645fc93..e149dd5 100644
--- a/bridgedb/test/email_helpers.py
+++ b/bridgedb/test/email_helpers.py
@@ -138,7 +138,7 @@ class DummyEmailDistributor(object):
         self.answerParameters = answerParameters
 
     def getBridges(self, bridgeRequest, epoch):
-        return [util.DummyBridge() for _ in xrange(self._bridgesPerResponseMin)]
+        return [util.DummyBridge() for _ in range(self._bridgesPerResponseMin)]
 
     def cleanDatabase(self):
         pass
@@ -167,7 +167,7 @@ class DummyEmailDistributorWithState(DummyEmailDistributor):
         self.alreadySeen[bridgeRequest.client] += 1
 
         if self.alreadySeen[bridgeRequest.client] <= 1:
-            return [util.DummyBridge() for _ in xrange(self._bridgesPerResponseMin)]
+            return [util.DummyBridge() for _ in range(self._bridgesPerResponseMin)]
         elif self.alreadySeen[bridgeRequest.client] == 2:
             raise TooSoonEmail(
                 "Seen client '%s' %d times"
diff --git a/bridgedb/test/legacy_Tests.py b/bridgedb/test/legacy_Tests.py
index 00e1d12..37bce70 100644
--- a/bridgedb/test/legacy_Tests.py
+++ b/bridgedb/test/legacy_Tests.py
@@ -61,17 +61,17 @@ def fakeBridge(orport=8080, running=True, stable=True, or_addresses=False,
         transports=False):
     ip = randomIPv4()
     nn = "bridge-%s" % int(ip)
-    fp = "".join([random.choice("0123456789ABCDEF") for _ in xrange(40)])
+    fp = "".join([random.choice("0123456789ABCDEF") for _ in range(40)])
     b = bridgedb.Bridges.Bridge(nn,ip,orport,fingerprint=fp)
     b.setStatus(running, stable)
 
     oraddrs = []
     if or_addresses:
-        for i in xrange(8):
+        for i in range(8):
             b.orAddresses.append((randomValidIPv6(), randomPort(), 6))
 
     if transports:
-        for i in xrange(0,8):
+        for i in range(0,8):
             b.transports.append(bridgedb.Bridges.PluggableTransport(b,
                 random.choice(["obfs", "obfs2", "pt1"]),
                 randomIP(), randomPort()))
@@ -81,17 +81,17 @@ def fakeBridge6(orport=8080, running=True, stable=True, or_addresses=False,
         transports=False):
     ip = randomIPv6()
     nn = "bridge-%s" % int(ip)
-    fp = "".join([random.choice("0123456789ABCDEF") for _ in xrange(40)])
+    fp = "".join([random.choice("0123456789ABCDEF") for _ in range(40)])
     b = bridgedb.Bridges.Bridge(nn,ip,orport,fingerprint=fp)
     b.setStatus(running, stable)
 
     oraddrs = []
     if or_addresses:
-        for i in xrange(8):
+        for i in range(8):
             b.orAddresses.append((randomValidIPv6(), randomPort(), 6))
 
     if transports:
-        for i in xrange(0,8):
+        for i in range(0,8):
             b.transports.append(bridgedb.Bridges.PluggableTransport(b,
                 random.choice(["obfs", "obfs2", "pt1"]),
                 randomIP(), randomPort()))
@@ -226,7 +226,7 @@ class BridgeStabilityTests(unittest.TestCase):
         db = self.db
         b = random.choice([fakeBridge,fakeBridge6])()
         def timestampSeries(x):
-            for i in xrange(61):
+            for i in range(61):
                 yield (i+1)*60*30 + x # 30 minute intervals
         now = time.time()
         time_on_address = long(60*30*60) # 30 hours
@@ -244,17 +244,17 @@ class BridgeStabilityTests(unittest.TestCase):
 
     def testLastSeenWithDifferentAddressAndPort(self):
         db = self.db
-        for i in xrange(10):
+        for i in range(10):
             num_desc = 30
             time_start = time.time()
-            ts = [ 60*30*(i+1) + time_start for i in xrange(num_desc) ]
+            ts = [ 60*30*(i+1) + time_start for i in range(num_desc) ]
             b = random.choice([fakeBridge(), fakeBridge6()])
             [ bridgedb.Stability.addOrUpdateBridgeHistory(b, t) for t in ts ]
 
             # change the port
             b.orport = b.orport+1
             last_seen = ts[-1]
-            ts = [ 60*30*(i+1) + last_seen for i in xrange(num_desc) ]
+            ts = [ 60*30*(i+1) + last_seen for i in range(num_desc) ]
             [ bridgedb.Stability.addOrUpdateBridgeHistory(b, t) for t in ts ]
             b = db.getBridgeHistory(b.fingerprint)
             assert b.tosa == ts[-1] - last_seen
@@ -267,11 +267,11 @@ class BridgeStabilityTests(unittest.TestCase):
         num_bridges = 10
         num_desc = 4*48 # 30m intervals, 48 per day
         time_start = time.time()
-        bridges = [ fakeBridge() for x in xrange(num_bridges) ]
+        bridges = [ fakeBridge() for x in range(num_bridges) ]
         t = time.time()
-        ts = [ (i+1)*60*30+t for i in xrange(num_bridges) ]
+        ts = [ (i+1)*60*30+t for i in range(num_bridges) ]
         for b in bridges:
-            time_series = [ 60*30*(i+1) + time_start for i in xrange(num_desc) ]
+            time_series = [ 60*30*(i+1) + time_start for i in range(num_desc) ]
             [ bridgedb.Stability.addOrUpdateBridgeHistory(b, i) for i in time_series ]
         assert None not in bridges
         # +1 to avoid rounding errors
@@ -288,7 +288,7 @@ class BridgeStabilityTests(unittest.TestCase):
         num_bridges = 20
         time_start = time.time()
         bridges = [random.choice([fakeBridge, fakeBridge6])()
-                   for i in xrange(num_bridges)]
+                   for i in range(num_bridges)]
 
         # run some of the bridges for the full time series
         running = bridges[:num_bridges/2]
@@ -303,7 +303,7 @@ class BridgeStabilityTests(unittest.TestCase):
         # figure out how many intervals it will take for weightedUptime to
         # decay to < 1
         num_desc = int(30*log(1/float(num_successful*30*60))/(-0.05))
-        timeseries = [ 60*30*(i+1) + time_start for i in xrange(num_desc) ]
+        timeseries = [ 60*30*(i+1) + time_start for i in range(num_desc) ]
 
         for i in timeseries:
             for b in running:
diff --git a/bridgedb/test/test_https_distributor.py b/bridgedb/test/test_https_distributor.py
index c25c598..dd56785 100644
--- a/bridgedb/test/test_https_distributor.py
+++ b/bridgedb/test/test_https_distributor.py
@@ -261,7 +261,7 @@ class HTTPSDistributorTests(unittest.TestCase):
 
         [dist.insert(bridge) for bridge in bridges]
 
-        for i in xrange(5):
+        for i in range(5):
             bridgeRequest1 = self.randomClientRequestForNotBlockedIn('cn')
             bridgeRequest1.transports.append('obfs2')
             bridgeRequest1.generateFilters()
@@ -335,7 +335,7 @@ class HTTPSDistributorTests(unittest.TestCase):
        [dist.insert(bridge) for bridge in bridges]
        [dist.insert(bridge) for bridge in self.bridges[:250]]
 
-       for _ in xrange(32):
+       for _ in range(32):
            bridgeRequest = self.randomClientRequest()
            answer = dist.getBridges(bridgeRequest, 1)
            count = 0
@@ -399,7 +399,7 @@ class HTTPSDistributorTests(unittest.TestCase):
         dist = distributor.HTTPSDistributor(3, self.key)
         [dist.insert(bridge) for bridge in self.bridges[:250]]
 
-        for i in xrange(500):
+        for i in range(500):
             bridgeRequest = self.randomClientRequest()
             bridgeRequest.withIPv6()
             bridgeRequest.generateFilters()
@@ -421,7 +421,7 @@ class HTTPSDistributorTests(unittest.TestCase):
         dist = distributor.HTTPSDistributor(1, self.key)
         [dist.insert(bridge) for bridge in self.bridges[:250]]
 
-        for i in xrange(500):
+        for i in range(500):
             bridgeRequest = self.randomClientRequest()
             bridgeRequest.generateFilters()
 
diff --git a/bridgedb/test/test_parse_addr.py b/bridgedb/test/test_parse_addr.py
index 81e4508..124ea0f 100644
--- a/bridgedb/test/test_parse_addr.py
+++ b/bridgedb/test/test_parse_addr.py
@@ -669,7 +669,7 @@ class PortListTest(unittest.TestCase):
         ports.
         """
         tooMany = addr.PortList.PORTSPEC_LEN + 1
-        ports = [self.getRandomPort() for x in xrange(tooMany)]
+        ports = [self.getRandomPort() for x in range(tooMany)]
         log.msg("Testing addr.PortList(%s))"
                 % ', '.join([type('')(port) for port in ports]))
         portList = addr.PortList(*ports)
@@ -699,7 +699,7 @@ class PortListTest(unittest.TestCase):
         ports = (443, 9001, 9030)
         portList = addr.PortList(*ports)
         iterator = iter(portList)
-        for x in xrange(len(ports)):
+        for x in range(len(ports)):
             self.assertIn(iterator.next(), portList)
 
     def test_str(self):
diff --git a/bridgedb/test/test_schedule.py b/bridgedb/test/test_schedule.py
index 1bcec07..c95d6f9 100644
--- a/bridgedb/test/test_schedule.py
+++ b/bridgedb/test/test_schedule.py
@@ -168,7 +168,7 @@ class ScheduledIntervalTests(unittest.TestCase):
         ts = sched.getInterval(now)
         self.assertIsInstance(ts, str)
         secs = [int(x) for x in ts.replace('-', ' ').replace(':', ' ').split()]
-        [secs.append(0) for _ in xrange(6-len(secs))]
+        [secs.append(0) for _ in range(6-len(secs))]
         secs = schedule.calendar.timegm(secs)
         self.assertApproximates(now, secs, variance)
 
diff --git a/bridgedb/test/util.py b/bridgedb/test/util.py
index 9fc16b8..43219d5 100644
--- a/bridgedb/test/util.py
+++ b/bridgedb/test/util.py
@@ -135,7 +135,7 @@ def randomIPv6():
     return ipaddr.IPv6Address(random.getrandbits(128))
 
 def randomIP():
-    if random.choice(xrange(2)):
+    if random.choice(range(2)):
         return randomIPv4()
     return randomIPv6()
 
@@ -146,7 +146,7 @@ def randomIPv6String():
     return bracketIPv6(randomIPv6().compressed)
 
 def randomIPString():
-    if random.choice(xrange(2)):
+    if random.choice(range(2)):
         return randomIPv4String()
     return randomIPv6String()
 
@@ -194,7 +194,7 @@ def generateFakeBridges(n=500):
         # Real tor currently only supports one extra ORAddress, and it can
         # only be IPv6.
         addrs = [(randomValidIPv6(), randomHighPort(), 6)]
-        fpr = "".join(random.choice('abcdef0123456789') for _ in xrange(40))
+        fpr = "".join(random.choice('abcdef0123456789') for _ in range(40))
 
         supported = ["obfs2", "obfs3", "fte"]
         transports = []
@@ -282,7 +282,7 @@ class DummyBridge(object):
         self.address = ipaddr.IPv4Address(ipv4)
         self.orPort = randomPort()
         self.fingerprint = "".join(random.choice('abcdef0123456789')
-                                   for _ in xrange(40))
+                                   for _ in range(40))
         self.orAddresses = [(randomIPv6(), randomPort(), 6)]
 
     def getBridgeLine(self, bridgeRequest, includeFingerprint=True):





More information about the tor-commits mailing list