[svn r57535] extending hacks for a safe representation of objects
to also work for broken __repr__s on newstyle classes. --HG-- branch : trunk
This commit is contained in:
parent
d5e61e470d
commit
5f666c99b7
|
@ -153,7 +153,7 @@ class FormattedExcinfo(object):
|
|||
else:
|
||||
# This formatting could all be handled by the _repr() function, which is
|
||||
# only repr.Repr in disguise, so is very configurable.
|
||||
str_repr = safe_repr._repr(value)
|
||||
str_repr = self._saferepr(value)
|
||||
#if len(str_repr) < 70 or not isinstance(value,
|
||||
# (list, tuple, dict)):
|
||||
lines.append("%-10s = %s" %(name, str_repr))
|
||||
|
|
|
@ -30,10 +30,17 @@ class SafeRepr(repr.Repr):
|
|||
# Do we need a commandline switch for this?
|
||||
self.maxstring = 240 # 3 * 80 chars
|
||||
self.maxother = 160 # 2 * 80 chars
|
||||
|
||||
def repr(self, x):
|
||||
return self._callhelper(repr.Repr.repr, self, x)
|
||||
|
||||
def repr_instance(self, x, level):
|
||||
return self._callhelper(__builtin__.repr, x)
|
||||
|
||||
def _callhelper(self, call, x, *args):
|
||||
try:
|
||||
# Try the vanilla repr and make sure that the result is a string
|
||||
s = str(__builtin__.repr(x))
|
||||
s = call(x, *args)
|
||||
except (KeyboardInterrupt, MemoryError, SystemExit):
|
||||
raise
|
||||
except Exception ,e:
|
||||
|
@ -60,4 +67,5 @@ class SafeRepr(repr.Repr):
|
|||
s = s[:i] + '...' + s[len(s)-j:]
|
||||
return s
|
||||
|
||||
|
||||
_repr = SafeRepr().repr
|
||||
|
|
|
@ -35,5 +35,14 @@ def test_big_repr():
|
|||
assert len(safe_repr._repr(range(1000))) <= \
|
||||
len('[' + safe_repr.SafeRepr().maxlist * "1000" + ']')
|
||||
|
||||
def test_repr_on_newstyle():
|
||||
class Function(object):
|
||||
def __repr__(self):
|
||||
return "<%s>" %(self.name)
|
||||
try:
|
||||
s = safe_repr._repr(Function())
|
||||
except Exception, e:
|
||||
py.test.fail("saferepr failed for newstyle class")
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue