##// END OF EJS Templates
protect against broken repr in lib.pretty
MinRK -
Show More
@@ -202,7 +202,7 b' class FormatterABC(with_metaclass(abc.ABCMeta, object)):'
202 """
202 """
203 try:
203 try:
204 return repr(obj)
204 return repr(obj)
205 except TypeError:
205 except Exception:
206 return None
206 return None
207
207
208
208
@@ -465,10 +465,7 b' class PlainTextFormatter(BaseFormatter):'
465 def __call__(self, obj):
465 def __call__(self, obj):
466 """Compute the pretty representation of the object."""
466 """Compute the pretty representation of the object."""
467 if not self.pprint:
467 if not self.pprint:
468 try:
468 return pretty._safe_repr(obj)
469 return repr(obj)
470 except TypeError:
471 return ''
472 else:
469 else:
473 # This uses use StringIO, as cStringIO doesn't handle unicode.
470 # This uses use StringIO, as cStringIO doesn't handle unicode.
474 stream = StringIO()
471 stream = StringIO()
@@ -125,6 +125,13 b" __all__ = ['pretty', 'pprint', 'PrettyPrinter', 'RepresentationPrinter',"
125
125
126 _re_pattern_type = type(re.compile(''))
126 _re_pattern_type = type(re.compile(''))
127
127
128 def _safe_repr(obj):
129 """Don't trust repr to not be broken."""
130 try:
131 return repr(obj)
132 except Exception as e:
133 return "<repr(obj) failed: %s>" % e
134
128
135
129 def pretty(obj, verbose=False, max_width=79, newline='\n'):
136 def pretty(obj, verbose=False, max_width=79, newline='\n'):
130 """
137 """
@@ -495,7 +502,7 b' def _default_pprint(obj, p, cycle):'
495 klass = getattr(obj, '__class__', None) or type(obj)
502 klass = getattr(obj, '__class__', None) or type(obj)
496 if getattr(klass, '__repr__', None) not in _baseclass_reprs:
503 if getattr(klass, '__repr__', None) not in _baseclass_reprs:
497 # A user-provided repr. Find newlines and replace them with p.break_()
504 # A user-provided repr. Find newlines and replace them with p.break_()
498 output = repr(obj)
505 output = _safe_repr(obj)
499 for idx,output_line in enumerate(output.splitlines()):
506 for idx,output_line in enumerate(output.splitlines()):
500 if idx:
507 if idx:
501 p.break_()
508 p.break_()
@@ -673,7 +680,7 b' def _type_pprint(obj, p, cycle):'
673
680
674 def _repr_pprint(obj, p, cycle):
681 def _repr_pprint(obj, p, cycle):
675 """A pprint that just redirects to the normal repr function."""
682 """A pprint that just redirects to the normal repr function."""
676 p.text(repr(obj))
683 p.text(_safe_repr(obj))
677
684
678
685
679 def _function_pprint(obj, p, cycle):
686 def _function_pprint(obj, p, cycle):
General Comments 0
You need to be logged in to leave comments. Login now