Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1 | """Redo the builtin repr() (representation) but with limits on most sizes.""" |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 2 | |
Raymond Hettinger | 98a5f3f | 2010-09-13 21:36:00 +0000 | [diff] [blame] | 3 | __all__ = ["Repr", "repr", "recursive_repr"] |
Skip Montanaro | 0de6580 | 2001-02-15 22:15:14 +0000 | [diff] [blame] | 4 | |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 5 | import builtins |
Raymond Hettinger | ba6cd36 | 2004-05-21 10:00:15 +0000 | [diff] [blame] | 6 | from itertools import islice |
Raymond Hettinger | 98a5f3f | 2010-09-13 21:36:00 +0000 | [diff] [blame] | 7 | try: |
Antoine Pitrou | bb0ad4c | 2011-10-11 18:51:53 +0200 | [diff] [blame] | 8 | from _thread import get_ident |
Brett Cannon | cd171c8 | 2013-07-04 17:43:24 -0400 | [diff] [blame] | 9 | except ImportError: |
Raymond Hettinger | 98a5f3f | 2010-09-13 21:36:00 +0000 | [diff] [blame] | 10 | from _dummy_thread import get_ident |
| 11 | |
| 12 | def recursive_repr(fillvalue='...'): |
| 13 | 'Decorator to make a repr function return fillvalue for a recursive call' |
| 14 | |
| 15 | def decorating_function(user_function): |
| 16 | repr_running = set() |
| 17 | |
| 18 | def wrapper(self): |
| 19 | key = id(self), get_ident() |
| 20 | if key in repr_running: |
| 21 | return fillvalue |
| 22 | repr_running.add(key) |
| 23 | try: |
| 24 | result = user_function(self) |
| 25 | finally: |
| 26 | repr_running.discard(key) |
| 27 | return result |
| 28 | |
| 29 | # Can't use functools.wraps() here because of bootstrap issues |
| 30 | wrapper.__module__ = getattr(user_function, '__module__') |
| 31 | wrapper.__doc__ = getattr(user_function, '__doc__') |
| 32 | wrapper.__name__ = getattr(user_function, '__name__') |
Serhiy Storchaka | b3b366d | 2016-04-26 09:30:44 +0300 | [diff] [blame] | 33 | wrapper.__qualname__ = getattr(user_function, '__qualname__') |
Raymond Hettinger | 6ef18f4 | 2011-01-23 21:33:36 +0000 | [diff] [blame] | 34 | wrapper.__annotations__ = getattr(user_function, '__annotations__', {}) |
Raymond Hettinger | 98a5f3f | 2010-09-13 21:36:00 +0000 | [diff] [blame] | 35 | return wrapper |
| 36 | |
| 37 | return decorating_function |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 38 | |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 39 | class Repr: |
Raymond Hettinger | ba6cd36 | 2004-05-21 10:00:15 +0000 | [diff] [blame] | 40 | |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 41 | def __init__(self): |
| 42 | self.maxlevel = 6 |
| 43 | self.maxtuple = 6 |
| 44 | self.maxlist = 6 |
Tim Peters | 6ee0480 | 2003-02-05 18:29:34 +0000 | [diff] [blame] | 45 | self.maxarray = 5 |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 46 | self.maxdict = 4 |
Raymond Hettinger | ba6cd36 | 2004-05-21 10:00:15 +0000 | [diff] [blame] | 47 | self.maxset = 6 |
| 48 | self.maxfrozenset = 6 |
Raymond Hettinger | 1453e4a | 2004-05-21 23:01:18 +0000 | [diff] [blame] | 49 | self.maxdeque = 6 |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 50 | self.maxstring = 30 |
| 51 | self.maxlong = 40 |
Guido van Rossum | a48a3b4 | 2006-04-20 16:07:39 +0000 | [diff] [blame] | 52 | self.maxother = 30 |
Raymond Hettinger | ba6cd36 | 2004-05-21 10:00:15 +0000 | [diff] [blame] | 53 | |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 54 | def repr(self, x): |
| 55 | return self.repr1(x, self.maxlevel) |
Raymond Hettinger | ba6cd36 | 2004-05-21 10:00:15 +0000 | [diff] [blame] | 56 | |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 57 | def repr1(self, x, level): |
Guido van Rossum | a995c91 | 2001-08-09 18:56:27 +0000 | [diff] [blame] | 58 | typename = type(x).__name__ |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 59 | if ' ' in typename: |
Eric S. Raymond | 6b71e74 | 2001-02-09 08:56:30 +0000 | [diff] [blame] | 60 | parts = typename.split() |
| 61 | typename = '_'.join(parts) |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 62 | if hasattr(self, 'repr_' + typename): |
| 63 | return getattr(self, 'repr_' + typename)(x, level) |
| 64 | else: |
Guido van Rossum | a48a3b4 | 2006-04-20 16:07:39 +0000 | [diff] [blame] | 65 | return self.repr_instance(x, level) |
Raymond Hettinger | ba6cd36 | 2004-05-21 10:00:15 +0000 | [diff] [blame] | 66 | |
Raymond Hettinger | 1453e4a | 2004-05-21 23:01:18 +0000 | [diff] [blame] | 67 | def _repr_iterable(self, x, level, left, right, maxiter, trail=''): |
Raymond Hettinger | ba6cd36 | 2004-05-21 10:00:15 +0000 | [diff] [blame] | 68 | n = len(x) |
| 69 | if level <= 0 and n: |
| 70 | s = '...' |
| 71 | else: |
| 72 | newlevel = level - 1 |
| 73 | repr1 = self.repr1 |
| 74 | pieces = [repr1(elem, newlevel) for elem in islice(x, maxiter)] |
| 75 | if n > maxiter: pieces.append('...') |
| 76 | s = ', '.join(pieces) |
Raymond Hettinger | 1453e4a | 2004-05-21 23:01:18 +0000 | [diff] [blame] | 77 | if n == 1 and trail: right = trail + right |
Raymond Hettinger | ba6cd36 | 2004-05-21 10:00:15 +0000 | [diff] [blame] | 78 | return '%s%s%s' % (left, s, right) |
| 79 | |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 80 | def repr_tuple(self, x, level): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 81 | return self._repr_iterable(x, level, '(', ')', self.maxtuple, ',') |
Raymond Hettinger | ba6cd36 | 2004-05-21 10:00:15 +0000 | [diff] [blame] | 82 | |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 83 | def repr_list(self, x, level): |
Raymond Hettinger | ba6cd36 | 2004-05-21 10:00:15 +0000 | [diff] [blame] | 84 | return self._repr_iterable(x, level, '[', ']', self.maxlist) |
Tim Peters | 6ee0480 | 2003-02-05 18:29:34 +0000 | [diff] [blame] | 85 | |
| 86 | def repr_array(self, x, level): |
Raymond Hettinger | a34cd0c | 2014-11-15 10:58:58 -0800 | [diff] [blame] | 87 | if not x: |
| 88 | return "array('%s')" % x.typecode |
Tim Peters | 6ee0480 | 2003-02-05 18:29:34 +0000 | [diff] [blame] | 89 | header = "array('%s', [" % x.typecode |
Raymond Hettinger | ba6cd36 | 2004-05-21 10:00:15 +0000 | [diff] [blame] | 90 | return self._repr_iterable(x, level, header, '])', self.maxarray) |
| 91 | |
| 92 | def repr_set(self, x, level): |
Raymond Hettinger | ffd842e | 2014-11-09 22:30:36 -0800 | [diff] [blame] | 93 | if not x: |
| 94 | return 'set()' |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 95 | x = _possibly_sorted(x) |
Raymond Hettinger | ffd842e | 2014-11-09 22:30:36 -0800 | [diff] [blame] | 96 | return self._repr_iterable(x, level, '{', '}', self.maxset) |
Raymond Hettinger | ba6cd36 | 2004-05-21 10:00:15 +0000 | [diff] [blame] | 97 | |
| 98 | def repr_frozenset(self, x, level): |
Raymond Hettinger | ffd842e | 2014-11-09 22:30:36 -0800 | [diff] [blame] | 99 | if not x: |
| 100 | return 'frozenset()' |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 101 | x = _possibly_sorted(x) |
Raymond Hettinger | ffd842e | 2014-11-09 22:30:36 -0800 | [diff] [blame] | 102 | return self._repr_iterable(x, level, 'frozenset({', '})', |
Raymond Hettinger | ba6cd36 | 2004-05-21 10:00:15 +0000 | [diff] [blame] | 103 | self.maxfrozenset) |
Tim Peters | 6ee0480 | 2003-02-05 18:29:34 +0000 | [diff] [blame] | 104 | |
Raymond Hettinger | 1453e4a | 2004-05-21 23:01:18 +0000 | [diff] [blame] | 105 | def repr_deque(self, x, level): |
| 106 | return self._repr_iterable(x, level, 'deque([', '])', self.maxdeque) |
| 107 | |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 108 | def repr_dict(self, x, level): |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 109 | n = len(x) |
| 110 | if n == 0: return '{}' |
| 111 | if level <= 0: return '{...}' |
Raymond Hettinger | ba6cd36 | 2004-05-21 10:00:15 +0000 | [diff] [blame] | 112 | newlevel = level - 1 |
| 113 | repr1 = self.repr1 |
| 114 | pieces = [] |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 115 | for key in islice(_possibly_sorted(x), self.maxdict): |
Raymond Hettinger | ba6cd36 | 2004-05-21 10:00:15 +0000 | [diff] [blame] | 116 | keyrepr = repr1(key, newlevel) |
| 117 | valrepr = repr1(x[key], newlevel) |
| 118 | pieces.append('%s: %s' % (keyrepr, valrepr)) |
| 119 | if n > self.maxdict: pieces.append('...') |
| 120 | s = ', '.join(pieces) |
| 121 | return '{%s}' % (s,) |
| 122 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 123 | def repr_str(self, x, level): |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 124 | s = builtins.repr(x[:self.maxstring]) |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 125 | if len(s) > self.maxstring: |
Guido van Rossum | 54e54c6 | 2001-09-04 19:14:14 +0000 | [diff] [blame] | 126 | i = max(0, (self.maxstring-3)//2) |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 127 | j = max(0, self.maxstring-3-i) |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 128 | s = builtins.repr(x[:i] + x[len(x)-j:]) |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 129 | s = s[:i] + '...' + s[len(s)-j:] |
| 130 | return s |
Raymond Hettinger | ba6cd36 | 2004-05-21 10:00:15 +0000 | [diff] [blame] | 131 | |
Guido van Rossum | 4c9695a | 2007-01-15 00:14:39 +0000 | [diff] [blame] | 132 | def repr_int(self, x, level): |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 133 | s = builtins.repr(x) # XXX Hope this isn't too slow... |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 134 | if len(s) > self.maxlong: |
Guido van Rossum | 54e54c6 | 2001-09-04 19:14:14 +0000 | [diff] [blame] | 135 | i = max(0, (self.maxlong-3)//2) |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 136 | j = max(0, self.maxlong-3-i) |
| 137 | s = s[:i] + '...' + s[len(s)-j:] |
| 138 | return s |
Raymond Hettinger | ba6cd36 | 2004-05-21 10:00:15 +0000 | [diff] [blame] | 139 | |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 140 | def repr_instance(self, x, level): |
| 141 | try: |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 142 | s = builtins.repr(x) |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 143 | # Bugs in x.__repr__() can cause arbitrary |
| 144 | # exceptions -- then make up something |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 145 | except Exception: |
Serhiy Storchaka | 0c937b3 | 2014-07-22 12:14:52 +0300 | [diff] [blame] | 146 | return '<%s instance at %#x>' % (x.__class__.__name__, id(x)) |
Guido van Rossum | a48a3b4 | 2006-04-20 16:07:39 +0000 | [diff] [blame] | 147 | if len(s) > self.maxother: |
| 148 | i = max(0, (self.maxother-3)//2) |
| 149 | j = max(0, self.maxother-3-i) |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 150 | s = s[:i] + '...' + s[len(s)-j:] |
| 151 | return s |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 152 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 153 | |
| 154 | def _possibly_sorted(x): |
| 155 | # Since not all sequences of items can be sorted and comparison |
| 156 | # functions may raise arbitrary exceptions, return an unsorted |
| 157 | # sequence in that case. |
| 158 | try: |
| 159 | return sorted(x) |
| 160 | except Exception: |
| 161 | return list(x) |
| 162 | |
Guido van Rossum | 7bc817d | 1993-12-17 15:25:27 +0000 | [diff] [blame] | 163 | aRepr = Repr() |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 164 | repr = aRepr.repr |