diff options
author | Armin Rigo <arigo@tunes.org> | 2003-10-28 12:05:48 +0000 |
---|---|---|
committer | Armin Rigo <arigo@tunes.org> | 2003-10-28 12:05:48 +0000 |
commit | 8cbe70f474e47964f45a14a4e265a30ce2fedac9 (patch) | |
tree | 27ae29f45de7f7075c4b4a1ee153496b21c28acb /Lib | |
parent | f426ab0fb21e06f226e83a2f59ec7c1e8d523661 (diff) | |
download | cpython-8cbe70f474e47964f45a14a4e265a30ce2fedac9.tar.gz |
Deleting cyclic object comparison.
SF patch 825639
http://mail.python.org/pipermail/python-dev/2003-October/039445.html
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/pickletester.py | 25 | ||||
-rw-r--r-- | Lib/test/test_builtin.py | 12 | ||||
-rw-r--r-- | Lib/test/test_copy.py | 12 | ||||
-rw-r--r-- | Lib/test/test_richcmp.py | 63 |
4 files changed, 43 insertions, 69 deletions
diff --git a/Lib/test/pickletester.py b/Lib/test/pickletester.py index cf1bb373ab..6e6d97de5c 100644 --- a/Lib/test/pickletester.py +++ b/Lib/test/pickletester.py @@ -424,9 +424,8 @@ class AbstractPickleTests(unittest.TestCase): for proto in protocols: s = self.dumps(l, proto) x = self.loads(s) - self.assertEqual(x, l) - self.assertEqual(x, x[0]) - self.assertEqual(id(x), id(x[0])) + self.assertEqual(len(x), 1) + self.assert_(x is x[0]) def test_recursive_dict(self): d = {} @@ -434,9 +433,8 @@ class AbstractPickleTests(unittest.TestCase): for proto in protocols: s = self.dumps(d, proto) x = self.loads(s) - self.assertEqual(x, d) - self.assertEqual(x[1], x) - self.assertEqual(id(x[1]), id(x)) + self.assertEqual(x.keys(), [1]) + self.assert_(x[1] is x) def test_recursive_inst(self): i = C() @@ -444,9 +442,8 @@ class AbstractPickleTests(unittest.TestCase): for proto in protocols: s = self.dumps(i, 2) x = self.loads(s) - self.assertEqual(x, i) - self.assertEqual(x.attr, x) - self.assertEqual(id(x.attr), id(x)) + self.assertEqual(dir(x), dir(i)) + self.assert_(x.attr is x) def test_recursive_multi(self): l = [] @@ -457,12 +454,10 @@ class AbstractPickleTests(unittest.TestCase): for proto in protocols: s = self.dumps(l, proto) x = self.loads(s) - self.assertEqual(x, l) - self.assertEqual(x[0], i) - self.assertEqual(x[0].attr, d) - self.assertEqual(x[0].attr[1], x) - self.assertEqual(x[0].attr[1][0], i) - self.assertEqual(x[0].attr[1][0].attr, d) + self.assertEqual(len(x), 1) + self.assertEqual(dir(x[0]), dir(i)) + self.assertEqual(x[0].attr.keys(), [1]) + self.assert_(x[0].attr[1] is x) def test_garyp(self): self.assertRaises(self.error, self.loads, 'garyp') diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py index 652163411e..e84cfbd50e 100644 --- a/Lib/test/test_builtin.py +++ b/Lib/test/test_builtin.py @@ -167,16 +167,16 @@ class BuiltinTest(unittest.TestCase): self.assertEqual(cmp(-1, 1), -1) self.assertEqual(cmp(1, -1), 1) self.assertEqual(cmp(1, 1), 0) - # verify that circular objects are handled + # verify that circular objects are not handled a = []; a.append(a) b = []; b.append(b) from UserList import UserList c = UserList(); c.append(c) - self.assertEqual(cmp(a, b), 0) - self.assertEqual(cmp(b, c), 0) - self.assertEqual(cmp(c, a), 0) - self.assertEqual(cmp(a, c), 0) - # okay, now break the cycles + self.assertRaises(RuntimeError, cmp, a, b) + self.assertRaises(RuntimeError, cmp, b, c) + self.assertRaises(RuntimeError, cmp, c, a) + self.assertRaises(RuntimeError, cmp, a, c) + # okay, now break the cycles a.pop(); b.pop(); c.pop() self.assertRaises(TypeError, cmp) diff --git a/Lib/test/test_copy.py b/Lib/test/test_copy.py index 3d44304db9..6e32ddd88f 100644 --- a/Lib/test/test_copy.py +++ b/Lib/test/test_copy.py @@ -272,10 +272,10 @@ class TestCopy(unittest.TestCase): x = [] x.append(x) y = copy.deepcopy(x) - self.assertEqual(y, x) + self.assertRaises(RuntimeError, cmp, y, x) self.assert_(y is not x) - self.assert_(y[0] is not x[0]) - self.assert_(y is y[0]) + self.assert_(y[0] is y) + self.assertEqual(len(y), 1) def test_deepcopy_tuple(self): x = ([1, 2], 3) @@ -288,7 +288,7 @@ class TestCopy(unittest.TestCase): x = ([],) x[0].append(x) y = copy.deepcopy(x) - self.assertEqual(y, x) + self.assertRaises(RuntimeError, cmp, y, x) self.assert_(y is not x) self.assert_(y[0] is not x[0]) self.assert_(y[0][0] is y) @@ -304,10 +304,10 @@ class TestCopy(unittest.TestCase): x = {} x['foo'] = x y = copy.deepcopy(x) - self.assertEqual(y, x) + self.assertRaises(RuntimeError, cmp, y, x) self.assert_(y is not x) self.assert_(y['foo'] is y) - self.assertEqual(y, {'foo': y}) + self.assertEqual(len(y), 1) def test_deepcopy_keepalive(self): memo = {} diff --git a/Lib/test/test_richcmp.py b/Lib/test/test_richcmp.py index 5ade8ede56..006b1528c9 100644 --- a/Lib/test/test_richcmp.py +++ b/Lib/test/test_richcmp.py @@ -224,57 +224,36 @@ class MiscTest(unittest.TestCase): self.assertRaises(Exc, func, Bad()) def test_recursion(self): - # Check comparison for recursive objects + # Check that comparison for recursive objects fails gracefully from UserList import UserList - a = UserList(); a.append(a) - b = UserList(); b.append(b) - - self.assert_(a == b) - self.assert_(not a != b) - a.append(1) - self.assert_(a == a[0]) - self.assert_(not a != a[0]) - self.assert_(a != b) - self.assert_(not a == b) - b.append(0) - self.assert_(a != b) - self.assert_(not a == b) - a[1] = -1 - self.assert_(a != b) - self.assert_(not a == b) - a = UserList() b = UserList() a.append(b) b.append(a) - self.assert_(a == b) - self.assert_(not a != b) + self.assertRaises(RuntimeError, operator.eq, a, b) + self.assertRaises(RuntimeError, operator.ne, a, b) + self.assertRaises(RuntimeError, operator.lt, a, b) + self.assertRaises(RuntimeError, operator.le, a, b) + self.assertRaises(RuntimeError, operator.gt, a, b) + self.assertRaises(RuntimeError, operator.ge, a, b) b.append(17) + # Even recursive lists of different lengths are different, + # but they cannot be ordered + self.assert_(not (a == b)) self.assert_(a != b) - self.assert_(not a == b) + self.assertRaises(RuntimeError, operator.lt, a, b) + self.assertRaises(RuntimeError, operator.le, a, b) + self.assertRaises(RuntimeError, operator.gt, a, b) + self.assertRaises(RuntimeError, operator.ge, a, b) a.append(17) - self.assert_(a == b) - self.assert_(not a != b) - - def test_recursion2(self): - # This test exercises the circular structure handling code - # in PyObject_RichCompare() - class Weird(object): - def __eq__(self, other): - return self != other - def __ne__(self, other): - return self == other - def __lt__(self, other): - return self > other - def __gt__(self, other): - return self < other - - self.assert_(Weird() == Weird()) - self.assert_(not (Weird() != Weird())) - - for op in opmap["lt"]: - self.assertRaises(ValueError, op, Weird(), Weird()) + self.assertRaises(RuntimeError, operator.eq, a, b) + self.assertRaises(RuntimeError, operator.ne, a, b) + a.insert(0, 11) + b.insert(0, 12) + self.assert_(not (a == b)) + self.assert_(a != b) + self.assert_(a < b) class DictTest(unittest.TestCase): |