annotate Lib/reprlib.py @ 79691:05c8928362ee

Merge __next__ method link fixes with 3.3.
author Ezio Melotti <ezio.melotti@gmail.com>
date Fri, 12 Oct 2012 13:46:37 +0300
parents 3b81c1eae514
children 8d28d44f3a9a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 42084
diff changeset
1 """Redo the builtin repr() (representation) but with limits on most sizes."""
622
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
64726
cfaf0f7ee0c2 Issue 9840: Add reprlib.recursive_repr(), a decorator for handling recursive calls to __repr__ methods.
Raymond Hettinger <python@rcn.com>
parents: 47102
diff changeset
3 __all__ = ["Repr", "repr", "recursive_repr"]
16613
f68dbffff927 bunch more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 16495
diff changeset
4
44098
0889daf6d0d9 #1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents: 43400
diff changeset
5 import builtins
32134
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
6 from itertools import islice
64726
cfaf0f7ee0c2 Issue 9840: Add reprlib.recursive_repr(), a decorator for handling recursive calls to __repr__ methods.
Raymond Hettinger <python@rcn.com>
parents: 47102
diff changeset
7 try:
72857
3b81c1eae514 Avoid pulling threading when _thread is sufficient
Antoine Pitrou <solipsis@pitrou.net>
parents: 70530
diff changeset
8 from _thread import get_ident
65987
385d239bcbfa Fix bootstrap issues when building without threads
Antoine Pitrou <solipsis@pitrou.net>
parents: 64726
diff changeset
9 except ImportError:
64726
cfaf0f7ee0c2 Issue 9840: Add reprlib.recursive_repr(), a decorator for handling recursive calls to __repr__ methods.
Raymond Hettinger <python@rcn.com>
parents: 47102
diff changeset
10 from _dummy_thread import get_ident
cfaf0f7ee0c2 Issue 9840: Add reprlib.recursive_repr(), a decorator for handling recursive calls to __repr__ methods.
Raymond Hettinger <python@rcn.com>
parents: 47102
diff changeset
11
cfaf0f7ee0c2 Issue 9840: Add reprlib.recursive_repr(), a decorator for handling recursive calls to __repr__ methods.
Raymond Hettinger <python@rcn.com>
parents: 47102
diff changeset
12 def recursive_repr(fillvalue='...'):
cfaf0f7ee0c2 Issue 9840: Add reprlib.recursive_repr(), a decorator for handling recursive calls to __repr__ methods.
Raymond Hettinger <python@rcn.com>
parents: 47102
diff changeset
13 'Decorator to make a repr function return fillvalue for a recursive call'
cfaf0f7ee0c2 Issue 9840: Add reprlib.recursive_repr(), a decorator for handling recursive calls to __repr__ methods.
Raymond Hettinger <python@rcn.com>
parents: 47102
diff changeset
14
cfaf0f7ee0c2 Issue 9840: Add reprlib.recursive_repr(), a decorator for handling recursive calls to __repr__ methods.
Raymond Hettinger <python@rcn.com>
parents: 47102
diff changeset
15 def decorating_function(user_function):
cfaf0f7ee0c2 Issue 9840: Add reprlib.recursive_repr(), a decorator for handling recursive calls to __repr__ methods.
Raymond Hettinger <python@rcn.com>
parents: 47102
diff changeset
16 repr_running = set()
cfaf0f7ee0c2 Issue 9840: Add reprlib.recursive_repr(), a decorator for handling recursive calls to __repr__ methods.
Raymond Hettinger <python@rcn.com>
parents: 47102
diff changeset
17
cfaf0f7ee0c2 Issue 9840: Add reprlib.recursive_repr(), a decorator for handling recursive calls to __repr__ methods.
Raymond Hettinger <python@rcn.com>
parents: 47102
diff changeset
18 def wrapper(self):
cfaf0f7ee0c2 Issue 9840: Add reprlib.recursive_repr(), a decorator for handling recursive calls to __repr__ methods.
Raymond Hettinger <python@rcn.com>
parents: 47102
diff changeset
19 key = id(self), get_ident()
cfaf0f7ee0c2 Issue 9840: Add reprlib.recursive_repr(), a decorator for handling recursive calls to __repr__ methods.
Raymond Hettinger <python@rcn.com>
parents: 47102
diff changeset
20 if key in repr_running:
cfaf0f7ee0c2 Issue 9840: Add reprlib.recursive_repr(), a decorator for handling recursive calls to __repr__ methods.
Raymond Hettinger <python@rcn.com>
parents: 47102
diff changeset
21 return fillvalue
cfaf0f7ee0c2 Issue 9840: Add reprlib.recursive_repr(), a decorator for handling recursive calls to __repr__ methods.
Raymond Hettinger <python@rcn.com>
parents: 47102
diff changeset
22 repr_running.add(key)
cfaf0f7ee0c2 Issue 9840: Add reprlib.recursive_repr(), a decorator for handling recursive calls to __repr__ methods.
Raymond Hettinger <python@rcn.com>
parents: 47102
diff changeset
23 try:
cfaf0f7ee0c2 Issue 9840: Add reprlib.recursive_repr(), a decorator for handling recursive calls to __repr__ methods.
Raymond Hettinger <python@rcn.com>
parents: 47102
diff changeset
24 result = user_function(self)
cfaf0f7ee0c2 Issue 9840: Add reprlib.recursive_repr(), a decorator for handling recursive calls to __repr__ methods.
Raymond Hettinger <python@rcn.com>
parents: 47102
diff changeset
25 finally:
cfaf0f7ee0c2 Issue 9840: Add reprlib.recursive_repr(), a decorator for handling recursive calls to __repr__ methods.
Raymond Hettinger <python@rcn.com>
parents: 47102
diff changeset
26 repr_running.discard(key)
cfaf0f7ee0c2 Issue 9840: Add reprlib.recursive_repr(), a decorator for handling recursive calls to __repr__ methods.
Raymond Hettinger <python@rcn.com>
parents: 47102
diff changeset
27 return result
cfaf0f7ee0c2 Issue 9840: Add reprlib.recursive_repr(), a decorator for handling recursive calls to __repr__ methods.
Raymond Hettinger <python@rcn.com>
parents: 47102
diff changeset
28
cfaf0f7ee0c2 Issue 9840: Add reprlib.recursive_repr(), a decorator for handling recursive calls to __repr__ methods.
Raymond Hettinger <python@rcn.com>
parents: 47102
diff changeset
29 # Can't use functools.wraps() here because of bootstrap issues
cfaf0f7ee0c2 Issue 9840: Add reprlib.recursive_repr(), a decorator for handling recursive calls to __repr__ methods.
Raymond Hettinger <python@rcn.com>
parents: 47102
diff changeset
30 wrapper.__module__ = getattr(user_function, '__module__')
cfaf0f7ee0c2 Issue 9840: Add reprlib.recursive_repr(), a decorator for handling recursive calls to __repr__ methods.
Raymond Hettinger <python@rcn.com>
parents: 47102
diff changeset
31 wrapper.__doc__ = getattr(user_function, '__doc__')
cfaf0f7ee0c2 Issue 9840: Add reprlib.recursive_repr(), a decorator for handling recursive calls to __repr__ methods.
Raymond Hettinger <python@rcn.com>
parents: 47102
diff changeset
32 wrapper.__name__ = getattr(user_function, '__name__')
67729
d1c06306c19b Fix typo.
Raymond Hettinger <python@rcn.com>
parents: 67726
diff changeset
33 wrapper.__annotations__ = getattr(user_function, '__annotations__', {})
64726
cfaf0f7ee0c2 Issue 9840: Add reprlib.recursive_repr(), a decorator for handling recursive calls to __repr__ methods.
Raymond Hettinger <python@rcn.com>
parents: 47102
diff changeset
34 return wrapper
cfaf0f7ee0c2 Issue 9840: Add reprlib.recursive_repr(), a decorator for handling recursive calls to __repr__ methods.
Raymond Hettinger <python@rcn.com>
parents: 47102
diff changeset
35
cfaf0f7ee0c2 Issue 9840: Add reprlib.recursive_repr(), a decorator for handling recursive calls to __repr__ methods.
Raymond Hettinger <python@rcn.com>
parents: 47102
diff changeset
36 return decorating_function
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 27701
diff changeset
37
622
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 class Repr:
32134
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
39
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
40 def __init__(self):
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
41 self.maxlevel = 6
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
42 self.maxtuple = 6
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
43 self.maxlist = 6
27701
37999f6924b4 [680789] Debug with long array takes forever
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
44 self.maxarray = 5
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
45 self.maxdict = 4
32134
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
46 self.maxset = 6
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
47 self.maxfrozenset = 6
32135
4e5b978e6b78 * teach repr.repr() about collections.deque()
Raymond Hettinger <python@rcn.com>
parents: 32134
diff changeset
48 self.maxdeque = 6
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
49 self.maxstring = 30
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
50 self.maxlong = 40
37868
2fb16a629415 Fix test failures for repr.py.
Guido van Rossum <guido@python.org>
parents: 32460
diff changeset
51 self.maxother = 30
32134
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
52
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
53 def repr(self, x):
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
54 return self.repr1(x, self.maxlevel)
32134
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
55
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
56 def repr1(self, x, level):
19107
131a01b6f6c7 Use type(x).__name__ to get the name of the type instead of parsing
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
57 typename = type(x).__name__
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
58 if ' ' in typename:
16495
69f162dcd8b4 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 15871
diff changeset
59 parts = typename.split()
69f162dcd8b4 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 15871
diff changeset
60 typename = '_'.join(parts)
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
61 if hasattr(self, 'repr_' + typename):
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
62 return getattr(self, 'repr_' + typename)(x, level)
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
63 else:
37868
2fb16a629415 Fix test failures for repr.py.
Guido van Rossum <guido@python.org>
parents: 32460
diff changeset
64 return self.repr_instance(x, level)
32134
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
65
32135
4e5b978e6b78 * teach repr.repr() about collections.deque()
Raymond Hettinger <python@rcn.com>
parents: 32134
diff changeset
66 def _repr_iterable(self, x, level, left, right, maxiter, trail=''):
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
67 n = len(x)
32134
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
68 if level <= 0 and n:
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
69 s = '...'
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
70 else:
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
71 newlevel = level - 1
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
72 repr1 = self.repr1
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
73 pieces = [repr1(elem, newlevel) for elem in islice(x, maxiter)]
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
74 if n > maxiter: pieces.append('...')
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
75 s = ', '.join(pieces)
32135
4e5b978e6b78 * teach repr.repr() about collections.deque()
Raymond Hettinger <python@rcn.com>
parents: 32134
diff changeset
76 if n == 1 and trail: right = trail + right
32134
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
77 return '%s%s%s' % (left, s, right)
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
78
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
79 def repr_tuple(self, x, level):
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40639
diff changeset
80 return self._repr_iterable(x, level, '(', ')', self.maxtuple, ',')
32134
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
81
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
82 def repr_list(self, x, level):
32134
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
83 return self._repr_iterable(x, level, '[', ']', self.maxlist)
27701
37999f6924b4 [680789] Debug with long array takes forever
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
84
37999f6924b4 [680789] Debug with long array takes forever
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
85 def repr_array(self, x, level):
37999f6924b4 [680789] Debug with long array takes forever
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
86 header = "array('%s', [" % x.typecode
32134
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
87 return self._repr_iterable(x, level, header, '])', self.maxarray)
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
88
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
89 def repr_set(self, x, level):
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 42084
diff changeset
90 x = _possibly_sorted(x)
32134
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
91 return self._repr_iterable(x, level, 'set([', '])', self.maxset)
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
92
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
93 def repr_frozenset(self, x, level):
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 42084
diff changeset
94 x = _possibly_sorted(x)
32134
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
95 return self._repr_iterable(x, level, 'frozenset([', '])',
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
96 self.maxfrozenset)
27701
37999f6924b4 [680789] Debug with long array takes forever
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
97
32135
4e5b978e6b78 * teach repr.repr() about collections.deque()
Raymond Hettinger <python@rcn.com>
parents: 32134
diff changeset
98 def repr_deque(self, x, level):
4e5b978e6b78 * teach repr.repr() about collections.deque()
Raymond Hettinger <python@rcn.com>
parents: 32134
diff changeset
99 return self._repr_iterable(x, level, 'deque([', '])', self.maxdeque)
4e5b978e6b78 * teach repr.repr() about collections.deque()
Raymond Hettinger <python@rcn.com>
parents: 32134
diff changeset
100
20754
272336648d1f Rename "dictionary" (type and constructor) to "dict".
Tim Peters <tim.peters@gmail.com>
parents: 19715
diff changeset
101 def repr_dict(self, x, level):
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
102 n = len(x)
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
103 if n == 0: return '{}'
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
104 if level <= 0: return '{...}'
32134
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
105 newlevel = level - 1
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
106 repr1 = self.repr1
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
107 pieces = []
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 42084
diff changeset
108 for key in islice(_possibly_sorted(x), self.maxdict):
32134
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
109 keyrepr = repr1(key, newlevel)
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
110 valrepr = repr1(x[key], newlevel)
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
111 pieces.append('%s: %s' % (keyrepr, valrepr))
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
112 if n > self.maxdict: pieces.append('...')
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
113 s = ', '.join(pieces)
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
114 return '{%s}' % (s,)
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
115
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 16613
diff changeset
116 def repr_str(self, x, level):
44098
0889daf6d0d9 #1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents: 43400
diff changeset
117 s = builtins.repr(x[:self.maxstring])
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
118 if len(s) > self.maxstring:
19694
3716b116fb31 The first batch of changes recommended by the fixdiv tool. These are
Guido van Rossum <guido@python.org>
parents: 19107
diff changeset
119 i = max(0, (self.maxstring-3)//2)
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
120 j = max(0, self.maxstring-3-i)
44098
0889daf6d0d9 #1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents: 43400
diff changeset
121 s = builtins.repr(x[:i] + x[len(x)-j:])
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
122 s = s[:i] + '...' + s[len(s)-j:]
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
123 return s
32134
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
124
40639
82b9b5b12256 Fix repr.py -- it was triggering on the type name 'long', should be 'int'.
Guido van Rossum <guido@python.org>
parents: 37868
diff changeset
125 def repr_int(self, x, level):
44098
0889daf6d0d9 #1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents: 43400
diff changeset
126 s = builtins.repr(x) # XXX Hope this isn't too slow...
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
127 if len(s) > self.maxlong:
19694
3716b116fb31 The first batch of changes recommended by the fixdiv tool. These are
Guido van Rossum <guido@python.org>
parents: 19107
diff changeset
128 i = max(0, (self.maxlong-3)//2)
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
129 j = max(0, self.maxlong-3-i)
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
130 s = s[:i] + '...' + s[len(s)-j:]
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
131 return s
32134
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
132
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
133 def repr_instance(self, x, level):
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
134 try:
44098
0889daf6d0d9 #1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents: 43400
diff changeset
135 s = builtins.repr(x)
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
136 # Bugs in x.__repr__() can cause arbitrary
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
137 # exceptions -- then make up something
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 42084
diff changeset
138 except Exception:
32460
c423e018a29e Committing Tim's patch for SF bug #983585:
Fred Drake <fdrake@acm.org>
parents: 32137
diff changeset
139 return '<%s instance at %x>' % (x.__class__.__name__, id(x))
37868
2fb16a629415 Fix test failures for repr.py.
Guido van Rossum <guido@python.org>
parents: 32460
diff changeset
140 if len(s) > self.maxother:
2fb16a629415 Fix test failures for repr.py.
Guido van Rossum <guido@python.org>
parents: 32460
diff changeset
141 i = max(0, (self.maxother-3)//2)
2fb16a629415 Fix test failures for repr.py.
Guido van Rossum <guido@python.org>
parents: 32460
diff changeset
142 j = max(0, self.maxother-3-i)
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
143 s = s[:i] + '...' + s[len(s)-j:]
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
144 return s
622
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
145
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 42084
diff changeset
146
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 42084
diff changeset
147 def _possibly_sorted(x):
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 42084
diff changeset
148 # Since not all sequences of items can be sorted and comparison
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 42084
diff changeset
149 # functions may raise arbitrary exceptions, return an unsorted
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 42084
diff changeset
150 # sequence in that case.
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 42084
diff changeset
151 try:
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 42084
diff changeset
152 return sorted(x)
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 42084
diff changeset
153 except Exception:
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 42084
diff changeset
154 return list(x)
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 42084
diff changeset
155
1435
560ec7030817 * Mass change: get rid of all init() methods, in favor of __init__()
Guido van Rossum <guido@python.org>
parents: 622
diff changeset
156 aRepr = Repr()
622
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 repr = aRepr.repr