Deleting cyclic object comparison.
SF patch 825639
http://mail.python.org/pipermail/python-dev/2003-October/039445.html
diff --git a/Lib/test/pickletester.py b/Lib/test/pickletester.py
index cf1bb37..6e6d97d 100644
--- a/Lib/test/pickletester.py
+++ b/Lib/test/pickletester.py
@@ -424,9 +424,8 @@
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 @@
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 @@
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 @@
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 6521634..e84cfbd 100644
--- a/Lib/test/test_builtin.py
+++ b/Lib/test/test_builtin.py
@@ -167,16 +167,16 @@
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 3d44304..6e32ddd 100644
--- a/Lib/test/test_copy.py
+++ b/Lib/test/test_copy.py
@@ -272,10 +272,10 @@
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 @@
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 @@
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 5ade8ed..006b152 100644
--- a/Lib/test/test_richcmp.py
+++ b/Lib/test/test_richcmp.py
@@ -224,57 +224,36 @@
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):