Fix test failures for repr.py.

But shouldn't we kill this module?  How many pprint clones do we need?
This commit is contained in:
Guido van Rossum 2006-04-20 16:07:39 +00:00
parent 08a1a9fac7
commit a48a3b42dd
2 changed files with 10 additions and 16 deletions

View File

@ -18,7 +18,7 @@ class Repr:
self.maxdeque = 6
self.maxstring = 30
self.maxlong = 40
self.maxother = 20
self.maxother = 30
def repr(self, x):
return self.repr1(x, self.maxlevel)
@ -31,12 +31,7 @@ class Repr:
if hasattr(self, 'repr_' + typename):
return getattr(self, 'repr_' + typename)(x, level)
else:
s = __builtin__.repr(x)
if len(s) > self.maxother:
i = max(0, (self.maxother-3)//2)
j = max(0, self.maxother-3-i)
s = s[:i] + '...' + s[len(s)-j:]
return s
return self.repr_instance(x, level)
def _repr_iterable(self, x, level, left, right, maxiter, trail=''):
n = len(x)
@ -112,9 +107,9 @@ class Repr:
# exceptions -- then make up something
except:
return '<%s instance at %x>' % (x.__class__.__name__, id(x))
if len(s) > self.maxstring:
i = max(0, (self.maxstring-3)//2)
j = max(0, self.maxstring-3-i)
if len(s) > self.maxother:
i = max(0, (self.maxother-3)//2)
j = max(0, self.maxother-3-i)
s = s[:i] + '...' + s[len(s)-j:]
return s

View File

@ -111,7 +111,7 @@ class ReprTests(unittest.TestCase):
s = r(ClassWithFailingRepr)
self.failUnless(s.startswith("<class "))
self.failUnless(s.endswith(">"))
self.failUnless(s.find("...") == 8)
self.failUnless(s.find("...") in [12, 13])
def test_file(self):
fp = open(unittest.__file__)
@ -249,8 +249,7 @@ class bar:
''')
from areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation import bar
# Module name may be prefixed with "test.", depending on how run.
self.failUnless(repr(bar.bar).startswith(
"<class %s.bar at 0x" % bar.__name__))
self.assertEquals(repr(bar.bar), "<class '%s.bar'>" % bar.__name__)
def test_instance(self):
touch(os.path.join(self.subpkgname, 'baz'+os.extsep+'py'), '''\
@ -260,7 +259,7 @@ class baz:
from areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation import baz
ibaz = baz.baz()
self.failUnless(repr(ibaz).startswith(
"<%s.baz instance at 0x" % baz.__name__))
"<%s.baz object at 0x" % baz.__name__))
def test_method(self):
eq = self.assertEquals
@ -275,7 +274,7 @@ class aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
# Bound method next
iqux = qux.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()
self.failUnless(repr(iqux.amethod).startswith(
'<bound method aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.amethod of <%s.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa instance at 0x' \
'<bound method aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.amethod of <%s.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa object at 0x' \
% (qux.__name__,) ))
def test_builtin_function(self):
@ -286,7 +285,7 @@ class ClassWithRepr:
def __init__(self, s):
self.s = s
def __repr__(self):
return "ClassWithLongRepr(%r)" % self.s
return "ClassWithRepr(%r)" % self.s
class ClassWithFailingRepr: