annotate Lib/repr.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents 9fbc41abdd4b
children 787b969d37f0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
43347
06efe835804a Bug #1153: repr.repr() now doesn't require set and dictionary items
Georg Brandl <georg@python.org>
parents: 42033
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
16613
f68dbffff927 bunch more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 16495
diff changeset
3 __all__ = ["Repr","repr"]
f68dbffff927 bunch more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 16495
diff changeset
4
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 27701
diff changeset
5 import __builtin__
32134
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
6 from itertools import islice
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 27701
diff changeset
7
622
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 class Repr:
32134
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
9
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
10 def __init__(self):
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
11 self.maxlevel = 6
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
12 self.maxtuple = 6
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
13 self.maxlist = 6
27701
37999f6924b4 [680789] Debug with long array takes forever
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
14 self.maxarray = 5
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
15 self.maxdict = 4
32134
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
16 self.maxset = 6
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
17 self.maxfrozenset = 6
32135
4e5b978e6b78 * teach repr.repr() about collections.deque()
Raymond Hettinger <python@rcn.com>
parents: 32134
diff changeset
18 self.maxdeque = 6
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
19 self.maxstring = 30
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
20 self.maxlong = 40
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
21 self.maxother = 20
32134
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
22
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
23 def repr(self, x):
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
24 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
25
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
26 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
27 typename = type(x).__name__
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
28 if ' ' in typename:
16495
69f162dcd8b4 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 15871
diff changeset
29 parts = typename.split()
69f162dcd8b4 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 15871
diff changeset
30 typename = '_'.join(parts)
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
31 if hasattr(self, 'repr_' + typename):
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
32 return getattr(self, 'repr_' + typename)(x, level)
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
33 else:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 27701
diff changeset
34 s = __builtin__.repr(x)
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
35 if len(s) > self.maxother:
19715
0d96145c44b8 Another / that should be a // (previously not caught because of
Guido van Rossum <guido@python.org>
parents: 19694
diff changeset
36 i = max(0, (self.maxother-3)//2)
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
37 j = max(0, self.maxother-3-i)
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
38 s = s[:i] + '...' + s[len(s)-j:]
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
39 return s
32134
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
40
32135
4e5b978e6b78 * teach repr.repr() about collections.deque()
Raymond Hettinger <python@rcn.com>
parents: 32134
diff changeset
41 def _repr_iterable(self, x, level, left, right, maxiter, trail=''):
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
42 n = len(x)
32134
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
43 if level <= 0 and n:
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
44 s = '...'
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
45 else:
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
46 newlevel = level - 1
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
47 repr1 = self.repr1
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
48 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
49 if n > maxiter: pieces.append('...')
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
50 s = ', '.join(pieces)
32135
4e5b978e6b78 * teach repr.repr() about collections.deque()
Raymond Hettinger <python@rcn.com>
parents: 32134
diff changeset
51 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
52 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
53
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
54 def repr_tuple(self, x, level):
42033
d1d79c71ff62 Bug #1734723: Fix repr.Repr() so it doesn't ignore the maxtuple attribute.
Neal Norwitz <nnorwitz@gmail.com>
parents: 32460
diff changeset
55 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
56
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
57 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
58 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
59
37999f6924b4 [680789] Debug with long array takes forever
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
60 def repr_array(self, x, level):
37999f6924b4 [680789] Debug with long array takes forever
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
61 header = "array('%s', [" % x.typecode
32134
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
62 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
63
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
64 def repr_set(self, x, level):
43347
06efe835804a Bug #1153: repr.repr() now doesn't require set and dictionary items
Georg Brandl <georg@python.org>
parents: 42033
diff changeset
65 x = _possibly_sorted(x)
32134
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
66 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
67
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
68 def repr_frozenset(self, x, level):
43347
06efe835804a Bug #1153: repr.repr() now doesn't require set and dictionary items
Georg Brandl <georg@python.org>
parents: 42033
diff changeset
69 x = _possibly_sorted(x)
32134
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
70 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
71 self.maxfrozenset)
27701
37999f6924b4 [680789] Debug with long array takes forever
Tim Peters <tim.peters@gmail.com>
parents: 20754
diff changeset
72
32135
4e5b978e6b78 * teach repr.repr() about collections.deque()
Raymond Hettinger <python@rcn.com>
parents: 32134
diff changeset
73 def repr_deque(self, x, level):
4e5b978e6b78 * teach repr.repr() about collections.deque()
Raymond Hettinger <python@rcn.com>
parents: 32134
diff changeset
74 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
75
20754
272336648d1f Rename "dictionary" (type and constructor) to "dict".
Tim Peters <tim.peters@gmail.com>
parents: 19715
diff changeset
76 def repr_dict(self, x, level):
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
77 n = len(x)
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
78 if n == 0: return '{}'
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
79 if level <= 0: return '{...}'
32134
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
80 newlevel = level - 1
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
81 repr1 = self.repr1
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
82 pieces = []
43347
06efe835804a Bug #1153: repr.repr() now doesn't require set and dictionary items
Georg Brandl <georg@python.org>
parents: 42033
diff changeset
83 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
84 keyrepr = repr1(key, newlevel)
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
85 valrepr = repr1(x[key], newlevel)
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
86 pieces.append('%s: %s' % (keyrepr, valrepr))
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
87 if n > self.maxdict: pieces.append('...')
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
88 s = ', '.join(pieces)
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
89 return '{%s}' % (s,)
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
90
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 16613
diff changeset
91 def repr_str(self, x, level):
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 27701
diff changeset
92 s = __builtin__.repr(x[:self.maxstring])
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
93 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
94 i = max(0, (self.maxstring-3)//2)
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
95 j = max(0, self.maxstring-3-i)
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 27701
diff changeset
96 s = __builtin__.repr(x[:i] + x[len(x)-j:])
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
97 s = s[:i] + '...' + s[len(s)-j:]
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
98 return s
32134
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
99
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 16613
diff changeset
100 def repr_long(self, x, level):
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 27701
diff changeset
101 s = __builtin__.repr(x) # XXX Hope this isn't too slow...
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
102 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
103 i = max(0, (self.maxlong-3)//2)
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
104 j = max(0, self.maxlong-3-i)
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
105 s = s[:i] + '...' + s[len(s)-j:]
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
106 return s
32134
31bdbec8f282 * Updated repr.py to handle set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
107
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
108 def repr_instance(self, x, level):
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
109 try:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 27701
diff changeset
110 s = __builtin__.repr(x)
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
111 # Bugs in x.__repr__() can cause arbitrary
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
112 # exceptions -- then make up something
43347
06efe835804a Bug #1153: repr.repr() now doesn't require set and dictionary items
Georg Brandl <georg@python.org>
parents: 42033
diff changeset
113 except Exception:
32460
c423e018a29e Committing Tim's patch for SF bug #983585:
Fred Drake <fdrake@acm.org>
parents: 32137
diff changeset
114 return '<%s instance at %x>' % (x.__class__.__name__, id(x))
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
115 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
116 i = max(0, (self.maxstring-3)//2)
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
117 j = max(0, self.maxstring-3-i)
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
118 s = s[:i] + '...' + s[len(s)-j:]
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11707
diff changeset
119 return s
622
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
120
43347
06efe835804a Bug #1153: repr.repr() now doesn't require set and dictionary items
Georg Brandl <georg@python.org>
parents: 42033
diff changeset
121
06efe835804a Bug #1153: repr.repr() now doesn't require set and dictionary items
Georg Brandl <georg@python.org>
parents: 42033
diff changeset
122 def _possibly_sorted(x):
06efe835804a Bug #1153: repr.repr() now doesn't require set and dictionary items
Georg Brandl <georg@python.org>
parents: 42033
diff changeset
123 # Since not all sequences of items can be sorted and comparison
06efe835804a Bug #1153: repr.repr() now doesn't require set and dictionary items
Georg Brandl <georg@python.org>
parents: 42033
diff changeset
124 # functions may raise arbitrary exceptions, return an unsorted
06efe835804a Bug #1153: repr.repr() now doesn't require set and dictionary items
Georg Brandl <georg@python.org>
parents: 42033
diff changeset
125 # sequence in that case.
06efe835804a Bug #1153: repr.repr() now doesn't require set and dictionary items
Georg Brandl <georg@python.org>
parents: 42033
diff changeset
126 try:
06efe835804a Bug #1153: repr.repr() now doesn't require set and dictionary items
Georg Brandl <georg@python.org>
parents: 42033
diff changeset
127 return sorted(x)
06efe835804a Bug #1153: repr.repr() now doesn't require set and dictionary items
Georg Brandl <georg@python.org>
parents: 42033
diff changeset
128 except Exception:
06efe835804a Bug #1153: repr.repr() now doesn't require set and dictionary items
Georg Brandl <georg@python.org>
parents: 42033
diff changeset
129 return list(x)
06efe835804a Bug #1153: repr.repr() now doesn't require set and dictionary items
Georg Brandl <georg@python.org>
parents: 42033
diff changeset
130
1435
560ec7030817 * Mass change: get rid of all init() methods, in favor of __init__()
Guido van Rossum <guido@python.org>
parents: 622
diff changeset
131 aRepr = Repr()
622
a0d4c5ef1d5d Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 repr = aRepr.repr