ยปCore Development>Code coverage>Lib/reprlib.py

Python code coverage for Lib/reprlib.py

#countcontent
1n/a"""Redo the builtin repr() (representation) but with limits on most sizes."""
2n/a
3n/a__all__ = ["Repr", "repr", "recursive_repr"]
4n/a
5n/aimport builtins
6n/afrom itertools import islice
7n/atry:
8n/a from _thread import get_ident
9n/aexcept ImportError:
10n/a from _dummy_thread import get_ident
11n/a
12n/adef recursive_repr(fillvalue='...'):
13n/a 'Decorator to make a repr function return fillvalue for a recursive call'
14n/a
15n/a def decorating_function(user_function):
16n/a repr_running = set()
17n/a
18n/a def wrapper(self):
19n/a key = id(self), get_ident()
20n/a if key in repr_running:
21n/a return fillvalue
22n/a repr_running.add(key)
23n/a try:
24n/a result = user_function(self)
25n/a finally:
26n/a repr_running.discard(key)
27n/a return result
28n/a
29n/a # Can't use functools.wraps() here because of bootstrap issues
30n/a wrapper.__module__ = getattr(user_function, '__module__')
31n/a wrapper.__doc__ = getattr(user_function, '__doc__')
32n/a wrapper.__name__ = getattr(user_function, '__name__')
33n/a wrapper.__qualname__ = getattr(user_function, '__qualname__')
34n/a wrapper.__annotations__ = getattr(user_function, '__annotations__', {})
35n/a return wrapper
36n/a
37n/a return decorating_function
38n/a
39n/aclass Repr:
40n/a
41n/a def __init__(self):
42n/a self.maxlevel = 6
43n/a self.maxtuple = 6
44n/a self.maxlist = 6
45n/a self.maxarray = 5
46n/a self.maxdict = 4
47n/a self.maxset = 6
48n/a self.maxfrozenset = 6
49n/a self.maxdeque = 6
50n/a self.maxstring = 30
51n/a self.maxlong = 40
52n/a self.maxother = 30
53n/a
54n/a def repr(self, x):
55n/a return self.repr1(x, self.maxlevel)
56n/a
57n/a def repr1(self, x, level):
58n/a typename = type(x).__name__
59n/a if ' ' in typename:
60n/a parts = typename.split()
61n/a typename = '_'.join(parts)
62n/a if hasattr(self, 'repr_' + typename):
63n/a return getattr(self, 'repr_' + typename)(x, level)
64n/a else:
65n/a return self.repr_instance(x, level)
66n/a
67n/a def _repr_iterable(self, x, level, left, right, maxiter, trail=''):
68n/a n = len(x)
69n/a if level <= 0 and n:
70n/a s = '...'
71n/a else:
72n/a newlevel = level - 1
73n/a repr1 = self.repr1
74n/a pieces = [repr1(elem, newlevel) for elem in islice(x, maxiter)]
75n/a if n > maxiter: pieces.append('...')
76n/a s = ', '.join(pieces)
77n/a if n == 1 and trail: right = trail + right
78n/a return '%s%s%s' % (left, s, right)
79n/a
80n/a def repr_tuple(self, x, level):
81n/a return self._repr_iterable(x, level, '(', ')', self.maxtuple, ',')
82n/a
83n/a def repr_list(self, x, level):
84n/a return self._repr_iterable(x, level, '[', ']', self.maxlist)
85n/a
86n/a def repr_array(self, x, level):
87n/a if not x:
88n/a return "array('%s')" % x.typecode
89n/a header = "array('%s', [" % x.typecode
90n/a return self._repr_iterable(x, level, header, '])', self.maxarray)
91n/a
92n/a def repr_set(self, x, level):
93n/a if not x:
94n/a return 'set()'
95n/a x = _possibly_sorted(x)
96n/a return self._repr_iterable(x, level, '{', '}', self.maxset)
97n/a
98n/a def repr_frozenset(self, x, level):
99n/a if not x:
100n/a return 'frozenset()'
101n/a x = _possibly_sorted(x)
102n/a return self._repr_iterable(x, level, 'frozenset({', '})',
103n/a self.maxfrozenset)
104n/a
105n/a def repr_deque(self, x, level):
106n/a return self._repr_iterable(x, level, 'deque([', '])', self.maxdeque)
107n/a
108n/a def repr_dict(self, x, level):
109n/a n = len(x)
110n/a if n == 0: return '{}'
111n/a if level <= 0: return '{...}'
112n/a newlevel = level - 1
113n/a repr1 = self.repr1
114n/a pieces = []
115n/a for key in islice(_possibly_sorted(x), self.maxdict):
116n/a keyrepr = repr1(key, newlevel)
117n/a valrepr = repr1(x[key], newlevel)
118n/a pieces.append('%s: %s' % (keyrepr, valrepr))
119n/a if n > self.maxdict: pieces.append('...')
120n/a s = ', '.join(pieces)
121n/a return '{%s}' % (s,)
122n/a
123n/a def repr_str(self, x, level):
124n/a s = builtins.repr(x[:self.maxstring])
125n/a if len(s) > self.maxstring:
126n/a i = max(0, (self.maxstring-3)//2)
127n/a j = max(0, self.maxstring-3-i)
128n/a s = builtins.repr(x[:i] + x[len(x)-j:])
129n/a s = s[:i] + '...' + s[len(s)-j:]
130n/a return s
131n/a
132n/a def repr_int(self, x, level):
133n/a s = builtins.repr(x) # XXX Hope this isn't too slow...
134n/a if len(s) > self.maxlong:
135n/a i = max(0, (self.maxlong-3)//2)
136n/a j = max(0, self.maxlong-3-i)
137n/a s = s[:i] + '...' + s[len(s)-j:]
138n/a return s
139n/a
140n/a def repr_instance(self, x, level):
141n/a try:
142n/a s = builtins.repr(x)
143n/a # Bugs in x.__repr__() can cause arbitrary
144n/a # exceptions -- then make up something
145n/a except Exception:
146n/a return '<%s instance at %#x>' % (x.__class__.__name__, id(x))
147n/a if len(s) > self.maxother:
148n/a i = max(0, (self.maxother-3)//2)
149n/a j = max(0, self.maxother-3-i)
150n/a s = s[:i] + '...' + s[len(s)-j:]
151n/a return s
152n/a
153n/a
154n/adef _possibly_sorted(x):
155n/a # Since not all sequences of items can be sorted and comparison
156n/a # functions may raise arbitrary exceptions, return an unsorted
157n/a # sequence in that case.
158n/a try:
159n/a return sorted(x)
160n/a except Exception:
161n/a return list(x)
162n/a
163n/aaRepr = Repr()
164n/arepr = aRepr.repr