mirror of
https://github.com/python/cpython.git
synced 2025-07-30 14:44:10 +00:00
For sets with cyclical reprs, emit an ellipsis instead of infinitely recursing.
This commit is contained in:
parent
a186ee22c0
commit
5399910eba
2 changed files with 50 additions and 4 deletions
|
@ -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
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue