diff options
author | Raymond Hettinger <python@rcn.com> | 2006-12-30 04:01:17 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2006-12-30 04:01:17 (GMT) |
commit | 5399910eba1ef3580aebf49482b1367f14fa6c48 (patch) | |
tree | d853caaf0b19b05ecef8330a704a64ce657bde37 /Lib | |
parent | a186ee22c0f30a136f56ae48fc8c56579045c05e (diff) | |
download | cpython-5399910eba1ef3580aebf49482b1367f14fa6c48.zip cpython-5399910eba1ef3580aebf49482b1367f14fa6c48.tar.gz cpython-5399910eba1ef3580aebf49482b1367f14fa6c48.tar.bz2 |
For sets with cyclical reprs, emit an ellipsis instead of infinitely recursing.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_set.py | 26 |
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 |