diff options
author | Michael Foord <fuzzyman@voidspace.org.uk> | 2010-06-05 12:58:39 (GMT) |
---|---|---|
committer | Michael Foord <fuzzyman@voidspace.org.uk> | 2010-06-05 12:58:39 (GMT) |
commit | 674648e3f2020e30bf73fed7ac77625d6f159de2 (patch) | |
tree | 65278bf23510fc853a2c1b4e230e42b7183925f5 /Lib/unittest/util.py | |
parent | 77acee95677152af7417ad47148cf24feb4c947c (diff) | |
download | cpython-674648e3f2020e30bf73fed7ac77625d6f159de2.zip cpython-674648e3f2020e30bf73fed7ac77625d6f159de2.tar.gz cpython-674648e3f2020e30bf73fed7ac77625d6f159de2.tar.bz2 |
unittest.TestCase.assertDictEqual and assertMultilineEqual provide better default failure messages in the event of long diffs.
Diffstat (limited to 'Lib/unittest/util.py')
-rw-r--r-- | Lib/unittest/util.py | 12 |
1 files changed, 8 insertions, 4 deletions
diff --git a/Lib/unittest/util.py b/Lib/unittest/util.py index 7d90ddf..d201657 100644 --- a/Lib/unittest/util.py +++ b/Lib/unittest/util.py @@ -2,12 +2,16 @@ __unittest = True - -def safe_repr(obj): +_MAX_LENGTH = 80 +def safe_repr(obj, short=False): try: - return repr(obj) + result = repr(obj) except Exception: - return object.__repr__(obj) + result = object.__repr__(obj) + if not short or len(result) < _MAX_LENGTH: + return result + return result[:_MAX_LENGTH] + ' [truncated]...' + def strclass(cls): return "%s.%s" % (cls.__module__, cls.__name__) |