[tor-commits] [bridgedb/develop] Convert generators to lists

phw at torproject.org phw at torproject.org
Wed Feb 19 18:27:17 UTC 2020


commit 75f876ee89226977d358c2dfc42b931ac94f3430
Author: Damian Johnson <atagar at torproject.org>
Date:   Fri Jan 17 12:20:30 2020 -0800

    Convert generators to lists
    
    Python 3 changes several builtins to provide generators rather than lists. This
    is more efficient, but means we need to convert to a list to be indexable.
    
    There will likely be more instances down the road, but fixing all the instances
    that are immediately evedant in the test output. This fixes...
    
      Traceback (most recent call last):
        File "/home/atagar/Desktop/tor/bridgedb/bridgedb/test/test_proxy.py", line 355, in test_ProxySet_addProxies_tuple_individual_tags
          self.assertEquals(len(self.proxyList), len(self.proxies) + len(extraProxies))
      builtins.TypeError: object of type 'zip' has no len()
    
      Traceback (most recent call last):
        File "/home/atagar/Desktop/tor/bridgedb/bridgedb/test/test_parse_headers.py", line 47, in test_addNonLocalizedVariant
          langs = headers.parseAcceptLanguage(header)
        File "/home/atagar/Desktop/tor/bridgedb/bridgedb/parse/headers.py", line 70, in parseAcceptLanguage
          placement = langs.index(insertAfter[0]) + 1
      builtins.TypeError: 'filter' object is not subscriptable
    
      Traceback (most recent call last):
        File "/home/atagar/Desktop/tor/bridgedb/bridgedb/test/test_bridges.py", line 895, in setUp
          self._parseAllDescriptorFiles()
        File "/home/atagar/Desktop/tor/bridgedb/bridgedb/test/test_bridges.py", line 852, in _parseAllDescriptorFiles
          self._extrainfoFile).values()[0]
      builtins.TypeError: 'dict_values' object does not support indexing
    
      Traceback (most recent call last):
        File "/home/atagar/Desktop/tor/bridgedb/bridgedb/test/test_bridges.py", line 315, in test_integration_getConfigLine_scramblesuit_withFingerprint
          transport='scramblesuit')
        File "/home/atagar/Desktop/tor/bridgedb/bridgedb/bridges.py", line 847, in getConfigLine
          bridgeLine = self.getBridgeLine(bridgeRequest, includeFingerprint)
        File "/home/atagar/Desktop/tor/bridgedb/bridgedb/bridges.py", line 1362, in getBridgeLine
          pt = self._getTransportForRequest(bridgeRequest)
        File "/home/atagar/Desktop/tor/bridgedb/bridgedb/bridges.py", line 1196, in _getTransportForRequest
          return transports[position % len(unblocked)]
      builtins.TypeError: 'filter' object is not subscriptable
    
      Traceback (most recent call last):
        File "/home/atagar/Desktop/tor/bridgedb/bridgedb/test/test_Bridges.py", line 78, in test_filterDistinctSubnets_random_bridges
          chosen = self.ring.bridges.keys()[:3]
      builtins.TypeError: 'dict_keys' object is not subscriptable
    
      Traceback (most recent call last):
        File "/home/atagar/Desktop/tor/bridgedb/bridgedb/test/test_Bridges.py", line 65, in test_filterDistinctSubnets
          chosen = self.ring.bridges.keys()[:10]
      builtins.TypeError: 'dict_keys' object is not subscriptable
    
    Test results changed as follows...
    
      before: FAILED (skips=114, failures=13, errors=191, successes=663)
      after:  FAILED (skips=114, failures=14, errors=158, successes=695)
---
 bridgedb/bridges.py           | 2 +-
 bridgedb/parse/headers.py     | 4 ++--
 bridgedb/test/test_Bridges.py | 4 ++--
 bridgedb/test/test_bridges.py | 8 ++++----
 bridgedb/test/test_proxy.py   | 2 +-
 5 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/bridgedb/bridges.py b/bridgedb/bridges.py
index ca1330b..a611c47 100644
--- a/bridgedb/bridges.py
+++ b/bridgedb/bridges.py
@@ -1177,7 +1177,7 @@ class Bridge(BridgeBackwardsCompatibility):
         # their ``methodname`` matches the requested transport:
         transports = filter(lambda pt: pt.methodname == desired, self.transports)
         # Filter again for whichever of IPv4 or IPv6 was requested:
-        transports = filter(lambda pt: pt.address.version == ipVersion, transports)
+        transports = list(filter(lambda pt: pt.address.version == ipVersion, transports))
 
         if not transports:
             raise PluggableTransportUnavailable(
diff --git a/bridgedb/parse/headers.py b/bridgedb/parse/headers.py
index 5c3597a..fa8cbf2 100644
--- a/bridgedb/parse/headers.py
+++ b/bridgedb/parse/headers.py
@@ -64,8 +64,8 @@ def parseAcceptLanguage(header):
     for only in langsOnly:
         if only not in langs:
             # Add the fallback after the other languages like it:
-            insertAfter = filter(lambda x: x.startswith(only),
-                                 [x for x in langs])
+            insertAfter = list(filter(lambda x: x.startswith(only),
+                                 [x for x in langs]))
             if insertAfter:
                 placement = langs.index(insertAfter[0]) + 1
                 langs.insert(placement, only)
diff --git a/bridgedb/test/test_Bridges.py b/bridgedb/test/test_Bridges.py
index 5991952..19ab09c 100644
--- a/bridgedb/test/test_Bridges.py
+++ b/bridgedb/test/test_Bridges.py
@@ -62,7 +62,7 @@ class BridgeRingTests(unittest.TestCase):
         """
         self.addBridgesFromSameSubnet()
 
-        chosen = self.ring.bridges.keys()[:10]
+        chosen = list(self.ring.bridges.keys())[:10]
         bridges = self.ring.filterDistinctSubnets(chosen)
 
         # Since they're all in the same /16, we should only get one
@@ -75,7 +75,7 @@ class BridgeRingTests(unittest.TestCase):
         """
         self.addRandomBridges()
 
-        chosen = self.ring.bridges.keys()[:3]
+        chosen = list(self.ring.bridges.keys())[:3]
         bridges = self.ring.filterDistinctSubnets(chosen)
 
         self.assertGreaterEqual(len(bridges), 1)
diff --git a/bridgedb/test/test_bridges.py b/bridgedb/test/test_bridges.py
index f2c2876..cf0ee23 100644
--- a/bridgedb/test/test_bridges.py
+++ b/bridgedb/test/test_bridges.py
@@ -848,10 +848,10 @@ class BridgeTests(unittest.TestCase):
             self._networkstatusFile)[0]
         self.serverdescriptor = descriptors.parseServerDescriptorsFile(
             self._serverDescriptorFile)[0]
-        self.extrainfo = descriptors.parseExtraInfoFiles(
-            self._extrainfoFile).values()[0]
-        self.extrainfoNew = descriptors.parseExtraInfoFiles(
-            self._extrainfoNewFile).values()[0]
+        self.extrainfo = list(descriptors.parseExtraInfoFiles(
+            self._extrainfoFile).values())[0]
+        self.extrainfoNew = list(descriptors.parseExtraInfoFiles(
+            self._extrainfoNewFile).values())[0]
 
     def _writeNetworkstatus(self, networkstatus):
         with open(self._networkstatusFile, 'w') as fh:
diff --git a/bridgedb/test/test_proxy.py b/bridgedb/test/test_proxy.py
index 74290bc..69b2b99 100644
--- a/bridgedb/test/test_proxy.py
+++ b/bridgedb/test/test_proxy.py
@@ -350,7 +350,7 @@ class ProxySetUnittests(unittest.TestCase):
         whatever tags we want.
         """
         tags = ['foo', 'bar', 'baz']
-        extraProxies = zip(self.moarProxies, tags)
+        extraProxies = list(zip(self.moarProxies, tags))
         self.proxyList.addProxies(extraProxies)
         self.assertEquals(len(self.proxyList), len(self.proxies) + len(extraProxies))
         self.assertIn(extraProxies[0][0], self.proxyList)





More information about the tor-commits mailing list