blob: f8033604da28da12dcad7d164580be638e51904a [file] [log] [blame]
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001"""Redo the builtin repr() (representation) but with limits on most sizes."""
Guido van Rossumbabe2bf1992-01-22 22:21:31 +00002
Raymond Hettinger98a5f3f2010-09-13 21:36:00 +00003__all__ = ["Repr", "repr", "recursive_repr"]
Skip Montanaro0de65802001-02-15 22:15:14 +00004
Georg Brandl1a3284e2007-12-02 09:40:06 +00005import builtins
Raymond Hettingerba6cd362004-05-21 10:00:15 +00006from itertools import islice
Raymond Hettinger98a5f3f2010-09-13 21:36:00 +00007try:
Antoine Pitroubb0ad4c2011-10-11 18:51:53 +02008 from _thread import get_ident
Antoine Pitroudb1bad22010-11-05 19:58:28 +00009except ImportError:
Raymond Hettinger98a5f3f2010-09-13 21:36:00 +000010 from _dummy_thread import get_ident
11
12def 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__')
Raymond Hettinger6ef18f42011-01-23 21:33:36 +000033 wrapper.__annotations__ = getattr(user_function, '__annotations__', {})
Raymond Hettinger98a5f3f2010-09-13 21:36:00 +000034 return wrapper
35
36 return decorating_function
Walter Dörwald70a6b492004-02-12 17:35:32 +000037
Guido van Rossumbabe2bf1992-01-22 22:21:31 +000038class Repr:
Raymond Hettingerba6cd362004-05-21 10:00:15 +000039
Tim Peters0c9886d2001-01-15 01:18:21 +000040 def __init__(self):
41 self.maxlevel = 6
42 self.maxtuple = 6
43 self.maxlist = 6
Tim Peters6ee04802003-02-05 18:29:34 +000044 self.maxarray = 5
Tim Peters0c9886d2001-01-15 01:18:21 +000045 self.maxdict = 4
Raymond Hettingerba6cd362004-05-21 10:00:15 +000046 self.maxset = 6
47 self.maxfrozenset = 6
Raymond Hettinger1453e4a2004-05-21 23:01:18 +000048 self.maxdeque = 6
Tim Peters0c9886d2001-01-15 01:18:21 +000049 self.maxstring = 30
50 self.maxlong = 40
Guido van Rossuma48a3b42006-04-20 16:07:39 +000051 self.maxother = 30
Raymond Hettingerba6cd362004-05-21 10:00:15 +000052
Tim Peters0c9886d2001-01-15 01:18:21 +000053 def repr(self, x):
54 return self.repr1(x, self.maxlevel)
Raymond Hettingerba6cd362004-05-21 10:00:15 +000055
Tim Peters0c9886d2001-01-15 01:18:21 +000056 def repr1(self, x, level):
Guido van Rossuma995c912001-08-09 18:56:27 +000057 typename = type(x).__name__
Tim Peters0c9886d2001-01-15 01:18:21 +000058 if ' ' in typename:
Eric S. Raymond6b71e742001-02-09 08:56:30 +000059 parts = typename.split()
60 typename = '_'.join(parts)
Tim Peters0c9886d2001-01-15 01:18:21 +000061 if hasattr(self, 'repr_' + typename):
62 return getattr(self, 'repr_' + typename)(x, level)
63 else:
Guido van Rossuma48a3b42006-04-20 16:07:39 +000064 return self.repr_instance(x, level)
Raymond Hettingerba6cd362004-05-21 10:00:15 +000065
Raymond Hettinger1453e4a2004-05-21 23:01:18 +000066 def _repr_iterable(self, x, level, left, right, maxiter, trail=''):
Raymond Hettingerba6cd362004-05-21 10:00:15 +000067 n = len(x)
68 if level <= 0 and n:
69 s = '...'
70 else:
71 newlevel = level - 1
72 repr1 = self.repr1
73 pieces = [repr1(elem, newlevel) for elem in islice(x, maxiter)]
74 if n > maxiter: pieces.append('...')
75 s = ', '.join(pieces)
Raymond Hettinger1453e4a2004-05-21 23:01:18 +000076 if n == 1 and trail: right = trail + right
Raymond Hettingerba6cd362004-05-21 10:00:15 +000077 return '%s%s%s' % (left, s, right)
78
Tim Peters0c9886d2001-01-15 01:18:21 +000079 def repr_tuple(self, x, level):
Guido van Rossumcd16bf62007-06-13 18:07:49 +000080 return self._repr_iterable(x, level, '(', ')', self.maxtuple, ',')
Raymond Hettingerba6cd362004-05-21 10:00:15 +000081
Tim Peters0c9886d2001-01-15 01:18:21 +000082 def repr_list(self, x, level):
Raymond Hettingerba6cd362004-05-21 10:00:15 +000083 return self._repr_iterable(x, level, '[', ']', self.maxlist)
Tim Peters6ee04802003-02-05 18:29:34 +000084
85 def repr_array(self, x, level):
Tim Peters6ee04802003-02-05 18:29:34 +000086 header = "array('%s', [" % x.typecode
Raymond Hettingerba6cd362004-05-21 10:00:15 +000087 return self._repr_iterable(x, level, header, '])', self.maxarray)
88
89 def repr_set(self, x, level):
Thomas Wouters1b7f8912007-09-19 03:06:30 +000090 x = _possibly_sorted(x)
Raymond Hettingerba6cd362004-05-21 10:00:15 +000091 return self._repr_iterable(x, level, 'set([', '])', self.maxset)
92
93 def repr_frozenset(self, x, level):
Thomas Wouters1b7f8912007-09-19 03:06:30 +000094 x = _possibly_sorted(x)
Raymond Hettingerba6cd362004-05-21 10:00:15 +000095 return self._repr_iterable(x, level, 'frozenset([', '])',
96 self.maxfrozenset)
Tim Peters6ee04802003-02-05 18:29:34 +000097
Raymond Hettinger1453e4a2004-05-21 23:01:18 +000098 def repr_deque(self, x, level):
99 return self._repr_iterable(x, level, 'deque([', '])', self.maxdeque)
100
Tim Petersa427a2b2001-10-29 22:25:45 +0000101 def repr_dict(self, x, level):
Tim Peters0c9886d2001-01-15 01:18:21 +0000102 n = len(x)
103 if n == 0: return '{}'
104 if level <= 0: return '{...}'
Raymond Hettingerba6cd362004-05-21 10:00:15 +0000105 newlevel = level - 1
106 repr1 = self.repr1
107 pieces = []
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000108 for key in islice(_possibly_sorted(x), self.maxdict):
Raymond Hettingerba6cd362004-05-21 10:00:15 +0000109 keyrepr = repr1(key, newlevel)
110 valrepr = repr1(x[key], newlevel)
111 pieces.append('%s: %s' % (keyrepr, valrepr))
112 if n > self.maxdict: pieces.append('...')
113 s = ', '.join(pieces)
114 return '{%s}' % (s,)
115
Tim Peters6d6c1a32001-08-02 04:15:00 +0000116 def repr_str(self, x, level):
Georg Brandl1a3284e2007-12-02 09:40:06 +0000117 s = builtins.repr(x[:self.maxstring])
Tim Peters0c9886d2001-01-15 01:18:21 +0000118 if len(s) > self.maxstring:
Guido van Rossum54e54c62001-09-04 19:14:14 +0000119 i = max(0, (self.maxstring-3)//2)
Tim Peters0c9886d2001-01-15 01:18:21 +0000120 j = max(0, self.maxstring-3-i)
Georg Brandl1a3284e2007-12-02 09:40:06 +0000121 s = builtins.repr(x[:i] + x[len(x)-j:])
Tim Peters0c9886d2001-01-15 01:18:21 +0000122 s = s[:i] + '...' + s[len(s)-j:]
123 return s
Raymond Hettingerba6cd362004-05-21 10:00:15 +0000124
Guido van Rossum4c9695a2007-01-15 00:14:39 +0000125 def repr_int(self, x, level):
Georg Brandl1a3284e2007-12-02 09:40:06 +0000126 s = builtins.repr(x) # XXX Hope this isn't too slow...
Tim Peters0c9886d2001-01-15 01:18:21 +0000127 if len(s) > self.maxlong:
Guido van Rossum54e54c62001-09-04 19:14:14 +0000128 i = max(0, (self.maxlong-3)//2)
Tim Peters0c9886d2001-01-15 01:18:21 +0000129 j = max(0, self.maxlong-3-i)
130 s = s[:i] + '...' + s[len(s)-j:]
131 return s
Raymond Hettingerba6cd362004-05-21 10:00:15 +0000132
Tim Peters0c9886d2001-01-15 01:18:21 +0000133 def repr_instance(self, x, level):
134 try:
Georg Brandl1a3284e2007-12-02 09:40:06 +0000135 s = builtins.repr(x)
Tim Peters0c9886d2001-01-15 01:18:21 +0000136 # Bugs in x.__repr__() can cause arbitrary
137 # exceptions -- then make up something
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000138 except Exception:
Fred Drake83a64302004-07-01 20:28:47 +0000139 return '<%s instance at %x>' % (x.__class__.__name__, id(x))
Guido van Rossuma48a3b42006-04-20 16:07:39 +0000140 if len(s) > self.maxother:
141 i = max(0, (self.maxother-3)//2)
142 j = max(0, self.maxother-3-i)
Tim Peters0c9886d2001-01-15 01:18:21 +0000143 s = s[:i] + '...' + s[len(s)-j:]
144 return s
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000145
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000146
147def _possibly_sorted(x):
148 # Since not all sequences of items can be sorted and comparison
149 # functions may raise arbitrary exceptions, return an unsorted
150 # sequence in that case.
151 try:
152 return sorted(x)
153 except Exception:
154 return list(x)
155
Guido van Rossum7bc817d1993-12-17 15:25:27 +0000156aRepr = Repr()
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000157repr = aRepr.repr