summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_set.py
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2006-12-29 18:49:13 (GMT)
committerRaymond Hettinger <python@rcn.com>2006-12-29 18:49:13 (GMT)
commit9cdf70399f9d61434741f1efb3eff01bfa26f656 (patch)
tree542eddd243be4b5a09120e1e6ffc846ed4c9e5ed /Lib/test/test_set.py
parent04e820443b78edebc986212e74291daba2480a12 (diff)
downloadcpython-9cdf70399f9d61434741f1efb3eff01bfa26f656.zip
cpython-9cdf70399f9d61434741f1efb3eff01bfa26f656.tar.gz
cpython-9cdf70399f9d61434741f1efb3eff01bfa26f656.tar.bz2
For sets with cyclical reprs, emit '...' instead of recursing.
Diffstat (limited to 'Lib/test/test_set.py')
-rw-r--r--Lib/test/test_set.py26
1 files changed, 26 insertions, 0 deletions
diff --git a/Lib/test/test_set.py b/Lib/test/test_set.py
index 422cc41..dedd1fb 100644
--- a/Lib/test/test_set.py
+++ b/Lib/test/test_set.py
@@ -21,6 +21,11 @@ class BadCmp:
def __cmp__(self, other):
raise RuntimeError
+class ReprWrapper:
+ 'Used to test self-referential repr() calls'
+ def __repr__(self):
+ return repr(self.value)
+
class TestJointOps(unittest.TestCase):
# Tests common to both set and frozenset
@@ -244,6 +249,27 @@ class TestJointOps(unittest.TestCase):
self.assertRaises(RuntimeError, s.discard, BadCmp())
self.assertRaises(RuntimeError, s.remove, BadCmp())
+ def test_cyclical_repr(self):
+ w = ReprWrapper()
+ s = self.thetype([w])
+ w.value = s
+ name = repr(s).partition('(')[0] # strip class name from repr string
+ self.assertEqual(repr(s), '%s([%s(...)])' % (name, name))
+
+ def test_cyclical_print(self):
+ w = ReprWrapper()
+ s = self.thetype([w])
+ w.value = s
+ try:
+ fo = open(test_support.TESTFN, "wb")
+ print >> fo, s,
+ fo.close()
+ fo = open(test_support.TESTFN, "rb")
+ self.assertEqual(fo.read(), repr(s))
+ finally:
+ fo.close()
+ os.remove(test_support.TESTFN)
+
class TestSet(TestJointOps):
thetype = set