--- a/Lib/unittest/util.py Sat Jun 25 15:03:52 2011 +0200 +++ b/Lib/unittest/util.py Sun Jun 26 17:12:16 2011 +0200 @@ -5,14 +5,31 @@ __unittest = True _MAX_LENGTH = 80 -def safe_repr(obj, short=False): +def safe_repr(obj, short=False, max_length=None): try: result = repr(obj) except Exception: result = object.__repr__(obj) - if not short or len(result) < _MAX_LENGTH: + + len_result = len(result) + truncate_size = max_length or _MAX_LENGTH + if not short or len_result <= truncate_size: return result - return result[:_MAX_LENGTH] + ' [truncated]...' + + if truncate_size <= _MAX_LENGTH: + result = result[:truncate_size] + else: + # 0 _MAX_LENGTH truncate_size len(result) + # begin trunc end + # <-----------------------------------------------> + # begin ... trunc + # <--------------> + result = (result[:_MAX_LENGTH // 2] + '...' + + result[-_MAX_LENGTH // 2:truncate_size]) + + if truncate_size < len_result: + return result + ' [truncated]...' + return result def strclass(cls): return "%s.%s" % (cls.__module__, cls.__name__)