commit cfd2b45e9525355ded12885f2a8d48899e3ba573 Author: Damian Johnson atagar@torproject.org Date: Fri Jan 10 12:57:33 2020 -0800
Replace basestring with str
Python 3.x does not have a basestring class...
Traceback (most recent call last): File "/home/atagar/Desktop/tor/bridgedb/bridgedb/test/test_parse_addr.py", line 277, in test_isIPAddress_IP4DefaultRoute self.runTestForAddr(IP4DefaultRoute) File "/home/atagar/Desktop/tor/bridgedb/bridgedb/test/test_parse_addr.py", line 225, in runTestForAddr result = addr.isIPAddress(testAddress) File "/home/atagar/Desktop/tor/bridgedb/bridgedb/parse/addr.py", line 314, in isIPAddress if isValidIP(ip): File "/home/atagar/Desktop/tor/bridgedb/bridgedb/parse/addr.py", line 410, in isValidIP if isinstance(ip, basestring): builtins.NameError: name 'basestring' is not defined
Some replacement might *not* work. To be fully a drop-in replacement instead have...
python 2: isisntance(msg, basestring) python 3: isinstance(msg, (str, bytes))
... however, many of these code paths would break if they received a bytes rather than a unicode object. Porting to python 3.x requires that we play less fast and loose with byte/unicode typing so I'm going to presume 'bytes => str (unicode)' for our initial porting then fix required normalizations as we go along.
Test outcome changes as follows...
before: FAILED (failures=5, errors=138, successes=164) after: FAILED (skips=1, failures=3, errors=61, successes=242) --- bridgedb/distributors/email/autoresponder.py | 4 ++-- bridgedb/parse/addr.py | 12 ++++++------ bridgedb/persistent.py | 2 +- bridgedb/proxy.py | 18 +++++++++--------- bridgedb/test/test_bridges.py | 2 +- bridgedb/test/test_captcha.py | 10 +++++----- bridgedb/test/test_crypto.py | 6 +++--- bridgedb/test/test_geo.py | 4 ++-- bridgedb/test/test_parse_addr.py | 4 ++-- bridgedb/test/test_persistent.py | 8 ++++---- bridgedb/test/test_persistentSaveAndLoad.py | 4 ++-- bridgedb/test/test_proxy.py | 4 ++-- bridgedb/test/test_txrecaptcha.py | 2 +- bridgedb/test/test_util.py | 2 +- 14 files changed, 41 insertions(+), 41 deletions(-)
diff --git a/bridgedb/distributors/email/autoresponder.py b/bridgedb/distributors/email/autoresponder.py index e69f78a..b00feeb 100644 --- a/bridgedb/distributors/email/autoresponder.py +++ b/bridgedb/distributors/email/autoresponder.py @@ -291,10 +291,10 @@ class EmailResponse(object): (i.e. ``'\n'``). See :api:`twisted.mail.smtp.SMTPClient.getMailData` for the reason.
- :type lines: :any:`basestring` or :any:`list` + :type lines: :any:`str` or :any:`list` :param lines: The lines to write to the :attr:`mailfile`. """ - if isinstance(lines, basestring): + if isinstance(lines, str): lines = lines.replace('\r\n', '\n') for ln in lines.split('\n'): self.write(ln) diff --git a/bridgedb/parse/addr.py b/bridgedb/parse/addr.py index 90bc1d5..334733d 100644 --- a/bridgedb/parse/addr.py +++ b/bridgedb/parse/addr.py @@ -297,7 +297,7 @@ def extractEmailAddress(emailaddr): def isIPAddress(ip, compressed=True): """Check if an arbitrary string is an IP address, and that it's valid.
- :type ip: basestring or int + :type ip: str or int :param ip: The IP address to check. :param boolean compressed: If True, return a string representing the compressed form of the address. Otherwise, return an @@ -347,7 +347,7 @@ def isIPv4(ip):
.. attention:: This does *not* check validity. See :func:`isValidIP`.
- :type ip: basestring or int + :type ip: str or int :param ip: The IP address to check. :rtype: boolean :returns: True if the address is an IPv4 address. @@ -359,7 +359,7 @@ def isIPv6(ip):
.. attention:: This does *not* check validity. See :func:`isValidIP`.
- :type ip: basestring or int + :type ip: str or int :param ip: The IP address to check. :rtype: boolean :returns: True if the address is an IPv6 address. @@ -407,7 +407,7 @@ def isValidIP(ip): reasons = []
try: - if isinstance(ip, basestring): + if isinstance(ip, str): ip = ipaddr.IPAddress(ip)
if ip.is_link_local: @@ -445,7 +445,7 @@ def isLoopback(ip): otherwise. """ try: - if isinstance(ip, basestring): + if isinstance(ip, str): ip = ipaddr.IPAddress(ip)
if ip.is_loopback: @@ -572,7 +572,7 @@ class PortList(object): for arg in args: portlist = [] try: - if isinstance(arg, basestring): + if isinstance(arg, str): ports = set([int(p) for p in arg.split(',')][:self.PORTSPEC_LEN]) portlist.extend([self._sanitycheck(p) for p in ports]) diff --git a/bridgedb/persistent.py b/bridgedb/persistent.py index c46c4a5..4b077d8 100644 --- a/bridgedb/persistent.py +++ b/bridgedb/persistent.py @@ -173,7 +173,7 @@ class State(jelly.Jellyable): err = ''
try: - if isinstance(statefile, basestring): + if isinstance(statefile, str): fh = open(statefile, 'r') elif not statefile.closed: fh = statefile diff --git a/bridgedb/proxy.py b/bridgedb/proxy.py index 6d93c48..fdb824d 100644 --- a/bridgedb/proxy.py +++ b/bridgedb/proxy.py @@ -161,7 +161,7 @@ class ProxySet(MutableSet): only added if it passes the checks in :func:`~bridgedb.parse.addr.isIPAddress`.
- :type ip: basestring or int + :type ip: str or int :param ip: The IP address to add. :param tag: An optional value to link to **ip**. If not given, it will be a timestamp (seconds since epoch, as a float) for when **ip** @@ -183,7 +183,7 @@ class ProxySet(MutableSet): def __contains__(self, ip): """x.__contains__(y) <==> y in x.
- :type ip: basestring or int + :type ip: str or int :param ip: The IP address to check. :rtype: boolean :returns: True if ``ip`` is in this set; False otherwise. @@ -196,7 +196,7 @@ class ProxySet(MutableSet): def __sub__(self, ip): """Entirely remove **ip** from this set.
- :type ip: basestring or int + :type ip: str or int :param ip: The IP address to remove. """ try: @@ -244,7 +244,7 @@ class ProxySet(MutableSet): if len(x) == 2: self.add(x[0], x[1]) elif len(x) == 1: self.add(x, tag) else: raise ValueError(self._getErrorMessage(x, proxies)) - elif isinstance(x, (basestring, int)): + elif isinstance(x, (str, int)): self.add(x, tag) else: raise ValueError(self._getErrorMessage(x, proxies)) @@ -270,9 +270,9 @@ class ProxySet(MutableSet): def getTag(self, ip): """Get the tag for an **ip** in this ``ProxySet``, if available.
- :type ip: basestring or int + :type ip: str or int :param ip: The IP address to obtain the tag for. - :rtype: ``None`` or basestring or int + :rtype: ``None`` or str or int :returns: The tag for that **ip**, iff **ip** exists in this ``ProxySet`` and it has a tag. """ @@ -281,7 +281,7 @@ class ProxySet(MutableSet): def getAllWithTag(self, tag): """Get all proxies in this ``ProxySet`` with a given tag.
- :param basestring tag: A tag to search for. + :param str tag: A tag to search for. :rtype: set :returns: A set of all proxies which are contained within this :class:`~bridgedb.proxy.ProxySet` which are also tagged with @@ -293,7 +293,7 @@ class ProxySet(MutableSet): def firstSeen(self, ip): """Get the timestamp when **ip** was first seen, if available.
- :type ip: basestring or int + :type ip: str or int :param ip: The IP address to obtain a timestamp for. :rtype: float or None :returns: The timestamp (in seconds since epoch) if available. @@ -306,7 +306,7 @@ class ProxySet(MutableSet): def isExitRelay(self, ip): """Check if ``ip`` is a known Tor exit relay.
- :type ip: basestring or int + :type ip: str or int :param ip: The IP address to check. :rtype: boolean :returns: True if ``ip`` is a known Tor exit relay; False otherwise. diff --git a/bridgedb/test/test_bridges.py b/bridgedb/test/test_bridges.py index 709ae81..cc1680e 100644 --- a/bridgedb/test/test_bridges.py +++ b/bridgedb/test/test_bridges.py @@ -466,7 +466,7 @@ class BridgeAddressBaseTests(unittest.TestCase):
cc = self.bab.country self.assertIsNotNone(cc) - self.assertIsInstance(cc, basestring) + self.assertIsInstance(cc, str) self.assertEqual(len(cc), 2)
diff --git a/bridgedb/test/test_captcha.py b/bridgedb/test/test_captcha.py index 328a03c..76f962e 100644 --- a/bridgedb/test/test_captcha.py +++ b/bridgedb/test/test_captcha.py @@ -77,8 +77,8 @@ class ReCaptchaTests(unittest.TestCase): reason += "connection.\nThis test failed with: %s" % error raise unittest.SkipTest(reason) else: - self.assertIsInstance(self.c.image, basestring) - self.assertIsInstance(self.c.challenge, basestring) + self.assertIsInstance(self.c.image, str) + self.assertIsInstance(self.c.challenge, str) finally: # Replace the original environment variable if there was one: if oldkey: @@ -146,7 +146,7 @@ class GimpCaptchaTests(unittest.TestCase): c = captcha.GimpCaptcha(self.publik, self.sekrit, self.hmacKey, self.cacheDir) challenge = c.createChallenge('w00t') - self.assertIsInstance(challenge, basestring) + self.assertIsInstance(challenge, str)
def test_createChallenge_base64(self): """createChallenge() return value should be urlsafe base64-encoded.""" @@ -189,8 +189,8 @@ class GimpCaptchaTests(unittest.TestCase): c = captcha.GimpCaptcha(self.publik, self.sekrit, self.hmacKey, self.cacheDir) image, challenge = c.get() - self.assertIsInstance(image, basestring) - self.assertIsInstance(challenge, basestring) + self.assertIsInstance(image, str) + self.assertIsInstance(challenge, str)
def test_get_emptyCacheDir(self): """An empty cacheDir should raise GimpCaptchaError.""" diff --git a/bridgedb/test/test_crypto.py b/bridgedb/test/test_crypto.py index 7a34671..000ca0f 100644 --- a/bridgedb/test/test_crypto.py +++ b/bridgedb/test/test_crypto.py @@ -63,14 +63,14 @@ class GetKeyTests(unittest.TestCase): """Test retrieving the secret_key from an empty file.""" filename = os.path.join(os.getcwd(), 'sekrit') key = crypto.getKey(filename) - self.failUnlessIsInstance(key, basestring, + self.failUnlessIsInstance(key, str, "key isn't a string! type=%r" % type(key))
def test_getKey_tmpfile(self): """Test retrieving the secret_key from a new tmpfile.""" filename = self.mktemp() key = crypto.getKey(filename) - self.failUnlessIsInstance(key, basestring, + self.failUnlessIsInstance(key, str, "key isn't a string! type=%r" % type(key))
def test_getKey_keyexists(self): @@ -81,7 +81,7 @@ class GetKeyTests(unittest.TestCase): fh.flush()
key = crypto.getKey(filename) - self.failUnlessIsInstance(key, basestring, + self.failUnlessIsInstance(key, str, "key isn't a string! type=%r" % type(key)) self.assertEqual(SEKRIT_KEY, key, """The example key and the one read from file differ! diff --git a/bridgedb/test/test_geo.py b/bridgedb/test/test_geo.py index 4ca6cbb..371882c 100644 --- a/bridgedb/test/test_geo.py +++ b/bridgedb/test/test_geo.py @@ -50,7 +50,7 @@ class GeoTests(unittest.TestCase): """Should return the CC since the IP is an ``ipaddr.IPAddress``.""" cc = geo.getCountryCode(self.ipv4) self.assertIsNotNone(cc) - self.assertIsInstance(cc, basestring) + self.assertIsInstance(cc, str) self.assertEqual(len(cc), 2) self.assertEqual(cc, self.expectedCC)
@@ -77,7 +77,7 @@ class GeoTests(unittest.TestCase): """Should return the CC since the IP is an ``ipaddr.IPAddress``.""" cc = geo.getCountryCode(self.ipv6) self.assertIsNotNone(cc) - self.assertIsInstance(cc, basestring) + self.assertIsInstance(cc, str) self.assertEqual(len(cc), 2) self.assertEqual(cc, self.expectedCC)
diff --git a/bridgedb/test/test_parse_addr.py b/bridgedb/test/test_parse_addr.py index 4659efb..81e4508 100644 --- a/bridgedb/test/test_parse_addr.py +++ b/bridgedb/test/test_parse_addr.py @@ -226,7 +226,7 @@ class ParseAddrIsIPAddressTests(unittest.TestCase): log.msg("addr.isIPAddress(%r) => %s" % (testAddress, result)) self.assertTrue(result is not None, "Got a None for testAddress: %r" % testAddress) - self.assertFalse(isinstance(result, basestring), + self.assertFalse(isinstance(result, str), "Expected %r result from isIPAddress(%r): %r %r" % (bool, testAddress, result, type(result)))
@@ -709,7 +709,7 @@ class PortListTest(unittest.TestCase): """ ports = (443, 9001, 9030) portList = addr.PortList(*ports) - self.assertTrue(isinstance(str(portList), basestring)) + self.assertTrue(isinstance(str(portList), str)) for port in ports: self.assertIn(str(port), str(portList))
diff --git a/bridgedb/test/test_persistent.py b/bridgedb/test/test_persistent.py index 858ad1a..6547475 100644 --- a/bridgedb/test/test_persistent.py +++ b/bridgedb/test/test_persistent.py @@ -142,8 +142,8 @@ class StateTest(unittest.TestCase): setattr(thatConfig, 'BAR', 'all of the things') setattr(thatConfig, 'LOGFILE', 42)
- this(thatConfig).should.have.property('FOO').being.a(basestring) - this(thatConfig).should.have.property('BAR').being.a(basestring) + this(thatConfig).should.have.property('FOO').being.a(str) + this(thatConfig).should.have.property('BAR').being.a(str) this(thatConfig).should.have.property('LOGFILE').being.an(int) this(thatConfig).should.have.property('BRIDGE_FILES').being.a(list)
@@ -156,8 +156,8 @@ class StateTest(unittest.TestCase): thatState.useChangedSettings(thatConfig)
the(thatState.FOO).should.equal('fuuuuu') - the(thatState).should.have.property('FOO').being.a(basestring) - the(thatState).should.have.property('BAR').being.a(basestring) + the(thatState).should.have.property('FOO').being.a(str) + the(thatState).should.have.property('BAR').being.a(str) the(thatState).should.have.property('LOGFILE').being.an(int) the(thatState.FOO).must.equal(thatConfig.FOO) the(thatState.BAR).must.equal(thatConfig.BAR) diff --git a/bridgedb/test/test_persistentSaveAndLoad.py b/bridgedb/test/test_persistentSaveAndLoad.py index 6d6584d..18f601e 100644 --- a/bridgedb/test/test_persistentSaveAndLoad.py +++ b/bridgedb/test/test_persistentSaveAndLoad.py @@ -85,12 +85,12 @@ class StateSaveAndLoadTests(unittest.TestCase):
def test_get_statefile(self): statefile = self.state._get_statefile() - self.assertIsInstance(statefile, basestring) + self.assertIsInstance(statefile, str)
def test_set_statefile(self): self.state._set_statefile('bar.state') statefile = self.state._get_statefile() - self.assertIsInstance(statefile, basestring) + self.assertIsInstance(statefile, str)
def test_set_statefile_new_dir(self): config = self.config diff --git a/bridgedb/test/test_proxy.py b/bridgedb/test/test_proxy.py index 0a69eb0..c55e03c 100644 --- a/bridgedb/test/test_proxy.py +++ b/bridgedb/test/test_proxy.py @@ -393,13 +393,13 @@ class ProxySetUnittests(unittest.TestCase):
def test_ProxySet_addProxies_bad_type(self): """``ProxySet.addProxies()`` called with something which is neither an - iterable, a basestring, or an int should raise a ValueError. + iterable, a str, or an int should raise a ValueError. """ self.assertRaises(ValueError, self.proxyList.addProxies, object)
def test_ProxySet_addProxies_list_of_bad_types(self): """``ProxySet.addProxies()`` called with something which is neither an - iterable, a basestring, or an int should raise a ValueError. + iterable, a str, or an int should raise a ValueError. """ self.assertRaises(ValueError, self.proxyList.addProxies, [object, object, object])
diff --git a/bridgedb/test/test_txrecaptcha.py b/bridgedb/test/test_txrecaptcha.py index 2550c3d..22505b9 100644 --- a/bridgedb/test/test_txrecaptcha.py +++ b/bridgedb/test/test_txrecaptcha.py @@ -220,7 +220,7 @@ class SubmitTests(unittest.TestCase): """ self.assertIsInstance(response, txrecaptcha.RecaptchaResponse) self.assertIsInstance(response.is_valid, bool) - self.assertIsInstance(response.error_code, basestring) + self.assertIsInstance(response.error_code, str)
d = txrecaptcha.submit(self.challenge, self.response, self.key, self.ip) diff --git a/bridgedb/test/test_util.py b/bridgedb/test/test_util.py index e858861..55ce35f 100644 --- a/bridgedb/test/test_util.py +++ b/bridgedb/test/test_util.py @@ -169,7 +169,7 @@ class JustifiedLogFormatterTests(unittest.TestCase): formatter = util.JustifiedLogFormatter() formatted = formatter.format(self.record) self.assertIsInstance(formatter, logging.Formatter) - self.assertIsInstance(formatted, basestring) + self.assertIsInstance(formatted, str) self.assertNotEqual(formatted, '') self.assertTrue('INFO' in formatted) self.assertTrue('This is a message' in formatted)
tor-commits@lists.torproject.org