From 6dad561d923c3925c48fdd60d5e45a2fb1a7a359 Mon Sep 17 00:00:00 2001 From: Dan Callaghan Date: Sun, 26 Aug 2018 20:52:19 +1000 Subject: [PATCH 01/17] use repr() instead of backticks --- pyutil/test/out_of_shape/test_cache.py | 4 ++-- pyutil/test/out_of_shape/test_odict.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pyutil/test/out_of_shape/test_cache.py b/pyutil/test/out_of_shape/test_cache.py index 2bd90a3..07fa9f2 100644 --- a/pyutil/test/out_of_shape/test_cache.py +++ b/pyutil/test/out_of_shape/test_cache.py @@ -140,7 +140,7 @@ def _test_insert_and_remove(self, d): self.failUnless(d.get('spam') == "eggs") self.failUnless(d['spam'] == "eggs") x = d.remove('spam') - self.failUnless(x == "eggs", "x: %s" % `x`) + self.failUnless(x == "eggs", "x: %r" % x) self.failUnless(not d.has_key('spam')) d['spam'] = "eggs" self.failUnless(d.has_key('spam')) @@ -155,7 +155,7 @@ def _test_setdefault(self, d): self.failUnless(d.get('spam') == "eggs") self.failUnless(d['spam'] == "eggs") x = d.remove('spam') - self.failUnless(x == "eggs", "x: %s" % `x`) + self.failUnless(x == "eggs", "x: %r" % x) self.failUnless(not d.has_key('spam')) def _test_extracted_bound_method(self, d): diff --git a/pyutil/test/out_of_shape/test_odict.py b/pyutil/test/out_of_shape/test_odict.py index 88ada6a..c3ebd27 100644 --- a/pyutil/test/out_of_shape/test_odict.py +++ b/pyutil/test/out_of_shape/test_odict.py @@ -142,7 +142,7 @@ def _test_insert_and_remove(self, d): self.failUnless(d['spam'] == "eggs") self.failUnlessEqual(d.items(), [("spam", "eggs")]) x = d.remove('spam') - self.failUnless(x == "eggs", "x: %s" % `x`) + self.failUnless(x == "eggs", "x: %r" % x) self.failUnless(not d.has_key('spam')) self.failUnlessEqual(d.items(), []) d['spam'] = "eggsy" @@ -161,7 +161,7 @@ def _test_setdefault(self, d): self.failUnless(d['spam'] == "eggs") self.failUnlessEqual(d.items(), [("spam", "eggs")]) x = d.remove('spam') - self.failUnless(x == "eggs", "x: %s" % `x`) + self.failUnless(x == "eggs", "x: %r" % x) self.failUnless(not d.has_key('spam')) self.failUnlessEqual(d.items(), []) From 2ad9b14bd18ae4693c54cbcaad45ef96514e42b8 Mon Sep 17 00:00:00 2001 From: Dan Callaghan Date: Sun, 26 Aug 2018 20:52:48 +1000 Subject: [PATCH 02/17] assertEquals -> assertEqual --- pyutil/test/current/json_tests/test_decode.py | 4 +- .../test/current/json_tests/test_default.py | 2 +- pyutil/test/current/json_tests/test_dump.py | 4 +- .../test_encode_basestring_ascii.py | 2 +- pyutil/test/current/json_tests/test_float.py | 2 +- pyutil/test/current/json_tests/test_indent.py | 6 +-- pyutil/test/current/json_tests/test_pass1.py | 2 +- pyutil/test/current/json_tests/test_pass2.py | 2 +- pyutil/test/current/json_tests/test_pass3.py | 2 +- .../test/current/json_tests/test_recursion.py | 2 +- .../current/json_tests/test_separators.py | 6 +-- .../test/current/json_tests/test_speedups.py | 4 +- .../test/current/json_tests/test_unicode.py | 22 +++++------ pyutil/test/current/test_verlib.py | 38 +++++++++---------- 14 files changed, 49 insertions(+), 49 deletions(-) diff --git a/pyutil/test/current/json_tests/test_decode.py b/pyutil/test/current/json_tests/test_decode.py index 020382c..ca9a132 100644 --- a/pyutil/test/current/json_tests/test_decode.py +++ b/pyutil/test/current/json_tests/test_decode.py @@ -9,9 +9,9 @@ class TestDecode(TestCase): def test_decimal(self): rval = json.loads('1.1', parse_float=decimal.Decimal) self.assert_(isinstance(rval, decimal.Decimal)) - self.assertEquals(rval, decimal.Decimal('1.1')) + self.assertEqual(rval, decimal.Decimal('1.1')) def test_float(self): rval = json.loads('1', parse_int=float) self.assert_(isinstance(rval, float)) - self.assertEquals(rval, 1.0) + self.assertEqual(rval, 1.0) diff --git a/pyutil/test/current/json_tests/test_default.py b/pyutil/test/current/json_tests/test_default.py index f18cb02..3a13f3a 100644 --- a/pyutil/test/current/json_tests/test_default.py +++ b/pyutil/test/current/json_tests/test_default.py @@ -6,6 +6,6 @@ class TestDefault(TestCase): def test_default(self): - self.assertEquals( + self.assertEqual( json.dumps(type, default=repr), json.dumps(repr(type))) diff --git a/pyutil/test/current/json_tests/test_dump.py b/pyutil/test/current/json_tests/test_dump.py index f328a34..b153bbf 100644 --- a/pyutil/test/current/json_tests/test_dump.py +++ b/pyutil/test/current/json_tests/test_dump.py @@ -12,7 +12,7 @@ class TestDump(TestCase): def test_dump(self): sio = StringIO() json.dump({}, sio) - self.assertEquals(sio.getvalue(), '{}') + self.assertEqual(sio.getvalue(), '{}') def test_dumps(self): - self.assertEquals(json.dumps({}), '{}') + self.assertEqual(json.dumps({}), '{}') diff --git a/pyutil/test/current/json_tests/test_encode_basestring_ascii.py b/pyutil/test/current/json_tests/test_encode_basestring_ascii.py index d526092..aa1e1f2 100644 --- a/pyutil/test/current/json_tests/test_encode_basestring_ascii.py +++ b/pyutil/test/current/json_tests/test_encode_basestring_ascii.py @@ -37,4 +37,4 @@ def test_c_encode_basestring_ascii(self): def _test_encode_basestring_ascii(self, encode_basestring_ascii): for input_string, expect in CASES: result = encode_basestring_ascii(input_string) - self.assertEquals(result, expect) + self.assertEqual(result, expect) diff --git a/pyutil/test/current/json_tests/test_float.py b/pyutil/test/current/json_tests/test_float.py index 13aa972..22e3337 100644 --- a/pyutil/test/current/json_tests/test_float.py +++ b/pyutil/test/current/json_tests/test_float.py @@ -8,4 +8,4 @@ class TestFloat(TestCase): def test_floats(self): for num in [1617161771.7650001, math.pi, math.pi**100, math.pi**-100]: - self.assertEquals(float(json.dumps(num)), num) + self.assertEqual(float(json.dumps(num)), num) diff --git a/pyutil/test/current/json_tests/test_indent.py b/pyutil/test/current/json_tests/test_indent.py index 93c7f2d..e3258e3 100644 --- a/pyutil/test/current/json_tests/test_indent.py +++ b/pyutil/test/current/json_tests/test_indent.py @@ -38,6 +38,6 @@ def test_indent(self): h1 = json.loads(d1) h2 = json.loads(d2) - self.assertEquals(h1, h) - self.assertEquals(h2, h) - self.assertEquals(d2, expect) + self.assertEqual(h1, h) + self.assertEqual(h2, h) + self.assertEqual(d2, expect) diff --git a/pyutil/test/current/json_tests/test_pass1.py b/pyutil/test/current/json_tests/test_pass1.py index bcca9a3..d1897e9 100644 --- a/pyutil/test/current/json_tests/test_pass1.py +++ b/pyutil/test/current/json_tests/test_pass1.py @@ -69,5 +69,5 @@ def test_parse(self): # test in/out equivalence and parsing res = json.loads(JSON) out = json.dumps(res) - self.assertEquals(res, json.loads(out)) + self.assertEqual(res, json.loads(out)) self.failUnless("2.3456789012E+676" in json.dumps(res, allow_nan=False)) diff --git a/pyutil/test/current/json_tests/test_pass2.py b/pyutil/test/current/json_tests/test_pass2.py index 53f6978..68d2b0b 100644 --- a/pyutil/test/current/json_tests/test_pass2.py +++ b/pyutil/test/current/json_tests/test_pass2.py @@ -13,4 +13,4 @@ def test_parse(self): # test in/out equivalence and parsing res = json.loads(JSON) out = json.dumps(res) - self.assertEquals(res, json.loads(out)) + self.assertEqual(res, json.loads(out)) diff --git a/pyutil/test/current/json_tests/test_pass3.py b/pyutil/test/current/json_tests/test_pass3.py index 9224328..426fd66 100644 --- a/pyutil/test/current/json_tests/test_pass3.py +++ b/pyutil/test/current/json_tests/test_pass3.py @@ -19,4 +19,4 @@ def test_parse(self): # test in/out equivalence and parsing res = json.loads(JSON) out = json.dumps(res) - self.assertEquals(res, json.loads(out)) + self.assertEqual(res, json.loads(out)) diff --git a/pyutil/test/current/json_tests/test_recursion.py b/pyutil/test/current/json_tests/test_recursion.py index 134a911..0790b13 100644 --- a/pyutil/test/current/json_tests/test_recursion.py +++ b/pyutil/test/current/json_tests/test_recursion.py @@ -59,7 +59,7 @@ def test_dictrecursion(self): def test_defaultrecursion(self): enc = RecursiveJSONEncoder() - self.assertEquals(enc.encode(JSONTestObject), '"JSONTestObject"') + self.assertEqual(enc.encode(JSONTestObject), '"JSONTestObject"') enc.recurse = True try: enc.encode(JSONTestObject) diff --git a/pyutil/test/current/json_tests/test_separators.py b/pyutil/test/current/json_tests/test_separators.py index 3990d8d..7c578a0 100644 --- a/pyutil/test/current/json_tests/test_separators.py +++ b/pyutil/test/current/json_tests/test_separators.py @@ -39,6 +39,6 @@ def test_separators(self): h1 = json.loads(d1) h2 = json.loads(d2) - self.assertEquals(h1, h) - self.assertEquals(h2, h) - self.assertEquals(d2, expect) + self.assertEqual(h1, h) + self.assertEqual(h2, h) + self.assertEqual(d2, expect) diff --git a/pyutil/test/current/json_tests/test_speedups.py b/pyutil/test/current/json_tests/test_speedups.py index 002725b..e6ab2fd 100644 --- a/pyutil/test/current/json_tests/test_speedups.py +++ b/pyutil/test/current/json_tests/test_speedups.py @@ -9,12 +9,12 @@ class TestSpeedups(TestCase): def test_scanstring(self): if not encoder.c_encode_basestring_ascii: raise SkipTest("no C extension speedups available to test") - self.assertEquals(decoder.scanstring.__module__, "simplejson._speedups") + self.assertEqual(decoder.scanstring.__module__, "simplejson._speedups") self.assert_(decoder.scanstring is decoder.c_scanstring) def test_encode_basestring_ascii(self): if not encoder.c_encode_basestring_ascii: raise SkipTest("no C extension speedups available to test") - self.assertEquals(encoder.encode_basestring_ascii.__module__, "simplejson._speedups") + self.assertEqual(encoder.encode_basestring_ascii.__module__, "simplejson._speedups") self.assert_(encoder.encode_basestring_ascii is encoder.c_encode_basestring_ascii) diff --git a/pyutil/test/current/json_tests/test_unicode.py b/pyutil/test/current/json_tests/test_unicode.py index 7354ca2..4c42ca0 100644 --- a/pyutil/test/current/json_tests/test_unicode.py +++ b/pyutil/test/current/json_tests/test_unicode.py @@ -16,47 +16,47 @@ def test_encoding1(self): s = u.encode('utf-8') ju = encoder.encode(u) js = encoder.encode(s) - self.assertEquals(ju, js) + self.assertEqual(ju, js) def test_encoding2(self): u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}' s = u.encode('utf-8') ju = json.dumps(u, encoding='utf-8') js = json.dumps(s, encoding='utf-8') - self.assertEquals(ju, js) + self.assertEqual(ju, js) def test_encoding3(self): u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}' j = json.dumps(u) - self.assertEquals(j, '"\\u03b1\\u03a9"') + self.assertEqual(j, '"\\u03b1\\u03a9"') def test_encoding4(self): u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}' j = json.dumps([u]) - self.assertEquals(j, '["\\u03b1\\u03a9"]') + self.assertEqual(j, '["\\u03b1\\u03a9"]') def test_encoding5(self): u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}' j = json.dumps(u, ensure_ascii=False) - self.assertEquals(j, u'"%s"' % (u,)) + self.assertEqual(j, u'"%s"' % (u,)) def test_encoding6(self): u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}' j = json.dumps([u], ensure_ascii=False) - self.assertEquals(j, u'["%s"]' % (u,)) + self.assertEqual(j, u'["%s"]' % (u,)) def test_big_unicode_encode(self): u = u'\U0001d120' - self.assertEquals(json.dumps(u), '"\\ud834\\udd20"') - self.assertEquals(json.dumps(u, ensure_ascii=False), u'"\U0001d120"') + self.assertEqual(json.dumps(u), '"\\ud834\\udd20"') + self.assertEqual(json.dumps(u, ensure_ascii=False), u'"\U0001d120"') def test_big_unicode_decode(self): u = u'z\U0001d120x' - self.assertEquals(json.loads('"' + u + '"'), u) - self.assertEquals(json.loads('"z\\ud834\\udd20x"'), u) + self.assertEqual(json.loads('"' + u + '"'), u) + self.assertEqual(json.loads('"z\\ud834\\udd20x"'), u) def test_unicode_decode(self): for i in range(0, 0xd7ff): u = unichr(i) js = '"\\u%04x"' % (i,) - self.assertEquals(json.loads(js), u) + self.assertEqual(json.loads(js), u) diff --git a/pyutil/test/current/test_verlib.py b/pyutil/test/current/test_verlib.py index 375c414..bb03b49 100644 --- a/pyutil/test/current/test_verlib.py +++ b/pyutil/test/current/test_verlib.py @@ -28,14 +28,14 @@ class VersionTestCase(unittest.TestCase): def test_basic_versions(self): for v, s in self.versions: - self.assertEquals(str(v), s) + self.assertEqual(str(v), s) def test_from_parts(self): for v, s in self.versions: v2 = V.from_parts(*v.parts) - self.assertEquals(v, v2) - self.assertEquals(str(v), str(v2)) + self.assertEqual(v, v2) + self.assertEqual(str(v), str(v2)) def test_irrational_versions(self): @@ -98,31 +98,31 @@ def test_comparison(self): def test_suggest_normalized_version(self): - self.assertEquals(suggest('1.0'), '1.0') - self.assertEquals(suggest('1.0-alpha1'), '1.0a1') - self.assertEquals(suggest('1.0c2'), '1.0c2') - self.assertEquals(suggest('walla walla washington'), None) - self.assertEquals(suggest('2.4c1'), '2.4c1') + self.assertEqual(suggest('1.0'), '1.0') + self.assertEqual(suggest('1.0-alpha1'), '1.0a1') + self.assertEqual(suggest('1.0c2'), '1.0c2') + self.assertEqual(suggest('walla walla washington'), None) + self.assertEqual(suggest('2.4c1'), '2.4c1') # from setuptools - self.assertEquals(suggest('0.4a1.r10'), '0.4a1.post10') - self.assertEquals(suggest('0.7a1dev-r66608'), '0.7a1.dev66608') - self.assertEquals(suggest('0.6a9.dev-r41475'), '0.6a9.dev41475') - self.assertEquals(suggest('2.4preview1'), '2.4c1') - self.assertEquals(suggest('2.4pre1') , '2.4c1') - self.assertEquals(suggest('2.1-rc2'), '2.1c2') + self.assertEqual(suggest('0.4a1.r10'), '0.4a1.post10') + self.assertEqual(suggest('0.7a1dev-r66608'), '0.7a1.dev66608') + self.assertEqual(suggest('0.6a9.dev-r41475'), '0.6a9.dev41475') + self.assertEqual(suggest('2.4preview1'), '2.4c1') + self.assertEqual(suggest('2.4pre1') , '2.4c1') + self.assertEqual(suggest('2.1-rc2'), '2.1c2') # from pypi - self.assertEquals(suggest('0.1dev'), '0.1.dev0') - self.assertEquals(suggest('0.1.dev'), '0.1.dev0') + self.assertEqual(suggest('0.1dev'), '0.1.dev0') + self.assertEqual(suggest('0.1.dev'), '0.1.dev0') # we want to be able to parse Twisted # development versions are like post releases in Twisted - self.assertEquals(suggest('9.0.0+r2363'), '9.0.0.post2363') + self.assertEqual(suggest('9.0.0+r2363'), '9.0.0.post2363') # pre-releases are using markers like "pre1" - self.assertEquals(suggest('9.0.0pre1'), '9.0.0c1') + self.assertEqual(suggest('9.0.0pre1'), '9.0.0c1') # we want to be able to parse Tcl-TK # they us "p1" "p2" for post releases - self.assertEquals(suggest('1.4p1'), '1.4.post1') + self.assertEqual(suggest('1.4p1'), '1.4.post1') From 16497b5686bed5d0f21618b8d46ea6ce7da17ee1 Mon Sep 17 00:00:00 2001 From: Dan Callaghan Date: Sun, 26 Aug 2018 20:52:59 +1000 Subject: [PATCH 03/17] failUnlessEqual -> assertEqual --- pyutil/test/current/test_fileutil.py | 2 +- pyutil/test/current/test_jsonutil.py | 6 +- pyutil/test/current/test_mathutil.py | 168 +++++++++++----------- pyutil/test/current/test_observer.py | 22 +-- pyutil/test/current/test_time_format.py | 24 ++-- pyutil/test/current/test_version_class.py | 6 +- pyutil/test/out_of_shape/test_odict.py | 42 +++--- 7 files changed, 135 insertions(+), 135 deletions(-) diff --git a/pyutil/test/current/test_fileutil.py b/pyutil/test/current/test_fileutil.py index ce007d5..158951c 100644 --- a/pyutil/test/current/test_fileutil.py +++ b/pyutil/test/current/test_fileutil.py @@ -34,4 +34,4 @@ def test_du(self): self.touch(d, "a/c/2.txt", data="d"*13) used = fileutil.du(basedir) - self.failUnlessEqual(10+11+12+13, used) + self.assertEqual(10+11+12+13, used) diff --git a/pyutil/test/current/test_jsonutil.py b/pyutil/test/current/test_jsonutil.py index 121a5b7..a35964f 100644 --- a/pyutil/test/current/test_jsonutil.py +++ b/pyutil/test/current/test_jsonutil.py @@ -13,10 +13,10 @@ zero_point_one = Decimal("0.1") class TestDecimal(unittest.TestCase): def test_encode(self): - self.failUnlessEqual(jsonutil.dumps(zero_point_one), "0.1") + self.assertEqual(jsonutil.dumps(zero_point_one), "0.1") def test_decode(self): - self.failUnlessEqual(jsonutil.loads("0.1"), zero_point_one) + self.assertEqual(jsonutil.loads("0.1"), zero_point_one) def test_no_exception_on_convergent_parse_float(self): - self.failUnlessEqual(jsonutil.loads("0.1", parse_float=Decimal), zero_point_one) + self.assertEqual(jsonutil.loads("0.1", parse_float=Decimal), zero_point_one) diff --git a/pyutil/test/current/test_mathutil.py b/pyutil/test/current/test_mathutil.py index 47eadad..e544de0 100644 --- a/pyutil/test/current/test_mathutil.py +++ b/pyutil/test/current/test_mathutil.py @@ -20,76 +20,76 @@ def test_is_power_of_k(self): def test_log_ceil(self): f = mathutil.log_ceil - self.failUnlessEqual(f(1, 2), 0) - self.failUnlessEqual(f(1, 3), 0) - self.failUnlessEqual(f(2, 2), 1) - self.failUnlessEqual(f(2, 3), 1) - self.failUnlessEqual(f(3, 2), 2) + self.assertEqual(f(1, 2), 0) + self.assertEqual(f(1, 3), 0) + self.assertEqual(f(2, 2), 1) + self.assertEqual(f(2, 3), 1) + self.assertEqual(f(3, 2), 2) def test_log_floor(self): f = mathutil.log_floor - self.failUnlessEqual(f(1, 2), 0) - self.failUnlessEqual(f(1, 3), 0) - self.failUnlessEqual(f(2, 2), 1) - self.failUnlessEqual(f(2, 3), 0) - self.failUnlessEqual(f(3, 2), 1) + self.assertEqual(f(1, 2), 0) + self.assertEqual(f(1, 3), 0) + self.assertEqual(f(2, 2), 1) + self.assertEqual(f(2, 3), 0) + self.assertEqual(f(3, 2), 1) def test_div_ceil(self): f = mathutil.div_ceil - self.failUnlessEqual(f(0, 1), 0) - self.failUnlessEqual(f(0, 2), 0) - self.failUnlessEqual(f(0, 3), 0) - self.failUnlessEqual(f(1, 3), 1) - self.failUnlessEqual(f(2, 3), 1) - self.failUnlessEqual(f(3, 3), 1) - self.failUnlessEqual(f(4, 3), 2) - self.failUnlessEqual(f(5, 3), 2) - self.failUnlessEqual(f(6, 3), 2) - self.failUnlessEqual(f(7, 3), 3) + self.assertEqual(f(0, 1), 0) + self.assertEqual(f(0, 2), 0) + self.assertEqual(f(0, 3), 0) + self.assertEqual(f(1, 3), 1) + self.assertEqual(f(2, 3), 1) + self.assertEqual(f(3, 3), 1) + self.assertEqual(f(4, 3), 2) + self.assertEqual(f(5, 3), 2) + self.assertEqual(f(6, 3), 2) + self.assertEqual(f(7, 3), 3) self.failUnless(isinstance(f(0.0, 1), int)) - self.failUnlessEqual(f(7.0, 3.0), 3) - self.failUnlessEqual(f(7, 3.0), 3) - self.failUnlessEqual(f(7.0, 3), 3) - self.failUnlessEqual(f(6.0, 3.0), 2) - self.failUnlessEqual(f(6.0, 3), 2) - self.failUnlessEqual(f(6, 3.0), 2) + self.assertEqual(f(7.0, 3.0), 3) + self.assertEqual(f(7, 3.0), 3) + self.assertEqual(f(7.0, 3), 3) + self.assertEqual(f(6.0, 3.0), 2) + self.assertEqual(f(6.0, 3), 2) + self.assertEqual(f(6, 3.0), 2) def test_next_multiple(self): f = mathutil.next_multiple - self.failUnlessEqual(f(5, 1), 5) - self.failUnlessEqual(f(5, 2), 6) - self.failUnlessEqual(f(5, 3), 6) - self.failUnlessEqual(f(5, 4), 8) - self.failUnlessEqual(f(5, 5), 5) - self.failUnlessEqual(f(5, 6), 6) - self.failUnlessEqual(f(32, 1), 32) - self.failUnlessEqual(f(32, 2), 32) - self.failUnlessEqual(f(32, 3), 33) - self.failUnlessEqual(f(32, 4), 32) - self.failUnlessEqual(f(32, 5), 35) - self.failUnlessEqual(f(32, 6), 36) - self.failUnlessEqual(f(32, 7), 35) - self.failUnlessEqual(f(32, 8), 32) - self.failUnlessEqual(f(32, 9), 36) - self.failUnlessEqual(f(32, 10), 40) - self.failUnlessEqual(f(32, 11), 33) - self.failUnlessEqual(f(32, 12), 36) - self.failUnlessEqual(f(32, 13), 39) - self.failUnlessEqual(f(32, 14), 42) - self.failUnlessEqual(f(32, 15), 45) - self.failUnlessEqual(f(32, 16), 32) - self.failUnlessEqual(f(32, 17), 34) - self.failUnlessEqual(f(32, 18), 36) - self.failUnlessEqual(f(32, 589), 589) + self.assertEqual(f(5, 1), 5) + self.assertEqual(f(5, 2), 6) + self.assertEqual(f(5, 3), 6) + self.assertEqual(f(5, 4), 8) + self.assertEqual(f(5, 5), 5) + self.assertEqual(f(5, 6), 6) + self.assertEqual(f(32, 1), 32) + self.assertEqual(f(32, 2), 32) + self.assertEqual(f(32, 3), 33) + self.assertEqual(f(32, 4), 32) + self.assertEqual(f(32, 5), 35) + self.assertEqual(f(32, 6), 36) + self.assertEqual(f(32, 7), 35) + self.assertEqual(f(32, 8), 32) + self.assertEqual(f(32, 9), 36) + self.assertEqual(f(32, 10), 40) + self.assertEqual(f(32, 11), 33) + self.assertEqual(f(32, 12), 36) + self.assertEqual(f(32, 13), 39) + self.assertEqual(f(32, 14), 42) + self.assertEqual(f(32, 15), 45) + self.assertEqual(f(32, 16), 32) + self.assertEqual(f(32, 17), 34) + self.assertEqual(f(32, 18), 36) + self.assertEqual(f(32, 589), 589) def test_pad_size(self): f = mathutil.pad_size - self.failUnlessEqual(f(0, 4), 0) - self.failUnlessEqual(f(1, 4), 3) - self.failUnlessEqual(f(2, 4), 2) - self.failUnlessEqual(f(3, 4), 1) - self.failUnlessEqual(f(4, 4), 0) - self.failUnlessEqual(f(5, 4), 3) + self.assertEqual(f(0, 4), 0) + self.assertEqual(f(1, 4), 3) + self.assertEqual(f(2, 4), 2) + self.assertEqual(f(3, 4), 1) + self.assertEqual(f(4, 4), 0) + self.assertEqual(f(5, 4), 3) def test_is_power_of_k_part_2(self): f = mathutil.is_power_of_k @@ -106,41 +106,41 @@ def test_is_power_of_k_part_2(self): def test_next_power_of_k(self): f = mathutil.next_power_of_k - self.failUnlessEqual(f(0,2), 1) - self.failUnlessEqual(f(1,2), 1) - self.failUnlessEqual(f(2,2), 2) - self.failUnlessEqual(f(3,2), 4) - self.failUnlessEqual(f(4,2), 4) - for i in range(5, 8): self.failUnlessEqual(f(i,2), 8, "%d" % i) - for i in range(9, 16): self.failUnlessEqual(f(i,2), 16, "%d" % i) - for i in range(17, 32): self.failUnlessEqual(f(i,2), 32, "%d" % i) - for i in range(33, 64): self.failUnlessEqual(f(i,2), 64, "%d" % i) - for i in range(65, 100): self.failUnlessEqual(f(i,2), 128, "%d" % i) - - self.failUnlessEqual(f(0,3), 1) - self.failUnlessEqual(f(1,3), 1) - self.failUnlessEqual(f(2,3), 3) - self.failUnlessEqual(f(3,3), 3) - for i in range(4, 9): self.failUnlessEqual(f(i,3), 9, "%d" % i) - for i in range(10, 27): self.failUnlessEqual(f(i,3), 27, "%d" % i) - for i in range(28, 81): self.failUnlessEqual(f(i,3), 81, "%d" % i) - for i in range(82, 200): self.failUnlessEqual(f(i,3), 243, "%d" % i) + self.assertEqual(f(0,2), 1) + self.assertEqual(f(1,2), 1) + self.assertEqual(f(2,2), 2) + self.assertEqual(f(3,2), 4) + self.assertEqual(f(4,2), 4) + for i in range(5, 8): self.assertEqual(f(i,2), 8, "%d" % i) + for i in range(9, 16): self.assertEqual(f(i,2), 16, "%d" % i) + for i in range(17, 32): self.assertEqual(f(i,2), 32, "%d" % i) + for i in range(33, 64): self.assertEqual(f(i,2), 64, "%d" % i) + for i in range(65, 100): self.assertEqual(f(i,2), 128, "%d" % i) + + self.assertEqual(f(0,3), 1) + self.assertEqual(f(1,3), 1) + self.assertEqual(f(2,3), 3) + self.assertEqual(f(3,3), 3) + for i in range(4, 9): self.assertEqual(f(i,3), 9, "%d" % i) + for i in range(10, 27): self.assertEqual(f(i,3), 27, "%d" % i) + for i in range(28, 81): self.assertEqual(f(i,3), 81, "%d" % i) + for i in range(82, 200): self.assertEqual(f(i,3), 243, "%d" % i) def test_ave(self): f = mathutil.ave - self.failUnlessEqual(f([1,2,3]), 2) - self.failUnlessEqual(f([0,0,0,4]), 1) + self.assertEqual(f([1,2,3]), 2) + self.assertEqual(f([0,0,0,4]), 1) self.failUnlessAlmostEqual(f([0.0, 1.0, 1.0]), .666666666666) - def failUnlessEqualContents(self, a, b): - self.failUnlessEqual(sorted(a), sorted(b)) + def assertEqualContents(self, a, b): + self.assertEqual(sorted(a), sorted(b)) def test_permute(self): f = mathutil.permute - self.failUnlessEqualContents(f([]), []) - self.failUnlessEqualContents(f([1]), [[1]]) - self.failUnlessEqualContents(f([1,2]), [[1,2], [2,1]]) - self.failUnlessEqualContents(f([1,2,3]), + self.assertEqualContents(f([]), []) + self.assertEqualContents(f([1]), [[1]]) + self.assertEqualContents(f([1,2]), [[1,2], [2,1]]) + self.assertEqualContents(f([1,2,3]), [[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]]) diff --git a/pyutil/test/current/test_observer.py b/pyutil/test/current/test_observer.py index aeb5c64..84d399f 100644 --- a/pyutil/test/current/test_observer.py +++ b/pyutil/test/current/test_observer.py @@ -16,18 +16,18 @@ class Observer(unittest.TestCase): def test_oneshot(self): ol = observer.OneShotObserverList() rep = repr(ol) - self.failUnlessEqual(rep, "") + self.assertEqual(rep, "") d1 = ol.when_fired() d2 = ol.when_fired() def _addmore(res): - self.failUnlessEqual(res, "result") + self.assertEqual(res, "result") d3 = ol.when_fired() - d3.addCallback(self.failUnlessEqual, "result") + d3.addCallback(self.assertEqual, "result") return d3 d1.addCallback(_addmore) ol.fire("result") rep = repr(ol) - self.failUnlessEqual(rep, " result>") + self.assertEqual(rep, " result>") d4 = ol.when_fired() dl = defer.DeferredList([d1,d2,d4]) return dl @@ -36,10 +36,10 @@ def test_oneshot_fireagain(self): ol = observer.OneShotObserverList() d = ol.when_fired() def _addmore(res): - self.failUnlessEqual(res, "result") + self.assertEqual(res, "result") ol.fire_if_not_fired("result3") # should be ignored d2 = ol.when_fired() - d2.addCallback(self.failUnlessEqual, "result") + d2.addCallback(self.assertEqual, "result") return d2 d.addCallback(_addmore) ol.fire_if_not_fired("result") @@ -51,9 +51,9 @@ def test_lazy_oneshot(self): d1 = ol.when_fired() d2 = ol.when_fired() def _addmore(res): - self.failUnlessEqual(res, "result") + self.assertEqual(res, "result") d3 = ol.when_fired() - d3.addCallback(self.failUnlessEqual, "result") + d3.addCallback(self.assertEqual, "result") return d3 d1.addCallback(_addmore) def _get_result(): @@ -75,8 +75,8 @@ def test_observerlist(self): ol.unsubscribe(l1.append) ol.notify(3) def _check(res): - self.failUnlessEqual(l1, [1,2]) - self.failUnlessEqual(l2, [2,3]) + self.assertEqual(l1, [1,2]) + self.assertEqual(l2, [2,3]) d = nextTurn() d.addCallback(_check) def _step2(res): @@ -87,7 +87,7 @@ def _add(a, b, c=None): ol.notify(4, 5, c=6) return nextTurn() def _check2(res): - self.failUnlessEqual(l3, [(4,5,6)]) + self.assertEqual(l3, [(4,5,6)]) d.addCallback(_step2) d.addCallback(_check2) return d diff --git a/pyutil/test/current/test_time_format.py b/pyutil/test/current/test_time_format.py index 6c88184..c4d6137 100644 --- a/pyutil/test/current/test_time_format.py +++ b/pyutil/test/current/test_time_format.py @@ -35,11 +35,11 @@ def test_iso_utc_time_to_localseconds(self, timer=increasing_timer.timer): t1 = int(timer.time() - (365*3600*2/3)) iso_utc_t1 = time_format.iso_utc(t1) t1_2 = time_format.iso_utc_time_to_seconds(iso_utc_t1) - self.failUnlessEqual(t1, t1_2) + self.assertEqual(t1, t1_2) t1 = int(timer.time()) iso_utc_t1 = time_format.iso_utc(t1) t1_2 = time_format.iso_utc_time_to_seconds(iso_utc_t1) - self.failUnlessEqual(t1, t1_2) + self.assertEqual(t1, t1_2) def test_epoch(self): return self._help_test_epoch() @@ -71,32 +71,32 @@ def test_epoch_in_London(self): def _help_test_epoch(self): origtzname = time.tzname s = time_format.iso_utc_time_to_seconds("1970-01-01T00:00:01Z") - self.failUnlessEqual(s, 1.0) + self.assertEqual(s, 1.0) s = time_format.iso_utc_time_to_seconds("1970-01-01_00:00:01Z") - self.failUnlessEqual(s, 1.0) + self.assertEqual(s, 1.0) s = time_format.iso_utc_time_to_seconds("1970-01-01 00:00:01Z") - self.failUnlessEqual(s, 1.0) + self.assertEqual(s, 1.0) - self.failUnlessEqual(time_format.iso_utc(1.0), "1970-01-01 00:00:01Z") - self.failUnlessEqual(time_format.iso_utc(1.0, sep="_"), + self.assertEqual(time_format.iso_utc(1.0), "1970-01-01 00:00:01Z") + self.assertEqual(time_format.iso_utc(1.0, sep="_"), "1970-01-01_00:00:01Z") now = time.time() isostr = time_format.iso_utc(now) timestamp = time_format.iso_utc_time_to_seconds(isostr) - self.failUnlessEqual(int(timestamp), int(now)) + self.assertEqual(int(timestamp), int(now)) def my_time(): return 1.0 - self.failUnlessEqual(time_format.iso_utc(t=my_time), + self.assertEqual(time_format.iso_utc(t=my_time), "1970-01-01 00:00:01Z") self.failUnlessRaises(ValueError, time_format.iso_utc_time_to_seconds, "invalid timestring") s = time_format.iso_utc_time_to_seconds("1970-01-01 00:00:01.500Z") - self.failUnlessEqual(s, 1.5) + self.assertEqual(s, 1.5) # Look for daylight-savings-related errors. thatmomentinmarch = time_format.iso_utc_time_to_seconds("2009-03-20 21:49:02.226536Z") - self.failUnlessEqual(thatmomentinmarch, 1237585742.226536) - self.failUnlessEqual(origtzname, time.tzname) + self.assertEqual(thatmomentinmarch, 1237585742.226536) + self.assertEqual(origtzname, time.tzname) diff --git a/pyutil/test/current/test_version_class.py b/pyutil/test/current/test_version_class.py index d0a9a01..ae0f3e9 100644 --- a/pyutil/test/current/test_version_class.py +++ b/pyutil/test/current/test_version_class.py @@ -22,7 +22,7 @@ def test_comparisons(self): self.failUnless(V('1.0a1') < V('1.0b1')) self.failUnless(V('1.0b1') < V('1.0c1')) self.failUnless(V('1.0a1') < V('1.0a1-r99')) - self.failUnlessEqual(V('1.0a1.post987'), V('1.0a1-r987')) - self.failUnlessEqual(str(V('1.0a1.post999')), '1.0.0a1-r999') - self.failUnlessEqual(str(V('1.0a1-r999')), '1.0.0a1-r999') + self.assertEqual(V('1.0a1.post987'), V('1.0a1-r987')) + self.assertEqual(str(V('1.0a1.post999')), '1.0.0a1-r999') + self.assertEqual(str(V('1.0a1-r999')), '1.0.0a1-r999') self.failIfEqual(V('1.0a1'), V('1.0a1-r987')) diff --git a/pyutil/test/out_of_shape/test_odict.py b/pyutil/test/out_of_shape/test_odict.py index c3ebd27..e8b0118 100644 --- a/pyutil/test/out_of_shape/test_odict.py +++ b/pyutil/test/out_of_shape/test_odict.py @@ -122,7 +122,7 @@ def _test_insert_and_get_and_items(self, d) : self.failUnless(d.get("spam2") == "eggs2") self.failUnless(d["spam"] == "eggs") self.failUnless(d["spam2"] == "eggs2") - self.failUnlessEqual(d.items(), [("spam", "eggs"), ("spam2", "eggs2")], d) + self.assertEqual(d.items(), [("spam", "eggs"), ("spam2", "eggs2")], d) def _test_move_to_most_recent(self, d) : d.insert("spam", "eggs") @@ -131,39 +131,39 @@ def _test_move_to_most_recent(self, d) : self.failUnless(d.get("spam2") == "eggs2") self.failUnless(d["spam"] == "eggs") self.failUnless(d["spam2"] == "eggs2") - self.failUnlessEqual(d.items(), [("spam", "eggs"), ("spam2", "eggs2")]) + self.assertEqual(d.items(), [("spam", "eggs"), ("spam2", "eggs2")]) d.move_to_most_recent("spam") - self.failUnlessEqual(d.items(), [("spam2", "eggs2"), ("spam", "eggs")]) + self.assertEqual(d.items(), [("spam2", "eggs2"), ("spam", "eggs")]) def _test_insert_and_remove(self, d): d.insert('spam', "eggs") self.failUnless(d.has_key('spam')) self.failUnless(d.get('spam') == "eggs") self.failUnless(d['spam'] == "eggs") - self.failUnlessEqual(d.items(), [("spam", "eggs")]) + self.assertEqual(d.items(), [("spam", "eggs")]) x = d.remove('spam') self.failUnless(x == "eggs", "x: %r" % x) self.failUnless(not d.has_key('spam')) - self.failUnlessEqual(d.items(), []) + self.assertEqual(d.items(), []) d['spam'] = "eggsy" self.failUnless(d.has_key('spam')) self.failUnless(d.get('spam') == "eggsy") self.failUnless(d['spam'] == "eggsy") - self.failUnlessEqual(d.items(), [("spam", "eggsy")]) + self.assertEqual(d.items(), [("spam", "eggsy")]) del d['spam'] self.failUnless(not d.has_key('spam')) - self.failUnlessEqual(d.items(), []) + self.assertEqual(d.items(), []) def _test_setdefault(self, d): d.setdefault('spam', "eggs") self.failUnless(d.has_key('spam')) self.failUnless(d.get('spam') == "eggs") self.failUnless(d['spam'] == "eggs") - self.failUnlessEqual(d.items(), [("spam", "eggs")]) + self.assertEqual(d.items(), [("spam", "eggs")]) x = d.remove('spam') self.failUnless(x == "eggs", "x: %r" % x) self.failUnless(not d.has_key('spam')) - self.failUnlessEqual(d.items(), []) + self.assertEqual(d.items(), []) def _test_extracted_bound_method(self, d): insmeth = d.insert @@ -187,7 +187,7 @@ def _test_clear(self, d): d.clear() d._assert_invariants() self.failUnless(len(d) == 0) - self.failUnlessEqual(d.items(), []) + self.assertEqual(d.items(), []) def _test_update_from_dict(self, d): self.failUnless(d._assert_invariants()) @@ -219,38 +219,38 @@ def _test_update_from_odict(self, d): self.failUnless(d._assert_invariants()) self.failUnless(d.get('c') == 2) self.failUnless(d._assert_invariants()) - self.failUnlessEqual(d.items(), [("b", 1), ("a", 0), ("c", 2)]) + self.assertEqual(d.items(), [("b", 1), ("a", 0), ("c", 2)]) def _test_popitem(self, C): c = C({"a": 1}) res = c.popitem() - self.failUnlessEqual(res, ("a", 1,)) + self.assertEqual(res, ("a", 1,)) c["a"] = 1 c["b"] = 2 res = c.popitem() - self.failUnlessEqual(res, ("b", 2,)) + self.assertEqual(res, ("b", 2,)) def _test_pop(self, C): c = C({"a": 1}) res = c.pop() - self.failUnlessEqual(res, "a") + self.assertEqual(res, "a") c["a"] = 1 c["b"] = 2 res = c.pop() - self.failUnlessEqual(res, "b") + self.assertEqual(res, "b") def _test_iterate_items(self, C): c = C({"a": 1}) c["b"] = 2 i = c.iteritems() x = i.next() - self.failUnlessEqual(x, ("a", 1,)) + self.assertEqual(x, ("a", 1,)) x = i.next() - self.failUnlessEqual(x, ("b", 2,)) + self.assertEqual(x, ("b", 2,)) try: i.next() self.fail() # Should have gotten StopIteration exception @@ -262,9 +262,9 @@ def _test_iterate_keys(self, C): c["b"] = 2 i = c.iterkeys() x = i.next() - self.failUnlessEqual(x, "a") + self.assertEqual(x, "a") x = i.next() - self.failUnlessEqual(x, "b") + self.assertEqual(x, "b") try: i.next() self.fail() # Should have gotten StopIteration exception @@ -294,7 +294,7 @@ def _test_much_adding_some_removing(self, C): del c[k] for i in range(MUCHADDINGSIZE): c[i] = i - self.failUnlessEqual(len(c), MUCHADDINGSIZE) + self.assertEqual(len(c), MUCHADDINGSIZE) def _test_1(self, C): c = C() @@ -358,7 +358,7 @@ def _test_has_key(self, C): c._assert_invariants() x = c.pop() - self.failUnlessEqual(x, 10) + self.assertEqual(x, 10) c[99] = 99 c._assert_invariants() From 814e165d830b94beabbab1a785ac39da88ed3bf5 Mon Sep 17 00:00:00 2001 From: Dan Callaghan Date: Sun, 26 Aug 2018 20:54:50 +1000 Subject: [PATCH 04/17] failIf -> assertFalse --- pyutil/test/current/test_mathutil.py | 4 ++-- pyutil/test/out_of_shape/test_odict.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pyutil/test/current/test_mathutil.py b/pyutil/test/current/test_mathutil.py index e544de0..90dface 100644 --- a/pyutil/test/current/test_mathutil.py +++ b/pyutil/test/current/test_mathutil.py @@ -97,12 +97,12 @@ def test_is_power_of_k_part_2(self): if i in (1, 2, 4, 8, 16, 32, 64): self.failUnless(f(i, 2), "but %d *is* a power of 2" % i) else: - self.failIf(f(i, 2), "but %d is *not* a power of 2" % i) + self.assertFalse(f(i, 2), "but %d is *not* a power of 2" % i) for i in range(1, 100): if i in (1, 3, 9, 27, 81): self.failUnless(f(i, 3), "but %d *is* a power of 3" % i) else: - self.failIf(f(i, 3), "but %d is *not* a power of 3" % i) + self.assertFalse(f(i, 3), "but %d is *not* a power of 3" % i) def test_next_power_of_k(self): f = mathutil.next_power_of_k diff --git a/pyutil/test/out_of_shape/test_odict.py b/pyutil/test/out_of_shape/test_odict.py index e8b0118..5ed7483 100644 --- a/pyutil/test/out_of_shape/test_odict.py +++ b/pyutil/test/out_of_shape/test_odict.py @@ -365,7 +365,7 @@ def _test_has_key(self, C): self.failUnless(len(c) == 10) self.failUnless(1 in c.values(), "C: %s, c.values(): %s" % (hr(C), hr(c.values(),),)) self.failUnless(2 in c.values(), "C: %s, c.values(): %s" % (hr(C), hr(c.values(),),)) - self.failIf(10 in c.values(), "C: %s, c.values(): %s" % (hr(C), hr(c.values(),),)) + self.assertFalse(10 in c.values(), "C: %s, c.values(): %s" % (hr(C), hr(c.values(),),)) self.failUnless(99 in c.values()) def _test_em(self): From 05cc50d12d7f9b7c3064b2e3cfb61b987906fcf8 Mon Sep 17 00:00:00 2001 From: Dan Callaghan Date: Sun, 26 Aug 2018 20:55:36 +1000 Subject: [PATCH 05/17] failUnlessRaises -> assertRaises --- pyutil/test/current/test_time_format.py | 2 +- pyutil/test/current/test_version_class.py | 4 ++-- pyutil/test/out_of_shape/test_zlibutil.py | 8 ++++---- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/pyutil/test/current/test_time_format.py b/pyutil/test/current/test_time_format.py index c4d6137..4f55de1 100644 --- a/pyutil/test/current/test_time_format.py +++ b/pyutil/test/current/test_time_format.py @@ -90,7 +90,7 @@ def my_time(): return 1.0 self.assertEqual(time_format.iso_utc(t=my_time), "1970-01-01 00:00:01Z") - self.failUnlessRaises(ValueError, + self.assertRaises(ValueError, time_format.iso_utc_time_to_seconds, "invalid timestring") s = time_format.iso_utc_time_to_seconds("1970-01-01 00:00:01.500Z") diff --git a/pyutil/test/current/test_version_class.py b/pyutil/test/current/test_version_class.py index ae0f3e9..0706eb3 100644 --- a/pyutil/test/current/test_version_class.py +++ b/pyutil/test/current/test_version_class.py @@ -11,10 +11,10 @@ class T(unittest.TestCase): def test_rc_regex_rejects_rc_suffix(self): - self.failUnlessRaises(ValueError, V, '9.9.9rc9') + self.assertRaises(ValueError, V, '9.9.9rc9') def test_rc_regex_rejects_trailing_garbage(self): - self.failUnlessRaises(ValueError, V, '9.9.9c9HEYTHISISNTRIGHT') + self.assertRaises(ValueError, V, '9.9.9c9HEYTHISISNTRIGHT') def test_comparisons(self): self.failUnless(V('1.0') < V('1.1')) diff --git a/pyutil/test/out_of_shape/test_zlibutil.py b/pyutil/test/out_of_shape/test_zlibutil.py index 38ff865..a7b90fd 100644 --- a/pyutil/test/out_of_shape/test_zlibutil.py +++ b/pyutil/test/out_of_shape/test_zlibutil.py @@ -40,17 +40,17 @@ def _help_test(self, genstring, decomp, strlen): self.failUnless(s == s2) s2 = decomp(cs, maxlen=strlen, maxmem=strlen*2**6 + zlibutil.MINMAXMEM) self.failUnless(s == s2) - self.failUnlessRaises(zlibutil.TooBigError, decomp, cs, maxlen=strlen-1, maxmem=strlen*2**3 + zlibutil.MINMAXMEM) + self.assertRaises(zlibutil.TooBigError, decomp, cs, maxlen=strlen-1, maxmem=strlen*2**3 + zlibutil.MINMAXMEM) def _help_test_inplace_minmaxmem(self, genstring, decomp, strlen): s = genstring(strlen) cs = zlibutil.zlib.compress(s) s2 = decomp(cs, maxlen=strlen, maxmem=zlibutil.MINMAXMEM) self.failUnless(s == s2) - self.failUnlessRaises(zlibutil.TooBigError, decomp, cs, maxlen=strlen-1, maxmem=zlibutil.MINMAXMEM) + self.assertRaises(zlibutil.TooBigError, decomp, cs, maxlen=strlen-1, maxmem=zlibutil.MINMAXMEM) def _help_test_inplace(self, genstring, decomp, strlen): - # ### XXX self.failUnlessRaises(UnsafeDecompressError, decomp, zlib.compress(genstring(strlen)), maxlen=strlen, maxmem=strlen-1) + # ### XXX self.assertRaises(UnsafeDecompressError, decomp, zlib.compress(genstring(strlen)), maxlen=strlen, maxmem=strlen-1) s = genstring(strlen) cs = zlibutil.zlib.compress(s) s2 = decomp(cs, maxlen=strlen, maxmem=max(strlen*2**3, zlibutil.MINMAXMEM)) @@ -61,7 +61,7 @@ def _help_test_inplace(self, genstring, decomp, strlen): self.failUnless(s == s2) s2 = decomp(cs, maxlen=strlen, maxmem=max(strlen/2, zlibutil.MINMAXMEM)) self.failUnless(s == s2) - self.failUnlessRaises(zlibutil.TooBigError, decomp, cs, maxlen=strlen-1, maxmem=max(strlen*2**3, zlibutil.MINMAXMEM)) + self.assertRaises(zlibutil.TooBigError, decomp, cs, maxlen=strlen-1, maxmem=max(strlen*2**3, zlibutil.MINMAXMEM)) def testem(self): # for strlen in [2**1, 2**2, 2**10, 2**14, 2**21]: # a *real* test ought to include 2**21, which exercises different cases re: maxmem. But it takes too long. From aa00590408d0a95c48d6d52c3c019c89903c7a24 Mon Sep 17 00:00:00 2001 From: Dan Callaghan Date: Sun, 26 Aug 2018 20:56:17 +1000 Subject: [PATCH 06/17] failUnlessAlmostEqual -> assertAlmostEqual --- pyutil/test/current/test_mathutil.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyutil/test/current/test_mathutil.py b/pyutil/test/current/test_mathutil.py index 90dface..3994927 100644 --- a/pyutil/test/current/test_mathutil.py +++ b/pyutil/test/current/test_mathutil.py @@ -130,7 +130,7 @@ def test_ave(self): f = mathutil.ave self.assertEqual(f([1,2,3]), 2) self.assertEqual(f([0,0,0,4]), 1) - self.failUnlessAlmostEqual(f([0.0, 1.0, 1.0]), .666666666666) + self.assertAlmostEqual(f([0.0, 1.0, 1.0]), .666666666666) def assertEqualContents(self, a, b): self.assertEqual(sorted(a), sorted(b)) From b28ca7ffabd77f745956b9f00c5acb005664487a Mon Sep 17 00:00:00 2001 From: Dan Callaghan Date: Sun, 26 Aug 2018 20:56:36 +1000 Subject: [PATCH 07/17] failUnless -> assertTrue --- pyutil/test/current/json_tests/test_pass1.py | 2 +- pyutil/test/current/test_iputil.py | 6 +- pyutil/test/current/test_mathutil.py | 6 +- pyutil/test/current/test_version_class.py | 10 +- pyutil/test/deprecated/test_dictutil.py | 12 +- pyutil/test/deprecated/test_picklesaver.py | 2 +- pyutil/test/out_of_shape/test_cache.py | 144 +++++++++---------- pyutil/test/out_of_shape/test_odict.py | 118 +++++++-------- pyutil/test/out_of_shape/test_strutil.py | 8 +- pyutil/test/out_of_shape/test_zlibutil.py | 14 +- 10 files changed, 161 insertions(+), 161 deletions(-) diff --git a/pyutil/test/current/json_tests/test_pass1.py b/pyutil/test/current/json_tests/test_pass1.py index d1897e9..e4dcbab 100644 --- a/pyutil/test/current/json_tests/test_pass1.py +++ b/pyutil/test/current/json_tests/test_pass1.py @@ -70,4 +70,4 @@ def test_parse(self): res = json.loads(JSON) out = json.dumps(res) self.assertEqual(res, json.loads(out)) - self.failUnless("2.3456789012E+676" in json.dumps(res, allow_nan=False)) + self.assertTrue("2.3456789012E+676" in json.dumps(res, allow_nan=False)) diff --git a/pyutil/test/current/test_iputil.py b/pyutil/test/current/test_iputil.py index 9802ca9..f5da83a 100644 --- a/pyutil/test/current/test_iputil.py +++ b/pyutil/test/current/test_iputil.py @@ -20,7 +20,7 @@ class ListAddresses(testutil.SignalMixin): def test_get_local_ip_for(self): addr = iputil.get_local_ip_for('127.0.0.1') - self.failUnless(DOTTED_QUAD_RE.match(addr)) + self.assertTrue(DOTTED_QUAD_RE.match(addr)) def test_list_async(self): try: @@ -32,8 +32,8 @@ def test_list_async(self): d = iputil.get_local_addresses_async() def _check(addresses): - self.failUnless(len(addresses) >= 1) # always have localhost - self.failUnless("127.0.0.1" in addresses, addresses) + self.assertTrue(len(addresses) >= 1) # always have localhost + self.assertTrue("127.0.0.1" in addresses, addresses) d.addCallbacks(_check) return d test_list_async.timeout=2 diff --git a/pyutil/test/current/test_mathutil.py b/pyutil/test/current/test_mathutil.py index 3994927..17d4517 100644 --- a/pyutil/test/current/test_mathutil.py +++ b/pyutil/test/current/test_mathutil.py @@ -46,7 +46,7 @@ def test_div_ceil(self): self.assertEqual(f(5, 3), 2) self.assertEqual(f(6, 3), 2) self.assertEqual(f(7, 3), 3) - self.failUnless(isinstance(f(0.0, 1), int)) + self.assertTrue(isinstance(f(0.0, 1), int)) self.assertEqual(f(7.0, 3.0), 3) self.assertEqual(f(7, 3.0), 3) self.assertEqual(f(7.0, 3), 3) @@ -95,12 +95,12 @@ def test_is_power_of_k_part_2(self): f = mathutil.is_power_of_k for i in range(1, 100): if i in (1, 2, 4, 8, 16, 32, 64): - self.failUnless(f(i, 2), "but %d *is* a power of 2" % i) + self.assertTrue(f(i, 2), "but %d *is* a power of 2" % i) else: self.assertFalse(f(i, 2), "but %d is *not* a power of 2" % i) for i in range(1, 100): if i in (1, 3, 9, 27, 81): - self.failUnless(f(i, 3), "but %d *is* a power of 3" % i) + self.assertTrue(f(i, 3), "but %d *is* a power of 3" % i) else: self.assertFalse(f(i, 3), "but %d is *not* a power of 3" % i) diff --git a/pyutil/test/current/test_version_class.py b/pyutil/test/current/test_version_class.py index 0706eb3..8754440 100644 --- a/pyutil/test/current/test_version_class.py +++ b/pyutil/test/current/test_version_class.py @@ -17,11 +17,11 @@ def test_rc_regex_rejects_trailing_garbage(self): self.assertRaises(ValueError, V, '9.9.9c9HEYTHISISNTRIGHT') def test_comparisons(self): - self.failUnless(V('1.0') < V('1.1')) - self.failUnless(V('1.0a1') < V('1.0')) - self.failUnless(V('1.0a1') < V('1.0b1')) - self.failUnless(V('1.0b1') < V('1.0c1')) - self.failUnless(V('1.0a1') < V('1.0a1-r99')) + self.assertTrue(V('1.0') < V('1.1')) + self.assertTrue(V('1.0a1') < V('1.0')) + self.assertTrue(V('1.0a1') < V('1.0b1')) + self.assertTrue(V('1.0b1') < V('1.0c1')) + self.assertTrue(V('1.0a1') < V('1.0a1-r99')) self.assertEqual(V('1.0a1.post987'), V('1.0a1-r987')) self.assertEqual(str(V('1.0a1.post999')), '1.0.0a1-r999') self.assertEqual(str(V('1.0a1-r999')), '1.0.0a1-r999') diff --git a/pyutil/test/deprecated/test_dictutil.py b/pyutil/test/deprecated/test_dictutil.py index 6a728ec..0887680 100644 --- a/pyutil/test/deprecated/test_dictutil.py +++ b/pyutil/test/deprecated/test_dictutil.py @@ -36,17 +36,17 @@ def _help_test_empty_dict(self, klass): d1 = klass() d2 = klass({}) - self.failUnless(d1 == d2, "d1: %r, d2: %r" % (d1, d2,)) - self.failUnless(len(d1) == 0) - self.failUnless(len(d2) == 0) + self.assertTrue(d1 == d2, "d1: %r, d2: %r" % (d1, d2,)) + self.assertTrue(len(d1) == 0) + self.assertTrue(len(d2) == 0) def _help_test_nonempty_dict(self, klass): d1 = klass({'a': 1, 'b': "eggs", 3: "spam",}) d2 = klass({'a': 1, 'b': "eggs", 3: "spam",}) - self.failUnless(d1 == d2) - self.failUnless(len(d1) == 3, "%s, %s" % (len(d1), d1,)) - self.failUnless(len(d2) == 3) + self.assertTrue(d1 == d2) + self.assertTrue(len(d1) == 3, "%s, %s" % (len(d1), d1,)) + self.assertTrue(len(d2) == 3) def _help_test_eq_but_notis(self, klass): d = klass({'a': 3, 'b': EqButNotIs(3), 'c': 3}) diff --git a/pyutil/test/deprecated/test_picklesaver.py b/pyutil/test/deprecated/test_picklesaver.py index 178de12..0eb4ea2 100644 --- a/pyutil/test/deprecated/test_picklesaver.py +++ b/pyutil/test/deprecated/test_picklesaver.py @@ -32,6 +32,6 @@ def test_save_now(self): fname = os.path.join(tempdir.name, "picklesavertest") self._test_save_now(fname) - self.failUnless(os.path.isfile(fname), "The file [%s] does not exist." %(fname,)) + self.assertTrue(os.path.isfile(fname), "The file [%s] does not exist." %(fname,)) tempdir.shutdown() diff --git a/pyutil/test/out_of_shape/test_cache.py b/pyutil/test/out_of_shape/test_cache.py index 07fa9f2..9394f05 100644 --- a/pyutil/test/out_of_shape/test_cache.py +++ b/pyutil/test/out_of_shape/test_cache.py @@ -116,7 +116,7 @@ def slow_bench(): class Testy(unittest.TestCase): def _test_empty_lookup(self, d) : - self.failUnless(d.get('spam') is None) + self.assertTrue(d.get('spam') is None) def _test_key_error(self, C) : d = C() @@ -129,83 +129,83 @@ def _test_key_error(self, C) : def _test_insert_and_get(self, d) : d.insert("spam", "eggs") d["spam2"] = "eggs2" - self.failUnless(d.get("spam") == "eggs", str(d)) - self.failUnless(d.get("spam2") == "eggs2") - self.failUnless(d["spam"] == "eggs") - self.failUnless(d["spam2"] == "eggs2") + self.assertTrue(d.get("spam") == "eggs", str(d)) + self.assertTrue(d.get("spam2") == "eggs2") + self.assertTrue(d["spam"] == "eggs") + self.assertTrue(d["spam2"] == "eggs2") def _test_insert_and_remove(self, d): d.insert('spam', "eggs") - self.failUnless(d.has_key('spam')) - self.failUnless(d.get('spam') == "eggs") - self.failUnless(d['spam'] == "eggs") + self.assertTrue(d.has_key('spam')) + self.assertTrue(d.get('spam') == "eggs") + self.assertTrue(d['spam'] == "eggs") x = d.remove('spam') - self.failUnless(x == "eggs", "x: %r" % x) - self.failUnless(not d.has_key('spam')) + self.assertTrue(x == "eggs", "x: %r" % x) + self.assertTrue(not d.has_key('spam')) d['spam'] = "eggs" - self.failUnless(d.has_key('spam')) - self.failUnless(d.get('spam') == "eggs") - self.failUnless(d['spam'] == "eggs") + self.assertTrue(d.has_key('spam')) + self.assertTrue(d.get('spam') == "eggs") + self.assertTrue(d['spam'] == "eggs") del d['spam'] - self.failUnless(not d.has_key('spam')) + self.assertTrue(not d.has_key('spam')) def _test_setdefault(self, d): d.setdefault('spam', "eggs") - self.failUnless(d.has_key('spam')) - self.failUnless(d.get('spam') == "eggs") - self.failUnless(d['spam'] == "eggs") + self.assertTrue(d.has_key('spam')) + self.assertTrue(d.get('spam') == "eggs") + self.assertTrue(d['spam'] == "eggs") x = d.remove('spam') - self.failUnless(x == "eggs", "x: %r" % x) - self.failUnless(not d.has_key('spam')) + self.assertTrue(x == "eggs", "x: %r" % x) + self.assertTrue(not d.has_key('spam')) def _test_extracted_bound_method(self, d): insmeth = d.insert insmeth('spammy', "eggsy") - self.failUnless(d.get('spammy') == "eggsy") + self.assertTrue(d.get('spammy') == "eggsy") def _test_extracted_unbound_method(self, d): insumeth = d.__class__.insert insumeth(d, 'spammy', "eggsy") - self.failUnless(d.get('spammy') == "eggsy") + self.assertTrue(d.get('spammy') == "eggsy") def _test_unbound_method(self, C, d): umeth = C.insert umeth(d, 'spammy', "eggsy") - self.failUnless(d.get('spammy') == "eggsy") + self.assertTrue(d.get('spammy') == "eggsy") def _test_clear(self, d): d[11] = 11 d._assert_invariants() - self.failUnless(len(d) == 1) + self.assertTrue(len(d) == 1) d.clear() d._assert_invariants() - self.failUnless(len(d) == 0) + self.assertTrue(len(d) == 0) def _test_update(self, d): - self.failUnless(d._assert_invariants()) + self.assertTrue(d._assert_invariants()) d['b'] = 99 - self.failUnless(d._assert_invariants()) + self.assertTrue(d._assert_invariants()) d2={ 'a': 0, 'b': 1, 'c': 2,} d.update(d2) - self.failUnless(d._assert_invariants()) - self.failUnless(d.get('a') == 0, "d.get('a'): %s" % d.get('a')) - self.failUnless(d._assert_invariants()) - self.failUnless(d.get('b') == 1, "d.get('b'): %s" % d.get('b')) - self.failUnless(d._assert_invariants()) - self.failUnless(d.get('c') == 2) - self.failUnless(d._assert_invariants()) + self.assertTrue(d._assert_invariants()) + self.assertTrue(d.get('a') == 0, "d.get('a'): %s" % d.get('a')) + self.assertTrue(d._assert_invariants()) + self.assertTrue(d.get('b') == 1, "d.get('b'): %s" % d.get('b')) + self.assertTrue(d._assert_invariants()) + self.assertTrue(d.get('c') == 2) + self.assertTrue(d._assert_invariants()) def _test_popitem(self, C): c = C({"a": 1}) res = c.popitem() _assert(res == ("a", 1,), C, c, res) - self.failUnless(res == ("a", 1,)) + self.assertTrue(res == ("a", 1,)) def _test_iterate_items(self, C): c = C({"a": 1}) i = c.iteritems() x = i.next() - self.failUnless(x == ("a", 1,)) + self.assertTrue(x == ("a", 1,)) try: i.next() self.fail() # Should have gotten StopIteration exception @@ -216,7 +216,7 @@ def _test_iterate_keys(self, C): c = C({"a": 1}) i = c.iterkeys() x = i.next() - self.failUnless(x == "a") + self.assertTrue(x == "a") try: i.next() self.fail() # Should have gotten StopIteration exception @@ -227,7 +227,7 @@ def _test_iterate_values(self, C): c = C({"a": 1}) i = c.itervalues() x = i.next() - self.failUnless(x == 1) + self.assertTrue(x == 1) try: i.next() self.fail() # Should have gotten StopIteration exception @@ -243,7 +243,7 @@ def _test_LRU_much_adding_some_removing(self, C): del c[k] for i in range(MUCHADDINGSIZE): c[i] = i - self.failUnless(len(c) == MUCHADDINGSIZE) + self.assertTrue(len(c) == MUCHADDINGSIZE) def _test_LRU_1(self, C): c = C(maxsize=10) @@ -297,39 +297,39 @@ def _test_LRU_full(self, C): c._assert_invariants() c[i] = i c._assert_invariants() - self.failUnless(len(c) == 10) - self.failUnless(10 in c.values(), c.values()) - self.failUnless(0 not in c.values()) + self.assertTrue(len(c) == 10) + self.assertTrue(10 in c.values(), c.values()) + self.assertTrue(0 not in c.values()) del c[1] c._assert_invariants() - self.failUnless(1 not in c.values()) - self.failUnless(len(c) == 9) + self.assertTrue(1 not in c.values()) + self.assertTrue(len(c) == 9) c[11] = 11 c._assert_invariants() - self.failUnless(len(c) == 10) - self.failUnless(1 not in c.values()) - self.failUnless(11 in c.values()) + self.assertTrue(len(c) == 10) + self.assertTrue(1 not in c.values()) + self.assertTrue(11 in c.values()) del c[11] c._assert_invariants() c[11] = 11 c._assert_invariants() - self.failUnless(len(c) == 10) - self.failUnless(1 not in c.values()) - self.failUnless(11 in c.values()) + self.assertTrue(len(c) == 10) + self.assertTrue(1 not in c.values()) + self.assertTrue(11 in c.values()) c[11] = 11 c._assert_invariants() - self.failUnless(len(c) == 10) - self.failUnless(1 not in c.values()) - self.failUnless(11 in c.values()) + self.assertTrue(len(c) == 10) + self.assertTrue(1 not in c.values()) + self.assertTrue(11 in c.values()) for i in xrange(200): c[i] = i c._assert_invariants() - self.failUnless(199 in c.values()) - self.failUnless(190 in c.values()) + self.assertTrue(199 in c.values()) + self.assertTrue(190 in c.values()) def _test_LRU_has_key(self, C): c = C(maxsize=10) @@ -338,9 +338,9 @@ def _test_LRU_has_key(self, C): c._assert_invariants() c[i] = i c._assert_invariants() - self.failUnless(len(c) == 10) - self.failUnless(10 in c.values()) - self.failUnless(0 not in c.values()) + self.assertTrue(len(c) == 10) + self.assertTrue(10 in c.values()) + self.assertTrue(0 not in c.values()) # c.has_key(1) # this touches `1' and makes it fresher so that it will live and `2' will die next time we overfill. c[1] = 1 # this touches `1' and makes it fresher so that it will live and `2' will die next time we overfill. @@ -348,10 +348,10 @@ def _test_LRU_has_key(self, C): c[99] = 99 c._assert_invariants() - self.failUnless(len(c) == 10) - self.failUnless(1 in c.values(), "C: %s, c.values(): %s" % (hr(C), hr(c.values(),),)) - self.failUnless(not 2 in c.values()) - self.failUnless(99 in c.values()) + self.assertTrue(len(c) == 10) + self.assertTrue(1 in c.values(), "C: %s, c.values(): %s" % (hr(C), hr(c.values(),),)) + self.assertTrue(not 2 in c.values()) + self.assertTrue(99 in c.values()) def _test_LRU_not_overfull_on_idempotent_add(self, C): c = C(maxsize=10) @@ -360,27 +360,27 @@ def _test_LRU_not_overfull_on_idempotent_add(self, C): c[1] = "spam" # Now 1 is the freshest, so 2 is the next one that would be removed *if* we went over limit. c[3] = "eggs" - self.failUnless(c.has_key(2)) - self.failUnless(len(c) == 10) + self.assertTrue(c.has_key(2)) + self.assertTrue(len(c) == 10) c._assert_invariants() def _test_LRU_overflow_on_update(self, C): d = C(maxsize=10) - self.failUnless(d._assert_invariants()) + self.assertTrue(d._assert_invariants()) d2 = {} for i in range(12): d2[i] = i d.update(d2) - self.failUnless(d._assert_invariants()) - self.failUnless(len(d) == 10) + self.assertTrue(d._assert_invariants()) + self.assertTrue(len(d) == 10) def _test_LRU_overflow_on_init(self, C): d2 = {} for i in range(12): d2[i] = i d = C(d2, maxsize=10) - self.failUnless(d._assert_invariants()) - self.failUnless(len(d) == 10) + self.assertTrue(d._assert_invariants()) + self.assertTrue(len(d) == 10) def _test_em(self): for klass in (cache.LRUCache, cache.SmallLRUCache,): @@ -416,7 +416,7 @@ def _test_mem_leakage(self): memutil.measure_mem_leakage(self.test_em, max(2**3, SAMPLES/2**3), iterspersample=2**0) slope = memutil.measure_mem_leakage(self.test_em, max(2**3, SAMPLES/2**3), iterspersample=2**0) - self.failUnless(slope <= MIN_SLOPE, "%s leaks memory at a rate of approximately %s system bytes per invocation" % (self.test_em, "%0.3f" % slope,)) + self.assertTrue(slope <= MIN_SLOPE, "%s leaks memory at a rate of approximately %s system bytes per invocation" % (self.test_em, "%0.3f" % slope,)) def test_mem_leakage_much_adding_some_removing(self): try: @@ -431,7 +431,7 @@ def _test_mem_leakage_much_adding_some_removing(self): memutil.measure_mem_leakage(self._mem_test_LRU_much_adding_some_removing, SAMPLES, iterspersample=2**0) slope = memutil.measure_mem_leakage(self._mem_test_LRU_much_adding_some_removing, SAMPLES, iterspersample=2**0) - self.failUnless(slope <= MIN_SLOPE, "%s leaks memory at a rate of approximately %s system bytes per invocation" % (self._mem_test_LRU_much_adding_some_removing, "%0.3f" % slope,)) + self.assertTrue(slope <= MIN_SLOPE, "%s leaks memory at a rate of approximately %s system bytes per invocation" % (self._mem_test_LRU_much_adding_some_removing, "%0.3f" % slope,)) def test_obj_leakage(self): self._test_obj_leakage() @@ -442,7 +442,7 @@ def _test_obj_leakage(self): memutil.measure_obj_leakage(self.test_em, max(2**3, SAMPLES/2**3), iterspersample=2**0) slope = memutil.measure_obj_leakage(self.test_em, max(2**3, SAMPLES/2**3), iterspersample=2**0) - self.failUnless(slope <= MIN_SLOPE, "%s leaks objects at a rate of approximately %s system bytes per invocation" % (self.test_em, "%0.3f" % slope,)) + self.assertTrue(slope <= MIN_SLOPE, "%s leaks objects at a rate of approximately %s system bytes per invocation" % (self.test_em, "%0.3f" % slope,)) def test_obj_leakage_much_adding_some_removing(self): self._test_obj_leakage_much_adding_some_removing() @@ -453,4 +453,4 @@ def _test_obj_leakage_much_adding_some_removing(self): memutil.measure_obj_leakage(self._mem_test_LRU_much_adding_some_removing, SAMPLES, iterspersample=2**0) slope = memutil.measure_obj_leakage(self._mem_test_LRU_much_adding_some_removing, SAMPLES, iterspersample=2**0) - self.failUnless(slope <= MIN_SLOPE, "%s leaks objects at a rate of approximately %s system bytes per invocation" % (self._mem_test_LRU_much_adding_some_removing, "%0.3f" % slope,)) + self.assertTrue(slope <= MIN_SLOPE, "%s leaks objects at a rate of approximately %s system bytes per invocation" % (self._mem_test_LRU_much_adding_some_removing, "%0.3f" % slope,)) diff --git a/pyutil/test/out_of_shape/test_odict.py b/pyutil/test/out_of_shape/test_odict.py index 5ed7483..46014d5 100644 --- a/pyutil/test/out_of_shape/test_odict.py +++ b/pyutil/test/out_of_shape/test_odict.py @@ -105,7 +105,7 @@ def slow_bench(): class Testy(unittest.TestCase): def _test_empty_lookup(self, d) : - self.failUnless(d.get('spam') is None) + self.assertTrue(d.get('spam') is None) def _test_key_error(self, C) : d = C() @@ -118,107 +118,107 @@ def _test_key_error(self, C) : def _test_insert_and_get_and_items(self, d) : d.insert("spam", "eggs") d["spam2"] = "eggs2" - self.failUnless(d.get("spam") == "eggs", str(d)) - self.failUnless(d.get("spam2") == "eggs2") - self.failUnless(d["spam"] == "eggs") - self.failUnless(d["spam2"] == "eggs2") + self.assertTrue(d.get("spam") == "eggs", str(d)) + self.assertTrue(d.get("spam2") == "eggs2") + self.assertTrue(d["spam"] == "eggs") + self.assertTrue(d["spam2"] == "eggs2") self.assertEqual(d.items(), [("spam", "eggs"), ("spam2", "eggs2")], d) def _test_move_to_most_recent(self, d) : d.insert("spam", "eggs") d["spam2"] = "eggs2" - self.failUnless(d.get("spam") == "eggs", str(d)) - self.failUnless(d.get("spam2") == "eggs2") - self.failUnless(d["spam"] == "eggs") - self.failUnless(d["spam2"] == "eggs2") + self.assertTrue(d.get("spam") == "eggs", str(d)) + self.assertTrue(d.get("spam2") == "eggs2") + self.assertTrue(d["spam"] == "eggs") + self.assertTrue(d["spam2"] == "eggs2") self.assertEqual(d.items(), [("spam", "eggs"), ("spam2", "eggs2")]) d.move_to_most_recent("spam") self.assertEqual(d.items(), [("spam2", "eggs2"), ("spam", "eggs")]) def _test_insert_and_remove(self, d): d.insert('spam', "eggs") - self.failUnless(d.has_key('spam')) - self.failUnless(d.get('spam') == "eggs") - self.failUnless(d['spam'] == "eggs") + self.assertTrue(d.has_key('spam')) + self.assertTrue(d.get('spam') == "eggs") + self.assertTrue(d['spam'] == "eggs") self.assertEqual(d.items(), [("spam", "eggs")]) x = d.remove('spam') - self.failUnless(x == "eggs", "x: %r" % x) - self.failUnless(not d.has_key('spam')) + self.assertTrue(x == "eggs", "x: %r" % x) + self.assertTrue(not d.has_key('spam')) self.assertEqual(d.items(), []) d['spam'] = "eggsy" - self.failUnless(d.has_key('spam')) - self.failUnless(d.get('spam') == "eggsy") - self.failUnless(d['spam'] == "eggsy") + self.assertTrue(d.has_key('spam')) + self.assertTrue(d.get('spam') == "eggsy") + self.assertTrue(d['spam'] == "eggsy") self.assertEqual(d.items(), [("spam", "eggsy")]) del d['spam'] - self.failUnless(not d.has_key('spam')) + self.assertTrue(not d.has_key('spam')) self.assertEqual(d.items(), []) def _test_setdefault(self, d): d.setdefault('spam', "eggs") - self.failUnless(d.has_key('spam')) - self.failUnless(d.get('spam') == "eggs") - self.failUnless(d['spam'] == "eggs") + self.assertTrue(d.has_key('spam')) + self.assertTrue(d.get('spam') == "eggs") + self.assertTrue(d['spam'] == "eggs") self.assertEqual(d.items(), [("spam", "eggs")]) x = d.remove('spam') - self.failUnless(x == "eggs", "x: %r" % x) - self.failUnless(not d.has_key('spam')) + self.assertTrue(x == "eggs", "x: %r" % x) + self.assertTrue(not d.has_key('spam')) self.assertEqual(d.items(), []) def _test_extracted_bound_method(self, d): insmeth = d.insert insmeth('spammy', "eggsy") - self.failUnless(d.get('spammy') == "eggsy") + self.assertTrue(d.get('spammy') == "eggsy") def _test_extracted_unbound_method(self, d): insumeth = d.__class__.insert insumeth(d, 'spammy', "eggsy") - self.failUnless(d.get('spammy') == "eggsy") + self.assertTrue(d.get('spammy') == "eggsy") def _test_unbound_method(self, C, d): umeth = C.insert umeth(d, 'spammy', "eggsy") - self.failUnless(d.get('spammy') == "eggsy") + self.assertTrue(d.get('spammy') == "eggsy") def _test_clear(self, d): d[11] = 11 d._assert_invariants() - self.failUnless(len(d) == 1) + self.assertTrue(len(d) == 1) d.clear() d._assert_invariants() - self.failUnless(len(d) == 0) + self.assertTrue(len(d) == 0) self.assertEqual(d.items(), []) def _test_update_from_dict(self, d): - self.failUnless(d._assert_invariants()) + self.assertTrue(d._assert_invariants()) d['b'] = 99 - self.failUnless(d._assert_invariants()) + self.assertTrue(d._assert_invariants()) d2={ 'a': 0, 'b': 1, 'c': 2,} d.update(d2) - self.failUnless(d._assert_invariants()) - self.failUnless(d.get('a') == 0, "d.get('a'): %s" % d.get('a')) - self.failUnless(d._assert_invariants()) - self.failUnless(d.get('b') == 1, "d.get('b'): %s" % d.get('b')) - self.failUnless(d._assert_invariants()) - self.failUnless(d.get('c') == 2) - self.failUnless(d._assert_invariants()) + self.assertTrue(d._assert_invariants()) + self.assertTrue(d.get('a') == 0, "d.get('a'): %s" % d.get('a')) + self.assertTrue(d._assert_invariants()) + self.assertTrue(d.get('b') == 1, "d.get('b'): %s" % d.get('b')) + self.assertTrue(d._assert_invariants()) + self.assertTrue(d.get('c') == 2) + self.assertTrue(d._assert_invariants()) def _test_update_from_odict(self, d): - self.failUnless(d._assert_invariants()) + self.assertTrue(d._assert_invariants()) d['b'] = 99 - self.failUnless(d._assert_invariants()) + self.assertTrue(d._assert_invariants()) d2 = odict.OrderedDict() d2['a'] = 0 d2['b'] = 1 d2['c'] = 2 d.update(d2) - self.failUnless(d._assert_invariants()) - self.failUnless(d.get('a') == 0, "d.get('a'): %s" % d.get('a')) - self.failUnless(d._assert_invariants()) - self.failUnless(d.get('b') == 1, "d.get('b'): %s" % d.get('b')) - self.failUnless(d._assert_invariants()) - self.failUnless(d.get('c') == 2) - self.failUnless(d._assert_invariants()) + self.assertTrue(d._assert_invariants()) + self.assertTrue(d.get('a') == 0, "d.get('a'): %s" % d.get('a')) + self.assertTrue(d._assert_invariants()) + self.assertTrue(d.get('b') == 1, "d.get('b'): %s" % d.get('b')) + self.assertTrue(d._assert_invariants()) + self.assertTrue(d.get('c') == 2) + self.assertTrue(d._assert_invariants()) self.assertEqual(d.items(), [("b", 1), ("a", 0), ("c", 2)]) def _test_popitem(self, C): @@ -276,9 +276,9 @@ def _test_iterate_values(self, C): c["b"] = 2 i = c.itervalues() x = i.next() - self.failUnless(x == 1) + self.assertTrue(x == 1) x = i.next() - self.failUnless(x == 2) + self.assertTrue(x == 2) try: i.next() self.fail() # Should have gotten StopIteration exception @@ -349,9 +349,9 @@ def _test_has_key(self, C): c[i] = i c._assert_invariants() del c[0] - self.failUnless(len(c) == 10) - self.failUnless(10 in c.values()) - self.failUnless(0 not in c.values()) + self.assertTrue(len(c) == 10) + self.assertTrue(10 in c.values()) + self.assertTrue(0 not in c.values()) c.has_key(1) # this touches `1' but does not make it fresher so that it will get popped next time we pop. c[1] = 1 # this touches `1' but does not make it fresher so that it will get popped. @@ -362,11 +362,11 @@ def _test_has_key(self, C): c[99] = 99 c._assert_invariants() - self.failUnless(len(c) == 10) - self.failUnless(1 in c.values(), "C: %s, c.values(): %s" % (hr(C), hr(c.values(),),)) - self.failUnless(2 in c.values(), "C: %s, c.values(): %s" % (hr(C), hr(c.values(),),)) + self.assertTrue(len(c) == 10) + self.assertTrue(1 in c.values(), "C: %s, c.values(): %s" % (hr(C), hr(c.values(),),)) + self.assertTrue(2 in c.values(), "C: %s, c.values(): %s" % (hr(C), hr(c.values(),),)) self.assertFalse(10 in c.values(), "C: %s, c.values(): %s" % (hr(C), hr(c.values(),),)) - self.failUnless(99 in c.values()) + self.assertTrue(99 in c.values()) def _test_em(self): for klass in (odict.OrderedDict,): @@ -402,7 +402,7 @@ def _test_mem_leakage(self): memutil.measure_mem_leakage(self.test_em, max(2**3, SAMPLES/2**3), iterspersample=2**0) slope = memutil.measure_mem_leakage(self.test_em, max(2**3, SAMPLES/2**3), iterspersample=2**0) - self.failUnless(slope <= MIN_SLOPE, "%s leaks memory at a rate of approximately %s system bytes per invocation" % (self.test_em, "%0.3f" % slope,)) + self.assertTrue(slope <= MIN_SLOPE, "%s leaks memory at a rate of approximately %s system bytes per invocation" % (self.test_em, "%0.3f" % slope,)) def test_mem_leakage_much_adding_some_removing(self): try: @@ -417,7 +417,7 @@ def _test_mem_leakage_much_adding_some_removing(self): memutil.measure_mem_leakage(self._mem_test_much_adding_some_removing, SAMPLES, iterspersample=2**0) slope = memutil.measure_mem_leakage(self._mem_test_much_adding_some_removing, SAMPLES, iterspersample=2**0) - self.failUnless(slope <= MIN_SLOPE, "%s leaks memory at a rate of approximately %s system bytes per invocation" % (self._mem_test_much_adding_some_removing, "%0.3f" % slope,)) + self.assertTrue(slope <= MIN_SLOPE, "%s leaks memory at a rate of approximately %s system bytes per invocation" % (self._mem_test_much_adding_some_removing, "%0.3f" % slope,)) def test_obj_leakage(self): self._test_obj_leakage() @@ -428,7 +428,7 @@ def _test_obj_leakage(self): memutil.measure_obj_leakage(self.test_em, max(2**3, SAMPLES/2**3), iterspersample=2**0) slope = memutil.measure_obj_leakage(self.test_em, max(2**3, SAMPLES/2**3), iterspersample=2**0) - self.failUnless(slope <= MIN_SLOPE, "%s leaks objects at a rate of approximately %s system bytes per invocation" % (self.test_em, "%0.3f" % slope,)) + self.assertTrue(slope <= MIN_SLOPE, "%s leaks objects at a rate of approximately %s system bytes per invocation" % (self.test_em, "%0.3f" % slope,)) def test_obj_leakage_much_adding_some_removing(self): self._test_obj_leakage_much_adding_some_removing() @@ -439,4 +439,4 @@ def _test_obj_leakage_much_adding_some_removing(self): memutil.measure_obj_leakage(self._mem_test_much_adding_some_removing, SAMPLES, iterspersample=2**0) slope = memutil.measure_obj_leakage(self._mem_test_much_adding_some_removing, SAMPLES, iterspersample=2**0) - self.failUnless(slope <= MIN_SLOPE, "%s leaks objects at a rate of approximately %s system bytes per invocation" % (self._mem_test_much_adding_some_removing, "%0.3f" % slope,)) + self.assertTrue(slope <= MIN_SLOPE, "%s leaks objects at a rate of approximately %s system bytes per invocation" % (self._mem_test_much_adding_some_removing, "%0.3f" % slope,)) diff --git a/pyutil/test/out_of_shape/test_strutil.py b/pyutil/test/out_of_shape/test_strutil.py index 3d9ba40..a919f70 100644 --- a/pyutil/test/out_of_shape/test_strutil.py +++ b/pyutil/test/out_of_shape/test_strutil.py @@ -11,10 +11,10 @@ class Teststrutil(unittest.TestCase): def test_short_input(self): - self.failUnless(strutil.pop_trailing_newlines("\r\n") == "") - self.failUnless(strutil.pop_trailing_newlines("\r") == "") - self.failUnless(strutil.pop_trailing_newlines("x\r\n") == "x") - self.failUnless(strutil.pop_trailing_newlines("x\r") == "x") + self.assertTrue(strutil.pop_trailing_newlines("\r\n") == "") + self.assertTrue(strutil.pop_trailing_newlines("\r") == "") + self.assertTrue(strutil.pop_trailing_newlines("x\r\n") == "x") + self.assertTrue(strutil.pop_trailing_newlines("x\r") == "x") def test_split(self): _assert(strutil.split_on_newlines("x\r\ny") == ["x", "y",], strutil.split_on_newlines("x\r\ny")) diff --git a/pyutil/test/out_of_shape/test_zlibutil.py b/pyutil/test/out_of_shape/test_zlibutil.py index a7b90fd..7243576 100644 --- a/pyutil/test/out_of_shape/test_zlibutil.py +++ b/pyutil/test/out_of_shape/test_zlibutil.py @@ -37,16 +37,16 @@ def _help_test(self, genstring, decomp, strlen): s = genstring(strlen) cs = zlibutil.zlib.compress(s) s2 = decomp(cs, maxlen=strlen, maxmem=strlen*2**3 + zlibutil.MINMAXMEM) - self.failUnless(s == s2) + self.assertTrue(s == s2) s2 = decomp(cs, maxlen=strlen, maxmem=strlen*2**6 + zlibutil.MINMAXMEM) - self.failUnless(s == s2) + self.assertTrue(s == s2) self.assertRaises(zlibutil.TooBigError, decomp, cs, maxlen=strlen-1, maxmem=strlen*2**3 + zlibutil.MINMAXMEM) def _help_test_inplace_minmaxmem(self, genstring, decomp, strlen): s = genstring(strlen) cs = zlibutil.zlib.compress(s) s2 = decomp(cs, maxlen=strlen, maxmem=zlibutil.MINMAXMEM) - self.failUnless(s == s2) + self.assertTrue(s == s2) self.assertRaises(zlibutil.TooBigError, decomp, cs, maxlen=strlen-1, maxmem=zlibutil.MINMAXMEM) def _help_test_inplace(self, genstring, decomp, strlen): @@ -54,13 +54,13 @@ def _help_test_inplace(self, genstring, decomp, strlen): s = genstring(strlen) cs = zlibutil.zlib.compress(s) s2 = decomp(cs, maxlen=strlen, maxmem=max(strlen*2**3, zlibutil.MINMAXMEM)) - self.failUnless(s == s2) + self.assertTrue(s == s2) s2 = decomp(cs, maxlen=strlen, maxmem=max(strlen*2**6, zlibutil.MINMAXMEM)) - self.failUnless(s == s2) + self.assertTrue(s == s2) s2 = decomp(cs, maxlen=strlen, maxmem=max(strlen-1, zlibutil.MINMAXMEM)) - self.failUnless(s == s2) + self.assertTrue(s == s2) s2 = decomp(cs, maxlen=strlen, maxmem=max(strlen/2, zlibutil.MINMAXMEM)) - self.failUnless(s == s2) + self.assertTrue(s == s2) self.assertRaises(zlibutil.TooBigError, decomp, cs, maxlen=strlen-1, maxmem=max(strlen*2**3, zlibutil.MINMAXMEM)) def testem(self): From 76747d218f29fa2a4ecb94811d3bcc5c312fe24f Mon Sep 17 00:00:00 2001 From: Dan Callaghan Date: Sun, 26 Aug 2018 20:57:13 +1000 Subject: [PATCH 08/17] failIfEqual -> assertNotEqual --- pyutil/test/current/test_version_class.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyutil/test/current/test_version_class.py b/pyutil/test/current/test_version_class.py index 8754440..b713119 100644 --- a/pyutil/test/current/test_version_class.py +++ b/pyutil/test/current/test_version_class.py @@ -25,4 +25,4 @@ def test_comparisons(self): self.assertEqual(V('1.0a1.post987'), V('1.0a1-r987')) self.assertEqual(str(V('1.0a1.post999')), '1.0.0a1-r999') self.assertEqual(str(V('1.0a1-r999')), '1.0.0a1-r999') - self.failIfEqual(V('1.0a1'), V('1.0a1-r987')) + self.assertNotEqual(V('1.0a1'), V('1.0a1-r987')) From f583ca402c03d89915390c477243696216c8f156 Mon Sep 17 00:00:00 2001 From: Dan Callaghan Date: Sun, 26 Aug 2018 20:57:55 +1000 Subject: [PATCH 09/17] use Exception instead of StandardError on Python 3 --- pyutil/memutil.py | 8 ++++++-- pyutil/zlibutil.py | 8 ++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/pyutil/memutil.py b/pyutil/memutil.py index 702b100..8bb4d8a 100644 --- a/pyutil/memutil.py +++ b/pyutil/memutil.py @@ -6,7 +6,11 @@ from __future__ import print_function # from the Python Standard Library -import exceptions, gc, math, operator, os, sys, types +import gc, math, operator, os, sys, types +try: + StandardError # PY2 +except NameError: + StandardError = Exception # PY3 # from the pyutil library from .assertutil import precondition @@ -197,7 +201,7 @@ def measure_ref_leakage(f, numsamples=2**7, iterspersample=2**4, *args, **kwargs sxx = reduce(operator.__add__, map(lambda a, avex=avex: (a - avex) ** 2, resiters)) return sxy / sxx -class NotSupportedException(exceptions.StandardError): +class NotSupportedException(StandardError): """ Just an exception class. It is thrown by get_mem_usage if the OS does not support the operation. diff --git a/pyutil/zlibutil.py b/pyutil/zlibutil.py index 3d17d5b..ba2a303 100644 --- a/pyutil/zlibutil.py +++ b/pyutil/zlibutil.py @@ -11,12 +11,16 @@ consume all of your RAM while trying to decompress it. """ -import exceptions, string, zlib +import string, zlib +try: + StandardError # PY2 +except NameError: + StandardError = Exception # PY3 from .humanreadable import hr from .assertutil import precondition -class DecompressError(exceptions.StandardError, zlib.error): pass +class DecompressError(zlib.error, StandardError): pass class UnsafeDecompressError(DecompressError): pass # This means it would take more memory to decompress than we can spare. class TooBigError(DecompressError): pass # This means the resulting uncompressed text would exceed the maximum allowed length. class ZlibError(DecompressError): pass # internal error, probably due to the input not being zlib compressed text From 14aec258ec9af4dc528d6e9c8e049e4014e8ad0b Mon Sep 17 00:00:00 2001 From: Dan Callaghan Date: Mon, 27 Aug 2018 08:28:29 +1000 Subject: [PATCH 10/17] use numbers.Integral ABC for type checks There is no long type in Python 3. --- pyutil/zlibutil.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/pyutil/zlibutil.py b/pyutil/zlibutil.py index ba2a303..3fd1421 100644 --- a/pyutil/zlibutil.py +++ b/pyutil/zlibutil.py @@ -12,6 +12,7 @@ """ import string, zlib +import numbers try: StandardError # PY2 except NameError: @@ -62,8 +63,8 @@ def decompress(zbuf, maxlen=(65 * (2**20)), maxmem=(65 * (2**20))): exceedingly large until you realize that it means you can decompress 64 KB chunks of compressiontext at a bite.) """ - assert isinstance(maxlen, (int, long,)) and maxlen > 0, "maxlen is required to be a real maxlen, geez!" - assert isinstance(maxmem, (int, long,)) and maxmem > 0, "maxmem is required to be a real maxmem, geez!" + assert isinstance(maxlen, numbers.Integral) and maxlen > 0, "maxlen is required to be a real maxlen, geez!" + assert isinstance(maxmem, numbers.Integral) and maxmem > 0, "maxmem is required to be a real maxmem, geez!" assert maxlen <= maxmem, "maxlen is required to be <= maxmem. All data that is included in the return value is counted against maxmem as well as against maxlen, so it is impossible to return a result bigger than maxmem, even if maxlen is bigger than maxmem. See decompress_to_spool() if you want to spool a large text out while limiting the amount of memory used during the process." lenzbuf = len(zbuf) @@ -146,8 +147,8 @@ def decompress_to_fileobj(zbuf, fileobj, maxlen=(65 * (2**20)), maxmem=(65 * (2* @param fileobj a file object to which the decompressed text will be written """ precondition(hasattr(fileobj, 'write') and callable(fileobj.write), "fileobj is required to have a write() method.", fileobj=fileobj) - precondition(isinstance(maxlen, (int, long,)) and maxlen > 0, "maxlen is required to be a real maxlen, geez!", maxlen=maxlen) - precondition(isinstance(maxmem, (int, long,)) and maxmem > 0, "maxmem is required to be a real maxmem, geez!", maxmem=maxmem) + precondition(isinstance(maxlen, numbers.Integral) and maxlen > 0, "maxlen is required to be a real maxlen, geez!", maxlen=maxlen) + precondition(isinstance(maxmem, numbers.Integral) and maxmem > 0, "maxmem is required to be a real maxmem, geez!", maxmem=maxmem) precondition(maxlen <= maxmem, "maxlen is required to be <= maxmem. All data that is written out to fileobj is counted against maxmem as well as against maxlen, so it is impossible to return a result bigger than maxmem, even if maxlen is bigger than maxmem. See decompress_to_spool() if you want to spool a large text out while limiting the amount of memory used during the process.", maxlen=maxlen, maxmem=maxmem) lenzbuf = len(zbuf) @@ -220,8 +221,8 @@ def decompress_to_spool(zbuf, fileobj, maxlen=(65 * (2**20)), maxmem=(65 * (2**2 @param fileobj the decompressed text will be written to it """ precondition(hasattr(fileobj, 'write') and callable(fileobj.write), "fileobj is required to have a write() method.", fileobj=fileobj) - precondition(isinstance(maxlen, (int, long,)) and maxlen > 0, "maxlen is required to be a real maxlen, geez!", maxlen=maxlen) - precondition(isinstance(maxmem, (int, long,)) and maxmem > 0, "maxmem is required to be a real maxmem, geez!", maxmem=maxmem) + precondition(isinstance(maxlen, numbers.Integral) and maxlen > 0, "maxlen is required to be a real maxlen, geez!", maxlen=maxlen) + precondition(isinstance(maxmem, numbers.Integral) and maxmem > 0, "maxmem is required to be a real maxmem, geez!", maxmem=maxmem) tmpstr = '' lenzbuf = len(zbuf) From cd0d19ff6e3a25df74092dcdc93c6c20a3de4b25 Mon Sep 17 00:00:00 2001 From: Dan Callaghan Date: Mon, 27 Aug 2018 08:37:24 +1000 Subject: [PATCH 11/17] don't treat instances of types.InstanceType specially This is only old-style classes in Python 2, and it doesn't exist at all in Python 3. It's hard to see what a useful equivalent in modern Python would be. Testing isinstance(.., object) would be pointless because nowadays everything is an instance of object. --- pyutil/memutil.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/pyutil/memutil.py b/pyutil/memutil.py index 8bb4d8a..b948285 100644 --- a/pyutil/memutil.py +++ b/pyutil/memutil.py @@ -355,7 +355,7 @@ def measure_mem_leakage(f, numsamples=2**7, iterspersample=2**4, *args, **kwargs return None return sxy / sxx -def describe_object(o, FunctionType=types.FunctionType, MethodType=types.MethodType, InstanceType=types.InstanceType): +def describe_object(o, FunctionType=types.FunctionType, MethodType=types.MethodType): """ For human analysis, when humans are attempting to understand where all the memory is going. Argument o is an object, return value is a string @@ -372,11 +372,6 @@ def describe_object(o, FunctionType=types.FunctionType, MethodType=types.MethodT sl.append("" % str(o.im_func.func_name)) except: pass - elif isinstance(o, InstanceType): - try: - sl.append("" % str(o.__class__.__name__)) - except: - pass else: sl.append(str(type(o))) From c470b8f85fbcff4edff9950ffb940fb6f299580f Mon Sep 17 00:00:00 2001 From: Dan Callaghan Date: Mon, 27 Aug 2018 08:42:06 +1000 Subject: [PATCH 12/17] zlib works in bytes not str --- pyutil/test/out_of_shape/test_zlibutil.py | 4 ++-- pyutil/zlibutil.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pyutil/test/out_of_shape/test_zlibutil.py b/pyutil/test/out_of_shape/test_zlibutil.py index 7243576..bfd8a84 100644 --- a/pyutil/test/out_of_shape/test_zlibutil.py +++ b/pyutil/test/out_of_shape/test_zlibutil.py @@ -12,7 +12,7 @@ class Accumulator: def __init__(self): - self.buf = '' + self.buf = b'' def write(self, str): self.buf += str @@ -28,7 +28,7 @@ def genrandstr(strlen): return randutil.insecurerandstr(strlen) def genbombstr(strlen): - return '0' * strlen + return b'0' * strlen MAXMEM=65*2**20 diff --git a/pyutil/zlibutil.py b/pyutil/zlibutil.py index 3fd1421..364e99e 100644 --- a/pyutil/zlibutil.py +++ b/pyutil/zlibutil.py @@ -118,7 +118,7 @@ def decompress(zbuf, maxlen=(65 * (2**20)), maxmem=(65 * (2**20))): tmpstr = '' if len(decompstrlist) > 0: - return string.join(decompstrlist, '') + return b''.join(decompstrlist) else: return decompstrlist[0] From fc9bd16f55bbb297521676570a1071e008b57387 Mon Sep 17 00:00:00 2001 From: Dan Callaghan Date: Mon, 27 Aug 2018 08:58:09 +1000 Subject: [PATCH 13/17] fix relative import --- pyutil/memutil.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyutil/memutil.py b/pyutil/memutil.py index b948285..35117e2 100644 --- a/pyutil/memutil.py +++ b/pyutil/memutil.py @@ -15,6 +15,7 @@ # from the pyutil library from .assertutil import precondition from . import mathutil +from . import dictutil class Canary: """ @@ -381,7 +382,6 @@ def describe_object(o, FunctionType=types.FunctionType, MethodType=types.MethodT pass return ''.join(sl) -import dictutil def describe_object_with_dict_details(o): sl = [] sl.append(str(type(o))) From 6993f11db67ba91a24e0c05b3cd72a8a617bead3 Mon Sep 17 00:00:00 2001 From: Dan Callaghan Date: Mon, 27 Aug 2018 10:37:26 +1000 Subject: [PATCH 14/17] update for dict protocol in Python 2.7/Python 3 --- pyutil/cache.py | 81 ++++++++------ pyutil/dictutil.py | 141 +++++++++++++----------- pyutil/humanreadable.py | 3 +- pyutil/odict.py | 110 ++++++++++-------- pyutil/test/deprecated/test_dictutil.py | 38 +++---- pyutil/test/out_of_shape/test_cache.py | 12 +- pyutil/test/out_of_shape/test_odict.py | 22 ++-- 7 files changed, 231 insertions(+), 176 deletions(-) diff --git a/pyutil/cache.py b/pyutil/cache.py index cc265a3..fe407e2 100644 --- a/pyutil/cache.py +++ b/pyutil/cache.py @@ -42,14 +42,16 @@ def __init__(self, c): self.i = c.d[c.hs][2] def __iter__(self): return self - def next(self): + def __next__(self): if self.i is self.c.ts: raise StopIteration() k = self.i - precondition(self.c.d.has_key(k), "The iterated LRUCache doesn't have the next key. Most likely this is because someone altered the contents of the LRUCache while the iteration was in progress.", k, self.c) + precondition(k in self.c.d, "The iterated LRUCache doesn't have the next key. Most likely this is because someone altered the contents of the LRUCache while the iteration was in progress.", k, self.c) (v, p, n,) = self.c.d[k] self.i = n return (k, v,) + def next(self): + return self.__next__() class KeyIterator: def __init__(self, c): @@ -57,14 +59,16 @@ def __init__(self, c): self.i = c.d[c.hs][2] def __iter__(self): return self - def next(self): + def __next__(self): if self.i is self.c.ts: raise StopIteration() k = self.i - precondition(self.c.d.has_key(k), "The iterated LRUCache doesn't have the next key. Most likely this is because someone altered the contents of the LRUCache while the iteration was in progress.", k, self.c) + precondition(k in self.c.d, "The iterated LRUCache doesn't have the next key. Most likely this is because someone altered the contents of the LRUCache while the iteration was in progress.", k, self.c) (v, p, n,) = self.c.d[k] self.i = n return k + def next(self): + return self.__next__() class ValIterator: def __init__(self, c): @@ -72,13 +76,16 @@ def __init__(self, c): self.i = c.d[c.hs][2] def __iter__(self): return self - def next(self): + def __next__(self): if self.i is self.c.ts: raise StopIteration() - precondition(self.c.d.has_key(self.i), "The iterated LRUCache doesn't have the next key. Most likely this is because someone altered the contents of the LRUCache while the iteration was in progress.", self.i, self.c) + precondition(self.i in self.c.d, "The iterated LRUCache doesn't have the next key. Most likely this is because someone altered the contents of the LRUCache while the iteration was in progress.", self.i, self.c) (v, p, n,) = self.c.d[self.i] self.i = n return v + def next(self): + return self.__next__() + class Sentinel: def __init__(self, msg): @@ -124,7 +131,7 @@ def _assert_invariants(self): _assert((len(self.d) > 2) == (self.d[self.hs][2] is not self.ts) == (self.d[self.ts][1] is not self.hs), "Head and tail point to something other than each other if and only if there is at least one element in the dictionary.", self.hs, self.ts, len(self.d)) foundprevsentinel = 0 foundnextsentinel = 0 - for (k, (v, p, n,)) in self.d.iteritems(): + for (k, (v, p, n,)) in self.d.items(): _assert(v not in (self.hs, self.ts,)) _assert(p is not self.ts, "A reference to the tail sentinel may not appear in prev.", k, v, p, n) _assert(n is not self.hs, "A reference to the head sentinel may not appear in next.", k, v, p, n) @@ -150,7 +157,7 @@ def _assert_invariants(self): def freshen(self, k, strictkey=False): assert self._assert_invariants() - if not self.d.has_key(k): + if k not in self.d: if strictkey: raise KeyError(k) return @@ -227,7 +234,7 @@ def __delitem__(self, key, default=None, strictkey=True): that key and strictkey is False """ assert self._assert_invariants() - if self.d.has_key(key): + if key in self.d: node = self.d[key] # relink self.d[node[1]][2] = node[2] @@ -244,7 +251,7 @@ def __delitem__(self, key, default=None, strictkey=True): def has_key(self, key): assert self._assert_invariants() - if self.d.has_key(key): + if key in self.d: self.freshen(key) assert self._assert_invariants() return True @@ -270,10 +277,10 @@ def update(self, otherdict): self.clear() assert self._assert_invariants() - i = otherdict.iteritems() + i = iter(otherdict.items()) try: while len(self.d) < self.m: - (k, v,) = i.next() + (k, v,) = next(i) assert self._assert_invariants() self[k] = v assert self._assert_invariants() @@ -282,7 +289,7 @@ def update(self, otherdict): _assert(False, "Internal error -- this should never have happened since the while loop should have terminated first.") return self - for (k, v,) in otherdict.iteritems(): + for (k, v,) in otherdict.items(): assert self._assert_invariants() self[k] = v assert self._assert_invariants() @@ -401,10 +408,10 @@ def __iter__(self): return self def next(self): precondition(self.i <= len(self.c._lru), "The iterated SmallLRUCache doesn't have this many elements. Most likely this is because someone altered the contents of the LRUCache while the iteration was in progress.", self.i, self.c) - precondition(dict.has_key(self.c, self.c._lru[self.i]), "The iterated SmallLRUCache doesn't have this key. Most likely this is because someone altered the contents of the LRUCache while the iteration was in progress.", self.i, self.c._lru[self.i], self.c) if self.i == len(self.c._lru): raise StopIteration() - k = self.i + precondition(dict.__contains__(self.c, self.c._lru[self.i]), "The iterated SmallLRUCache doesn't have this key. Most likely this is because someone altered the contents of the LRUCache while the iteration was in progress.", self.i, self.c._lru[self.i], self.c) + k = self.c._lru[self.i] self.i += 1 return (k, dict.__getitem__(self.c, k),) @@ -416,10 +423,10 @@ def __iter__(self): return self def next(self): precondition(self.i <= len(self.c._lru), "The iterated SmallLRUCache doesn't have this many elements. Most likely this is because someone altered the contents of the LRUCache while the iteration was in progress.", self.i, self.c) - precondition(dict.has_key(self.c, self.c._lru[self.i]), "The iterated SmallLRUCache doesn't have this key. Most likely this is because someone altered the contents of the LRUCache while the iteration was in progress.", self.i, self.c._lru[self.i], self.c) if self.i == len(self.c._lru): raise StopIteration() - k = self.i + precondition(dict.__contains__(self.c, self.c._lru[self.i]), "The iterated SmallLRUCache doesn't have this key. Most likely this is because someone altered the contents of the LRUCache while the iteration was in progress.", self.i, self.c._lru[self.i], self.c) + k = self.c._lru[self.i] self.i += 1 return k @@ -431,27 +438,28 @@ def __iter__(self): return self def next(self): precondition(self.i <= len(self.c._lru), "The iterated SmallLRUCache doesn't have this many elements. Most likely this is because someone altered the contents of the LRUCache while the iteration was in progress.", self.i, self.c) - precondition(dict.has_key(self.c, self.c._lru[self.i]), "The iterated SmallLRUCache doesn't have this key. Most likely this is because someone altered the contents of the LRUCache while the iteration was in progress.", self.i, self.c._lru[self.i], self.c) if self.i == len(self.c._lru): raise StopIteration() - k = self.i + precondition(dict.__contains__(self.c, self.c._lru[self.i]), "The iterated SmallLRUCache doesn't have this key. Most likely this is because someone altered the contents of the LRUCache while the iteration was in progress.", self.i, self.c._lru[self.i], self.c) + k = self.c._lru[self.i] self.i += 1 return dict.__getitem__(self.c, k) def __init__(self, initialdata={}, maxsize=128): dict.__init__(self, initialdata) - self._lru = initialdata.keys() # contains keys + self._lru = list(initialdata.keys()) # contains keys self._maxsize = maxsize over = len(self) - self._maxsize if over > 0: - map(dict.__delitem__, [self]*over, self._lru[:over]) + for k in self._lru[:over]: + dict.__delitem__(self, k) del self._lru[:over] assert self._assert_invariants() def _assert_invariants(self): _assert(len(self._lru) <= self._maxsize, "Size is required to be <= maxsize.") - _assert(len(filter(lambda x: dict.has_key(self, x), self._lru)) == len(self._lru), "Each key in self._lru is required to be in dict.", filter(lambda x: not dict.has_key(self, x), self._lru), len(self._lru), self._lru, len(self), self) - _assert(len(filter(lambda x: x in self._lru, self.keys())) == len(self), "Each key in dict is required to be in self._lru.", filter(lambda x: x not in self._lru, self.keys()), len(self._lru), self._lru, len(self), self) + _assert(len([x for x in self._lru if dict.__contains__(self, x)]) == len(self._lru), "Each key in self._lru is required to be in dict.", filter(lambda x: not dict.__contains__(self, x), self._lru), len(self._lru), self._lru, len(self), self) + _assert(len([x for x in self.keys() if x in self._lru]) == len(self), "Each key in dict is required to be in self._lru.", [x for x in self.keys() if x not in self._lru], len(self._lru), self._lru, len(self), self) _assert(len(self._lru) == len(self), "internal consistency", filter(lambda x: x not in self.keys(), self._lru), len(self._lru), self._lru, len(self), self) _assert(len(self._lru) <= self._maxsize, "internal consistency", len(self._lru), self._lru, self._maxsize) return True @@ -471,7 +479,7 @@ def setdefault(self, key, default=None): def __setitem__(self, key, item=None): assert self._assert_invariants() - if dict.has_key(self, key): + if dict.__contains__(self, key): self._lru.remove(key) else: if len(self._lru) == self._maxsize: @@ -501,7 +509,7 @@ def __delitem__(self, key, default=None, strictkey=True): that key and strictkey is False """ assert self._assert_invariants() - if dict.has_key(self, key): + if dict.__contains__(self, key): val = dict.__getitem__(self, key) dict.__delitem__(self, key) self._lru.remove(key) @@ -535,7 +543,7 @@ def update(self, otherdict): while len(self) > self._maxsize: dict.popitem(self) else: - for k, v, in otherdict.iteritems(): + for k, v, in otherdict.items(): if len(self) == self._maxsize: break dict.__setitem__(self, k, v) @@ -543,8 +551,8 @@ def update(self, otherdict): assert self._assert_invariants() return self - for k in otherdict.iterkeys(): - if dict.has_key(self, k): + for k in otherdict: + if dict.__contains__(self, k): self._lru.remove(k) self._lru.extend(otherdict.keys()) dict.update(self, otherdict) @@ -559,7 +567,7 @@ def update(self, otherdict): def has_key(self, key): assert self._assert_invariants() - if dict.has_key(self, key): + if dict.__contains__(self, key): assert key in self._lru, "key: %s, self._lru: %s" % tuple(map(hr, (key, self._lru,))) self._lru.remove(key) self._lru.append(key) @@ -574,7 +582,7 @@ def refresh(self, key, strictkey=True): @param strictkey: raise a KeyError exception if key isn't present """ assert self._assert_invariants() - if not dict.has_key(self, key): + if not dict.__contains__(self, key): if strictkey: raise KeyError(key) return @@ -588,6 +596,15 @@ def popitem(self): obj = self.remove(k) return (k, obj,) + def iteritems(self): + return SmallLRUCache.ItemIterator(self) + + def iterkeys(self): + return SmallLRUCache.KeyIterator(self) + + def itervalues(self): + return SmallLRUCache.ValueIterator(self) + class LinkedListLRUCache: """ This is slower and less featureful than LRUCache. It is included @@ -608,14 +625,14 @@ def __init__(self, initialdata={}, maxsize=128): self.d = {} self.first = None self.last = None - for key, value in initialdata.iteritems(): + for key, value in initialdata.items(): self[key] = value def clear(self): self.d = {} self.first = None self.last = None def update(self, otherdict): - for (k, v,) in otherdict.iteritems(): + for (k, v,) in otherdict.items(): self[k] = v def setdefault(self, key, default=None): if not self.has_key(key): diff --git a/pyutil/dictutil.py b/pyutil/dictutil.py index 9ac11e1..5a5ff99 100644 --- a/pyutil/dictutil.py +++ b/pyutil/dictutil.py @@ -68,7 +68,7 @@ def items_sorted_by_value(self): """ @return a sequence of (key, value,) pairs sorted according to value """ - l = [(x[1], x[0],) for x in self.d.iteritems()] + l = [(x[1], x[0],) for x in self.d.items()] l.sort() return [(x[1], x[0],) for x in l] @@ -102,23 +102,23 @@ def __cmp__(self, other): except: raise le - def __eq__(self, *args, **kwargs): - return self.d.__eq__(*args, **kwargs) + def __eq__(self, other): + return self.d == other - def __ne__(self, *args, **kwargs): - return self.d.__ne__(*args, **kwargs) + def __ne__(self, other): + return self.d != other - def __gt__(self, *args, **kwargs): - return self.d.__gt__(*args, **kwargs) + def __gt__(self, other): + return self.d > other - def __ge__(self, *args, **kwargs): - return self.d.__ge__(*args, **kwargs) + def __ge__(self, other): + return self.d >= other - def __le__(self, *args, **kwargs): - return self.d.__le__(*args, **kwargs) + def __le__(self, other): + return self.d < other - def __lt__(self, *args, **kwargs): - return self.d.__lt__(*args, **kwargs) + def __lt__(self, other): + return self.d <= other def __getitem__(self, *args, **kwargs): return self.d.__getitem__(*args, **kwargs) @@ -145,19 +145,22 @@ def get(self, key, default=None): return self.d.get(key, default) def has_key(self, *args, **kwargs): - return self.d.has_key(*args, **kwargs) + return self.d.__contains__(*args, **kwargs) def items(self, *args, **kwargs): return self.d.items(*args, **kwargs) - def iteritems(self, *args, **kwargs): - return self.d.iteritems(*args, **kwargs) + if hasattr(dict, 'iteritems'): # PY2 + def iteritems(self, *args, **kwargs): + return self.d.iteritems(*args, **kwargs) - def iterkeys(self, *args, **kwargs): - return self.d.iterkeys(*args, **kwargs) + if hasattr(dict, 'iterkeys'): # PY2 + def iterkeys(self, *args, **kwargs): + return self.d.iterkeys(*args, **kwargs) - def itervalues(self, *args, **kwargs): - return self.d.itervalues(*args, **kwargs) + if hasattr(dict, 'itervalues'): # PY2 + def itervalues(self, *args, **kwargs): + return self.d.itervalues(*args, **kwargs) def keys(self, *args, **kwargs): return self.d.keys(*args, **kwargs) @@ -186,7 +189,7 @@ def merge(self, otherdict): """ Add all the values from otherdict into this dict. """ - for key, val in otherdict.iteritems(): + for key, val in otherdict.items(): self.add_num(key, val) def add_num(self, key, val, default=0): @@ -225,13 +228,13 @@ def items_sorted_by_value(self): """ @return a sequence of (key, value,) pairs sorted according to value """ - l = [(x[1], x[0],) for x in self.d.iteritems()] + l = [(x[1], x[0],) for x in self.d.items()] l.sort() return [(x[1], x[0],) for x in l] def item_with_largest_value(self): - it = self.d.iteritems() - (winner, winnerval,) = it.next() + it = iter(self.d.items()) + (winner, winnerval,) = next(it) try: while True: n, nv = it.next() @@ -272,23 +275,23 @@ def __cmp__(self, other): except: raise le - def __eq__(self, *args, **kwargs): - return self.d.__eq__(*args, **kwargs) + def __eq__(self, other): + return self.d == other - def __ne__(self, *args, **kwargs): - return self.d.__ne__(*args, **kwargs) + def __ne__(self, other): + return self.d != other - def __gt__(self, *args, **kwargs): - return self.d.__gt__(*args, **kwargs) + def __gt__(self, other): + return self.d > other - def __ge__(self, *args, **kwargs): - return self.d.__ge__(*args, **kwargs) + def __ge__(self, other): + return self.d >= other - def __le__(self, *args, **kwargs): - return self.d.__le__(*args, **kwargs) + def __le__(self, other): + return self.d < other - def __lt__(self, *args, **kwargs): - return self.d.__lt__(*args, **kwargs) + def __lt__(self, other): + return self.d <= other def __getitem__(self, *args, **kwargs): return self.d.__getitem__(*args, **kwargs) @@ -315,19 +318,22 @@ def get(self, key, default=0): return self.d.get(key, default) def has_key(self, *args, **kwargs): - return self.d.has_key(*args, **kwargs) + return self.d.__contains__(*args, **kwargs) def items(self, *args, **kwargs): return self.d.items(*args, **kwargs) - def iteritems(self, *args, **kwargs): - return self.d.iteritems(*args, **kwargs) + if hasattr(dict, 'iteritems'): # PY2 + def iteritems(self, *args, **kwargs): + return self.d.iteritems(*args, **kwargs) - def iterkeys(self, *args, **kwargs): - return self.d.iterkeys(*args, **kwargs) + if hasattr(dict, 'iterkeys'): # PY2 + def iterkeys(self, *args, **kwargs): + return self.d.iterkeys(*args, **kwargs) - def itervalues(self, *args, **kwargs): - return self.d.itervalues(*args, **kwargs) + if hasattr(dict, 'itervalues'): # PY2 + def itervalues(self, *args, **kwargs): + return self.d.itervalues(*args, **kwargs) def keys(self, *args, **kwargs): return self.d.keys(*args, **kwargs) @@ -348,7 +354,7 @@ def values(self, *args, **kwargs): return self.d.values(*args, **kwargs) def del_if_present(d, k): - if d.has_key(k): + if k in d: del d[k] class ValueOrderedDict: @@ -371,14 +377,16 @@ def __init__(self, c): self.i = 0 def __iter__(self): return self - def next(self): + def __next__(self): precondition(self.i <= len(self.c.l), "The iterated ValueOrderedDict doesn't have this many elements. Most likely this is because someone altered the contents of the ValueOrderedDict while the iteration was in progress.", self.i, self.c) - precondition((self.i == len(self.c.l)) or self.c.d.has_key(self.c.l[self.i][1]), "The iterated ValueOrderedDict doesn't have this key. Most likely this is because someone altered the contents of the ValueOrderedDict while the iteration was in progress.", self.i, (self.i < len(self.c.l)) and self.c.l[self.i], self.c) + precondition((self.i == len(self.c.l)) or self.c.l[self.i][1] in self.c.d, "The iterated ValueOrderedDict doesn't have this key. Most likely this is because someone altered the contents of the ValueOrderedDict while the iteration was in progress.", self.i, (self.i < len(self.c.l)) and self.c.l[self.i], self.c) if self.i == len(self.c.l): raise StopIteration() le = self.c.l[self.i] self.i += 1 return (le[1], le[0],) + def next(self): + return self.__next__() def iteritems(self): return ValueOrderedDict.ItemIterator(self) @@ -398,32 +406,39 @@ def __init__(self, c): self.i = 0 def __iter__(self): return self - def next(self): + def __next__(self): precondition(self.i <= len(self.c.l), "The iterated ValueOrderedDict doesn't have this many elements. Most likely this is because someone altered the contents of the ValueOrderedDict while the iteration was in progress.", self.i, self.c) - precondition((self.i == len(self.c.l)) or self.c.d.has_key(self.c.l[self.i][1]), "The iterated ValueOrderedDict doesn't have this key. Most likely this is because someone altered the contents of the ValueOrderedDict while the iteration was in progress.", self.i, (self.i < len(self.c.l)) and self.c.l[self.i], self.c) + precondition((self.i == len(self.c.l)) or self.c.l[self.i][1] in self.c.d, "The iterated ValueOrderedDict doesn't have this key. Most likely this is because someone altered the contents of the ValueOrderedDict while the iteration was in progress.", self.i, (self.i < len(self.c.l)) and self.c.l[self.i], self.c) if self.i == len(self.c.l): raise StopIteration() le = self.c.l[self.i] self.i += 1 return le[1] + def next(self): + return self.__next__() def iterkeys(self): return ValueOrderedDict.KeyIterator(self) + def __iter__(self): + return ValueOrderedDict.KeyIterator(self) + class ValueIterator: def __init__(self, c): self.c = c self.i = 0 def __iter__(self): return self - def next(self): + def __next__(self): precondition(self.i <= len(self.c.l), "The iterated ValueOrderedDict doesn't have this many elements. Most likely this is because someone altered the contents of the ValueOrderedDict while the iteration was in progress.", self.i, self.c) - precondition((self.i == len(self.c.l)) or self.c.d.has_key(self.c.l[self.i][1]), "The iterated ValueOrderedDict doesn't have this key. Most likely this is because someone altered the contents of the ValueOrderedDict while the iteration was in progress.", self.i, (self.i < len(self.c.l)) and self.c.l[self.i], self.c) + precondition((self.i == len(self.c.l)) or self.c.l[self.i][1] in self.c.d, "The iterated ValueOrderedDict doesn't have this key. Most likely this is because someone altered the contents of the ValueOrderedDict while the iteration was in progress.", self.i, (self.i < len(self.c.l)) and self.c.l[self.i], self.c) if self.i == len(self.c.l): raise StopIteration() le = self.c.l[self.i] self.i += 1 return le[0] + def next(self): + return self.__next__() def itervalues(self): return ValueOrderedDict.ValueIterator(self) @@ -460,8 +475,8 @@ def __str__(self): return "<%s %s>" % (self.__class__.__name__, self.__repr_n__(16),) def __eq__(self, other): - for (k, v,) in other.iteritems(): - if not self.d.has_key(k) or self.d[k] != v: + for (k, v,) in other.items(): + if k not in self.d or self.d[k] != v: return False return True @@ -469,19 +484,19 @@ def __ne__(self, other): return not self.__eq__(other) def _assert_invariants(self): - iter = self.l.__iter__() + it = iter(self.l) try: - oldx = iter.next() + oldx = next(it) while True: - x = iter.next() + x = next(it) # self.l is required to be sorted _assert(x >= oldx, x, oldx) # every element of self.l is required to appear in self.d - _assert(self.d.has_key(x[1]), x) + _assert(x[1] in self.d, x) oldx =x except StopIteration: pass - for (k, v,) in self.d.iteritems(): + for (k, v,) in self.d.items(): i = bisect_left(self.l, (v, k,)) while (self.l[i][0] is not v) or (self.l[i][1] is not k): i += 1 @@ -505,7 +520,7 @@ def setdefault(self, key, default=None): def __setitem__(self, key, val=None): assert self._assert_invariants() - if self.d.has_key(key): + if key in self.d: oldval = self.d[key] if oldval != val: # re-sort @@ -534,7 +549,7 @@ def remove(self, key, default=None, strictkey=True): return result def __getitem__(self, key, default=None, strictkey=True): - if not self.d.has_key(key): + if key not in self.d: if strictkey: raise KeyError(key) else: @@ -553,7 +568,7 @@ def __delitem__(self, key, default=None, strictkey=True): that key and strictkey is False """ assert self._assert_invariants() - if self.d.has_key(key): + if key in self.d: val = self.d.pop(key) i = bisect_left(self.l, (val, key,)) while (self.l[i][0] is not val) or (self.l[i][1] is not key): @@ -579,14 +594,14 @@ def update(self, otherdict): @return: self """ assert self._assert_invariants() - for (k, v,) in otherdict.iteritems(): + for (k, v,) in otherdict.items(): self.insert(k, v) assert self._assert_invariants() return self def has_key(self, key): assert self._assert_invariants() - return self.d.has_key(key) + return key in self.d def popitem(self): if not self.l: @@ -596,7 +611,7 @@ def popitem(self): return (le[1], le[0],) def pop(self, k, default=None, strictkey=False): - if not self.d.has_key(k): + if k in self.d: if strictkey: raise KeyError(k) else: diff --git a/pyutil/humanreadable.py b/pyutil/humanreadable.py index 899a12f..bec9ba4 100644 --- a/pyutil/humanreadable.py +++ b/pyutil/humanreadable.py @@ -4,6 +4,7 @@ # This file is part of pyutil; see README.rst for licensing terms. import os +import itertools try: from reprlib import Repr @@ -100,7 +101,7 @@ def repr_dict(self, obj, level): if level <= 0: return '{...}' s = '' n = len(obj) - items = obj.items()[:min(n, self.maxdict)] + items = list(itertools.islice(obj.items(), self.maxdict)) items.sort() for key, val in items: entry = self.repr1(key, level-1) + ':' + self.repr1(val, level-1) diff --git a/pyutil/odict.py b/pyutil/odict.py index 009e86a..2b70d99 100644 --- a/pyutil/odict.py +++ b/pyutil/odict.py @@ -36,14 +36,16 @@ def __init__(self, c): self.i = c.d[c.ts][1] def __iter__(self): return self - def next(self): + def __next__(self): if self.i is self.c.hs: raise StopIteration() k = self.i - precondition(self.c.d.has_key(k), "The iterated OrderedDict doesn't have the next key. Most likely this is because someone altered the contents of the OrderedDict while the iteration was in progress.", k, self.c) + precondition(k in self.c.d, "The iterated OrderedDict doesn't have the next key. Most likely this is because someone altered the contents of the OrderedDict while the iteration was in progress.", k, self.c) (v, p, n,) = self.c.d[k] self.i = p return (k, v,) + def next(self): + return self.__next__() class KeyIterator: def __init__(self, c): @@ -51,14 +53,16 @@ def __init__(self, c): self.i = c.d[c.ts][1] def __iter__(self): return self - def next(self): + def __next__(self): if self.i is self.c.hs: raise StopIteration() k = self.i - precondition(self.c.d.has_key(k), "The iterated OrderedDict doesn't have the next key. Most likely this is because someone altered the contents of the OrderedDict while the iteration was in progress.", k, self.c) + precondition(k in self.c.d, "The iterated OrderedDict doesn't have the next key. Most likely this is because someone altered the contents of the OrderedDict while the iteration was in progress.", k, self.c) (v, p, n,) = self.c.d[k] self.i = p return k + def next(self): + return self.__next__() class ValIterator: def __init__(self, c): @@ -66,13 +70,15 @@ def __init__(self, c): self.i = c.d[c.ts][1] def __iter__(self): return self - def next(self): + def __next__(self): if self.i is self.c.hs: raise StopIteration() - precondition(self.c.d.has_key(self.i), "The iterated OrderedDict doesn't have the next key. Most likely this is because someone altered the contents of the OrderedDict while the iteration was in progress.", self.i, self.c) + precondition(self.i in self.c.d, "The iterated OrderedDict doesn't have the next key. Most likely this is because someone altered the contents of the OrderedDict while the iteration was in progress.", self.i, self.c) (v, p, n,) = self.c.d[self.i] self.i = p return v + def next(self): + return self.__next__() class Sentinel: def __init__(self, msg): @@ -115,7 +121,7 @@ def _assert_invariants(self): _assert((len(self.d) > 2) == (self.d[self.hs][2] is not self.ts) == (self.d[self.ts][1] is not self.hs), "Head and tail point to something other than each other if and only if there is at least one element in the dictionary.", self.hs, self.ts, len(self.d)) foundprevsentinel = 0 foundnextsentinel = 0 - for (k, (v, p, n,)) in self.d.iteritems(): + for (k, (v, p, n,)) in self.d.items(): _assert(v not in (self.hs, self.ts,)) _assert(p is not self.ts, "A reference to the tail sentinel may not appear in prev.", k, v, p, n) _assert(n is not self.hs, "A reference to the head sentinel may not appear in next.", k, v, p, n) @@ -141,7 +147,7 @@ def _assert_invariants(self): def move_to_most_recent(self, k, strictkey=False): assert self._assert_invariants() - if not self.d.has_key(k): + if k not in self.d: if strictkey: raise KeyError(k) return @@ -211,7 +217,7 @@ def __delitem__(self, key, default=None, strictkey=True): that key and strictkey is False """ assert self._assert_invariants() - if self.d.has_key(key): + if key in self.d: node = self.d[key] # relink self.d[node[1]][2] = node[2] @@ -228,13 +234,16 @@ def __delitem__(self, key, default=None, strictkey=True): def has_key(self, key): assert self._assert_invariants() - if self.d.has_key(key): + if key in self.d: assert self._assert_invariants() return True else: assert self._assert_invariants() return False + def __contains__(self, key): + return self.has_key(key) + def clear(self): assert self._assert_invariants() self.d.clear() @@ -248,7 +257,7 @@ def update(self, otherdict): """ assert self._assert_invariants() - for (k, v,) in otherdict.iteritems(): + for (k, v,) in otherdict.items(): assert self._assert_invariants() self[k] = v assert self._assert_invariants() @@ -279,13 +288,17 @@ def keys_unsorted(self): assert self._assert_invariants() return t.keys() - def keys(self): - res = [None] * len(self) - i = 0 - for k in self.iterkeys(): - res[i] = k - i += 1 - return res + if hasattr(dict, 'iterkeys'): # PY2 + def keys(self): + res = [None] * len(self) + i = 0 + for k in self.iterkeys(): + res[i] = k + i += 1 + return res + else: # PY3 + def keys(self): + return self.__iter__() def values_unsorted(self): assert self._assert_invariants() @@ -295,21 +308,29 @@ def values_unsorted(self): assert self._assert_invariants() return map(operator.__getitem__, t.values(), [0]*len(t)) - def values(self): - res = [None] * len(self) - i = 0 - for v in self.itervalues(): - res[i] = v - i += 1 - return res - - def items(self): - res = [None] * len(self) - i = 0 - for it in self.iteritems(): - res[i] = it - i += 1 - return res + if hasattr(dict, 'itervalues'): # PY2 + def values(self): + res = [None] * len(self) + i = 0 + for v in self.itervalues(): + res[i] = v + i += 1 + return res + else: # PY3 + def values(self): + return OrderedDict.ValIterator(self) + + if hasattr(dict, 'iteritems'): # PY2 + def items(self): + res = [None] * len(self) + i = 0 + for it in self.iteritems(): + res[i] = it + i += 1 + return res + else: # PY3 + def items(self): + return OrderedDict.ItemIterator(self) def __len__(self): return len(self.d) - 2 @@ -367,7 +388,7 @@ def __iter__(self): return self def next(self): precondition(self.i <= len(self.c._lru), "The iterated SmallOrderedDict doesn't have this many elements. Most likely this is because someone altered the contents of the OrderedDict while the iteration was in progress.", self.i, self.c) - precondition(dict.has_key(self.c, self.c._lru[self.i]), "The iterated SmallOrderedDict doesn't have this key. Most likely this is because someone altered the contents of the OrderedDict while the iteration was in progress.", self.i, self.c._lru[self.i], self.c) + precondition(dict.__contains__(self.c, self.c._lru[self.i]), "The iterated SmallOrderedDict doesn't have this key. Most likely this is because someone altered the contents of the OrderedDict while the iteration was in progress.", self.i, self.c._lru[self.i], self.c) if self.i == len(self.c._lru): raise StopIteration() k = self.i @@ -382,7 +403,7 @@ def __iter__(self): return self def next(self): precondition(self.i <= len(self.c._lru), "The iterated SmallOrderedDict doesn't have this many elements. Most likely this is because someone altered the contents of the OrderedDict while the iteration was in progress.", self.i, self.c) - precondition(dict.has_key(self.c, self.c._lru[self.i]), "The iterated SmallOrderedDict doesn't have this key. Most likely this is because someone altered the contents of the OrderedDict while the iteration was in progress.", self.i, self.c._lru[self.i], self.c) + precondition(dict.__contains__(self.c, self.c._lru[self.i]), "The iterated SmallOrderedDict doesn't have this key. Most likely this is because someone altered the contents of the OrderedDict while the iteration was in progress.", self.i, self.c._lru[self.i], self.c) if self.i == len(self.c._lru): raise StopIteration() k = self.i @@ -397,7 +418,7 @@ def __iter__(self): return self def next(self): precondition(self.i <= len(self.c._lru), "The iterated SmallOrderedDict doesn't have this many elements. Most likely this is because someone altered the contents of the OrderedDict while the iteration was in progress.", self.i, self.c) - precondition(dict.has_key(self.c, self.c._lru[self.i]), "The iterated SmallOrderedDict doesn't have this key. Most likely this is because someone altered the contents of the OrderedDict while the iteration was in progress.", self.i, self.c._lru[self.i], self.c) + precondition(dict.__contains__(self.c, self.c._lru[self.i]), "The iterated SmallOrderedDict doesn't have this key. Most likely this is because someone altered the contents of the OrderedDict while the iteration was in progress.", self.i, self.c._lru[self.i], self.c) if self.i == len(self.c._lru): raise StopIteration() k = self.i @@ -416,7 +437,7 @@ def __init__(self, initialdata={}, maxsize=128): def _assert_invariants(self): _assert(len(self._lru) <= self._maxsize, "Size is required to be <= maxsize.") - _assert(len(filter(lambda x: dict.has_key(self, x), self._lru)) == len(self._lru), "Each key in self._lru is required to be in dict.", filter(lambda x: not dict.has_key(self, x), self._lru), len(self._lru), self._lru, len(self), self) + _assert(len(filter(lambda x: dict.__contains__(self, x), self._lru)) == len(self._lru), "Each key in self._lru is required to be in dict.", filter(lambda x: dict.__contains__(self, x), self._lru), len(self._lru), self._lru, len(self), self) _assert(len(filter(lambda x: x in self._lru, self.keys())) == len(self), "Each key in dict is required to be in self._lru.", filter(lambda x: x not in self._lru, self.keys()), len(self._lru), self._lru, len(self), self) _assert(len(self._lru) == len(self), "internal consistency", filter(lambda x: x not in self.keys(), self._lru), len(self._lru), self._lru, len(self), self) _assert(len(self._lru) <= self._maxsize, "internal consistency", len(self._lru), self._lru, self._maxsize) @@ -430,14 +451,14 @@ def insert(self, key, item=None): def setdefault(self, key, default=None): assert self._assert_invariants() - if not self.has_key(key): + if dict.__contains__(self, key): self[key] = default assert self._assert_invariants() return self[key] def __setitem__(self, key, item=None): assert self._assert_invariants() - if dict.has_key(self, key): + if dict.__contains__(self, key): self._lru.remove(key) else: if len(self._lru) == self._maxsize: @@ -467,7 +488,7 @@ def __delitem__(self, key, default=None, strictkey=True): that key and strictkey is False """ assert self._assert_invariants() - if dict.has_key(self, key): + if dict.__contains__(self, key): val = dict.__getitem__(self, key) dict.__delitem__(self, key) self._lru.remove(key) @@ -510,7 +531,7 @@ def update(self, otherdict): return self for k in otherdict.iterkeys(): - if dict.has_key(self, k): + if dict.__contains__(self, k): self._lru.remove(k) self._lru.extend(otherdict.keys()) dict.update(self, otherdict) @@ -525,7 +546,7 @@ def update(self, otherdict): def has_key(self, key): assert self._assert_invariants() - if dict.has_key(self, key): + if dict.__contains__(self, key): assert key in self._lru, "key: %s, self._lru: %s" % tuple(map(hr, (key, self._lru,))) self._lru.remove(key) self._lru.append(key) @@ -535,12 +556,15 @@ def has_key(self, key): assert self._assert_invariants() return False + def __contains__(self, key): + return self.has_key(key) + def refresh(self, key, strictkey=True): """ @param strictkey: raise a KeyError exception if key isn't present """ assert self._assert_invariants() - if not dict.has_key(self, key): + if not dict.__contains__(self, key): if strictkey: raise KeyError(key) return diff --git a/pyutil/test/deprecated/test_dictutil.py b/pyutil/test/deprecated/test_dictutil.py index 0887680..46523af 100644 --- a/pyutil/test/deprecated/test_dictutil.py +++ b/pyutil/test/deprecated/test_dictutil.py @@ -36,13 +36,17 @@ def _help_test_empty_dict(self, klass): d1 = klass() d2 = klass({}) - self.assertTrue(d1 == d2, "d1: %r, d2: %r" % (d1, d2,)) + self.assertTrue(d1 == d2, "klass: %s, d1: %r, d2: %r" % (klass, d1, d2,)) self.assertTrue(len(d1) == 0) self.assertTrue(len(d2) == 0) def _help_test_nonempty_dict(self, klass): - d1 = klass({'a': 1, 'b': "eggs", 3: "spam",}) - d2 = klass({'a': 1, 'b': "eggs", 3: "spam",}) + # Python 2 allowed comparison between str and int, + # therefore mixing values of different types in ValueOrderedDict + # would work. It's now a TypeError in Python 3. + #d1 = klass({'a': 1, 'b': "eggs", 3: "spam",}) + d1 = klass({'a': '1', 'b': "eggs", 3: "spam",}) + d2 = klass({'a': '1', 'b': "eggs", 3: "spam",}) self.assertTrue(d1 == d2) self.assertTrue(len(d1) == 3, "%s, %s" % (len(d1), d1,)) @@ -75,11 +79,11 @@ def _help_test_eq_but_notis(self, klass): d[fake3] = fake7 d[3] = 7 d[3] = 8 - _assert(filter(lambda x: x is 8, d.itervalues())) - _assert(filter(lambda x: x is fake7, d.itervalues())) - _assert(not filter(lambda x: x is 7, d.itervalues())) # The real 7 should have been ejected by the d[3] = 8. - _assert(filter(lambda x: x is fake3, d.iterkeys())) - _assert(filter(lambda x: x is 3, d.iterkeys())) + _assert(any(x for x in d.values() if x is 8)) + _assert(any(x for x in d.values() if x is fake7)) + _assert(not any(x for x in d.values() if x is 7)) # The real 7 should have been ejected by the d[3] = 8. + _assert(any(x for x in d if x is fake3)) + _assert(any(x for x in d if x is 3)) d[fake3] = 8 d.clear() @@ -88,11 +92,11 @@ def _help_test_eq_but_notis(self, klass): fake7 = EqButNotIs(7) d[fake3] = fake7 d[3] = 8 - _assert(filter(lambda x: x is 8, d.itervalues())) - _assert(filter(lambda x: x is fake7, d.itervalues())) - _assert(not filter(lambda x: x is 7, d.itervalues())) # The real 7 should have been ejected by the d[3] = 8. - _assert(filter(lambda x: x is fake3, d.iterkeys())) - _assert(filter(lambda x: x is 3, d.iterkeys())) + _assert(any(x for x in d.values() if x is 8)) + _assert(any(x for x in d.values() if x is fake7)) + _assert(not any(x for x in d.values() if x is 7)) # The real 7 should have been ejected by the d[3] = 8. + _assert(any(x for x in d if x is fake3)) + _assert(any(x for x in d if x is 3)) d[fake3] = 8 def test_em(self): @@ -100,13 +104,7 @@ def test_em(self): # print "name of class: ", klass for helper in (self._help_test_empty_dict, self._help_test_nonempty_dict, self._help_test_eq_but_notis,): # print "name of test func: ", helper - try: - helper(klass) - except: - (etype, evalue, realtb) = sys.exc_info() - traceback.print_exception(etype, evalue, realtb) - self.fail(evalue) - del realtb + helper(klass) def suite(): suite = unittest.makeSuite(Testy, 'test') diff --git a/pyutil/test/out_of_shape/test_cache.py b/pyutil/test/out_of_shape/test_cache.py index 9394f05..7576c01 100644 --- a/pyutil/test/out_of_shape/test_cache.py +++ b/pyutil/test/out_of_shape/test_cache.py @@ -48,7 +48,7 @@ def _benchmark_insert(self, n): MAXSIZE=n/2 d2 = self.klass(maxsize=MAXSIZE) assert len(d2) == 0 - for k, v, in self.d.iteritems(): + for k, v, in self.d.items(): d2[k] = v assert len(d2) == min(len(self.d), MAXSIZE) return True @@ -227,7 +227,7 @@ def _test_iterate_values(self, C): c = C({"a": 1}) i = c.itervalues() x = i.next() - self.assertTrue(x == 1) + self.assertEqual(x, 1) try: i.next() self.fail() # Should have gotten StopIteration exception @@ -293,7 +293,7 @@ def _test_LRU_3(self, C): def _test_LRU_full(self, C): c = C(maxsize=10) c._assert_invariants() - for i in xrange(11): + for i in range(11): c._assert_invariants() c[i] = i c._assert_invariants() @@ -325,7 +325,7 @@ def _test_LRU_full(self, C): self.assertTrue(1 not in c.values()) self.assertTrue(11 in c.values()) - for i in xrange(200): + for i in range(200): c[i] = i c._assert_invariants() self.assertTrue(199 in c.values()) @@ -334,7 +334,7 @@ def _test_LRU_full(self, C): def _test_LRU_has_key(self, C): c = C(maxsize=10) c._assert_invariants() - for i in xrange(11): + for i in range(11): c._assert_invariants() c[i] = i c._assert_invariants() @@ -355,7 +355,7 @@ def _test_LRU_has_key(self, C): def _test_LRU_not_overfull_on_idempotent_add(self, C): c = C(maxsize=10) - for i in xrange(11): + for i in range(11): c[i] = i c[1] = "spam" # Now 1 is the freshest, so 2 is the next one that would be removed *if* we went over limit. diff --git a/pyutil/test/out_of_shape/test_odict.py b/pyutil/test/out_of_shape/test_odict.py index 46014d5..05d56fd 100644 --- a/pyutil/test/out_of_shape/test_odict.py +++ b/pyutil/test/out_of_shape/test_odict.py @@ -122,7 +122,7 @@ def _test_insert_and_get_and_items(self, d) : self.assertTrue(d.get("spam2") == "eggs2") self.assertTrue(d["spam"] == "eggs") self.assertTrue(d["spam2"] == "eggs2") - self.assertEqual(d.items(), [("spam", "eggs"), ("spam2", "eggs2")], d) + self.assertEqual(list(d.items()), [("spam", "eggs"), ("spam2", "eggs2")], d) def _test_move_to_most_recent(self, d) : d.insert("spam", "eggs") @@ -133,37 +133,37 @@ def _test_move_to_most_recent(self, d) : self.assertTrue(d["spam2"] == "eggs2") self.assertEqual(d.items(), [("spam", "eggs"), ("spam2", "eggs2")]) d.move_to_most_recent("spam") - self.assertEqual(d.items(), [("spam2", "eggs2"), ("spam", "eggs")]) + self.assertEqual(list(d.items()), [("spam2", "eggs2"), ("spam", "eggs")]) def _test_insert_and_remove(self, d): d.insert('spam', "eggs") self.assertTrue(d.has_key('spam')) self.assertTrue(d.get('spam') == "eggs") self.assertTrue(d['spam'] == "eggs") - self.assertEqual(d.items(), [("spam", "eggs")]) + self.assertEqual(list(d.items()), [("spam", "eggs")]) x = d.remove('spam') self.assertTrue(x == "eggs", "x: %r" % x) self.assertTrue(not d.has_key('spam')) - self.assertEqual(d.items(), []) + self.assertEqual(list(d.items()), []) d['spam'] = "eggsy" self.assertTrue(d.has_key('spam')) self.assertTrue(d.get('spam') == "eggsy") self.assertTrue(d['spam'] == "eggsy") - self.assertEqual(d.items(), [("spam", "eggsy")]) + self.assertEqual(list(d.items()), [("spam", "eggsy")]) del d['spam'] self.assertTrue(not d.has_key('spam')) - self.assertEqual(d.items(), []) + self.assertEqual(list(d.items()), []) def _test_setdefault(self, d): d.setdefault('spam', "eggs") self.assertTrue(d.has_key('spam')) self.assertTrue(d.get('spam') == "eggs") self.assertTrue(d['spam'] == "eggs") - self.assertEqual(d.items(), [("spam", "eggs")]) + self.assertEqual(list(d.items()), [("spam", "eggs")]) x = d.remove('spam') self.assertTrue(x == "eggs", "x: %r" % x) self.assertTrue(not d.has_key('spam')) - self.assertEqual(d.items(), []) + self.assertEqual(list(d.items()), []) def _test_extracted_bound_method(self, d): insmeth = d.insert @@ -187,7 +187,7 @@ def _test_clear(self, d): d.clear() d._assert_invariants() self.assertTrue(len(d) == 0) - self.assertEqual(d.items(), []) + self.assertEqual(list(d.items()), []) def _test_update_from_dict(self, d): self.assertTrue(d._assert_invariants()) @@ -219,7 +219,7 @@ def _test_update_from_odict(self, d): self.assertTrue(d._assert_invariants()) self.assertTrue(d.get('c') == 2) self.assertTrue(d._assert_invariants()) - self.assertEqual(d.items(), [("b", 1), ("a", 0), ("c", 2)]) + self.assertEqual(list(d.items()), [("b", 1), ("a", 0), ("c", 2)]) def _test_popitem(self, C): c = C({"a": 1}) @@ -344,7 +344,7 @@ def _test_3(self, C): def _test_has_key(self, C): c = C() c._assert_invariants() - for i in xrange(11): + for i in range(11): c._assert_invariants() c[i] = i c._assert_invariants() From ef61b8fcd145f1f37f391b1f0a37a312dcf549bd Mon Sep 17 00:00:00 2001 From: Dan Callaghan Date: Mon, 27 Aug 2018 10:45:20 +1000 Subject: [PATCH 15/17] cPickle does not exist in Python 3 --- pyutil/PickleSaver.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pyutil/PickleSaver.py b/pyutil/PickleSaver.py index 26fc984..fb87f9e 100644 --- a/pyutil/PickleSaver.py +++ b/pyutil/PickleSaver.py @@ -10,7 +10,10 @@ # from the Python Standard Library import os -import cPickle as pickle +try: + import cPickle as pickle # PY2 +except ImportError: + import pickle # PY3 import warnings # from the pyutil library From cc19529ef5ff36a088699725ca80183f1abf9433 Mon Sep 17 00:00:00 2001 From: Dan Callaghan Date: Mon, 27 Aug 2018 10:45:25 +1000 Subject: [PATCH 16/17] unbound methods do not exist in Python 3 In Python 3, there are only functions and (bound) methods. The latter are instances of types.MethodType. --- pyutil/weakutil.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/pyutil/weakutil.py b/pyutil/weakutil.py index 80d75e3..0721ccc 100644 --- a/pyutil/weakutil.py +++ b/pyutil/weakutil.py @@ -7,19 +7,26 @@ # from the Python Standard Library from weakref import ref +import types # from the pyutil library from .assertutil import precondition +def is_bound_method(fn): + if hasattr(types, 'UnboundMethodType'): # PY2 + return hasattr(fn, 'im_self') + else: # PY3 + return isinstance(fn, types.MethodType) + class WeakMethod: """ Wraps a function or, more importantly, a bound method, in a way that allows a bound method's object to be GC'd """ def __init__(self, fn, callback=None): warnings.warn("deprecated", DeprecationWarning) - precondition(hasattr(fn, 'im_self'), "fn is required to be a bound method.") + precondition(is_bound_method(fn), "fn is required to be a bound method.") self._cleanupcallback = callback - self._obj = ref(fn.im_self, self.call_cleanup_cb) - self._meth = fn.im_func + self._obj = ref(fn.__self__, self.call_cleanup_cb) + self._meth = fn.__func__ def __call__(self, *args, **kws): s = self._obj() @@ -34,7 +41,7 @@ def call_cleanup_cb(self, thedeadweakref): self._cleanupcallback(self, thedeadweakref) def factory_function_name_here(o): - if hasattr(o, 'im_self'): + if is_bound_method(o): return WeakMethod(o) else: return o From 8e5ce90180f5c6dadc43723ec1719828862f2043 Mon Sep 17 00:00:00 2001 From: Dan Callaghan Date: Mon, 27 Aug 2018 10:54:55 +1000 Subject: [PATCH 17/17] xor should work with bytes not str --- pyutil/test/deprecated/test_xor.py | 10 +++++----- pyutil/xor/xor.py | 16 +++++++++++----- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/pyutil/test/deprecated/test_xor.py b/pyutil/test/deprecated/test_xor.py index 6c73b69..4678dde 100644 --- a/pyutil/test/deprecated/test_xor.py +++ b/pyutil/test/deprecated/test_xor.py @@ -10,11 +10,11 @@ # unit tests def _help_test(xf): - assert xf('\000', '\000') == '\000' - assert xf('\001', '\000') == '\001' - assert xf('\001', '\001') == '\000' - assert xf('\000\001', '\000\001') == '\000\000' - assert xf('\100\101', '\000\101') == '\100\000' + assert xf(b'\000', b'\000') == b'\000' + assert xf(b'\001', b'\000') == b'\001' + assert xf(b'\001', b'\001') == b'\000' + assert xf(b'\000\001', b'\000\001') == b'\000\000' + assert xf(b'\100\101', b'\000\101') == b'\100\000' class Testy(unittest.TestCase): def test_em(self): diff --git a/pyutil/xor/xor.py b/pyutil/xor/xor.py index e5d15b1..cca8c3b 100644 --- a/pyutil/xor/xor.py +++ b/pyutil/xor/xor.py @@ -29,12 +29,15 @@ def py_xor(str1, str2): for i in range(len(a1)): a2[i] = a2[i]^a1[i] else: - a1 = array.array('c', str1) - a2 = array.array('c', str2) + a1 = array.array('b', str1) + a2 = array.array('b', str2) for i in range(len(a1)): - a2[i] = chr(ord(a2[i])^ord(a1[i])) + a2[i] = a2[i]^a1[i] - return a2.tostring() + if hasattr(a2, 'tobytes'): # PY3 + return a2.tobytes() + else: # PY2 + return a2.tostring() def py_xor_simple(str1, str2): """ @@ -44,7 +47,10 @@ def py_xor_simple(str1, str2): warnings.warn("deprecated", DeprecationWarning) precondition(len(str1) == len(str2), "str1 and str2 are required to be of the same length.", str1=str1, str2=str2) - return ''.join(map(chr, map(operator.__xor__, map(ord, str1), map(ord, str2)))) + if bytes != str: # PY3 + return bytes(map(operator.__xor__, str1, str2)) + else: # PY2 + return ''.join(map(chr, map(operator.__xor__, map(ord, str1), map(ord, str2)))) # Now make "xor.xor()" be the best xor we've got: xor = py_xor