|
1 |
| -import sys |
| 1 | +import pprint |
2 | 2 |
|
3 | 3 | from six.moves import reprlib
|
4 | 4 |
|
5 | 5 |
|
| 6 | +def _call_and_format_exception(call, x, *args): |
| 7 | + try: |
| 8 | + # Try the vanilla repr and make sure that the result is a string |
| 9 | + return call(x, *args) |
| 10 | + except Exception as exc: |
| 11 | + exc_name = type(exc).__name__ |
| 12 | + try: |
| 13 | + exc_info = str(exc) |
| 14 | + except Exception: |
| 15 | + exc_info = "unknown" |
| 16 | + return '<[%s("%s") raised in repr()] %s object at 0x%x>' % ( |
| 17 | + exc_name, |
| 18 | + exc_info, |
| 19 | + x.__class__.__name__, |
| 20 | + id(x), |
| 21 | + ) |
| 22 | + |
| 23 | + |
6 | 24 | class SafeRepr(reprlib.Repr):
|
7 | 25 | """subclass of repr.Repr that limits the resulting size of repr()
|
8 | 26 | and includes information on exceptions raised during the call.
|
@@ -33,28 +51,20 @@ def repr_instance(self, x, level):
|
33 | 51 | return self._callhelper(repr, x)
|
34 | 52 |
|
35 | 53 | def _callhelper(self, call, x, *args):
|
36 |
| - try: |
37 |
| - # Try the vanilla repr and make sure that the result is a string |
38 |
| - s = call(x, *args) |
39 |
| - except Exception: |
40 |
| - cls, e, tb = sys.exc_info() |
41 |
| - exc_name = getattr(cls, "__name__", "unknown") |
42 |
| - try: |
43 |
| - exc_info = str(e) |
44 |
| - except Exception: |
45 |
| - exc_info = "unknown" |
46 |
| - return '<[%s("%s") raised in repr()] %s object at 0x%x>' % ( |
47 |
| - exc_name, |
48 |
| - exc_info, |
49 |
| - x.__class__.__name__, |
50 |
| - id(x), |
51 |
| - ) |
52 |
| - else: |
53 |
| - if len(s) > self.maxsize: |
54 |
| - i = max(0, (self.maxsize - 3) // 2) |
55 |
| - j = max(0, self.maxsize - 3 - i) |
56 |
| - s = s[:i] + "..." + s[len(s) - j :] |
57 |
| - return s |
| 54 | + s = _call_and_format_exception(call, x, *args) |
| 55 | + if len(s) > self.maxsize: |
| 56 | + i = max(0, (self.maxsize - 3) // 2) |
| 57 | + j = max(0, self.maxsize - 3 - i) |
| 58 | + s = s[:i] + "..." + s[len(s) - j :] |
| 59 | + return s |
| 60 | + |
| 61 | + |
| 62 | +def safeformat(obj): |
| 63 | + """return a pretty printed string for the given object. |
| 64 | + Failing __repr__ functions of user instances will be represented |
| 65 | + with a short exception info. |
| 66 | + """ |
| 67 | + return _call_and_format_exception(pprint.pformat, obj) |
58 | 68 |
|
59 | 69 |
|
60 | 70 | def saferepr(obj, maxsize=240):
|
|
0 commit comments