Mercurial > cpython-lru_cache-weakref
annotate Lib/repr.py @ 4566:6403c94de50d legacy-trunk
Added safeguard against failure in __del__.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Wed, 27 Nov 1996 19:39:58 +0000 |
| parents | 9d61c5749bf8 |
| children | 5bb4f50da474 |
| rev | line source |
|---|---|
| 622 | 1 # Redo the `...` (representation) but with limits on most sizes. |
| 2 | |
| 3 import string | |
| 4 | |
| 5 class Repr: | |
|
1435
560ec7030817
* Mass change: get rid of all init() methods, in favor of __init__()
Guido van Rossum <guido@python.org>
parents:
622
diff
changeset
|
6 def __init__(self): |
| 622 | 7 self.maxlevel = 6 |
| 8 self.maxtuple = 6 | |
| 9 self.maxlist = 6 | |
| 10 self.maxdict = 4 | |
| 11 self.maxstring = 30 | |
| 12 self.maxlong = 40 | |
| 13 self.maxother = 20 | |
| 14 def repr(self, x): | |
| 15 return self.repr1(x, self.maxlevel) | |
| 16 def repr1(self, x, level): | |
| 17 typename = `type(x)`[7:-2] # "<type '......'>" | |
| 18 if ' ' in typename: | |
| 19 parts = string.split(typename) | |
| 20 typename = string.joinfields(parts, '_') | |
|
2134
9d61c5749bf8
use getattr() instead of eval(), fix string representations
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
21 if hasattr(self, 'repr_' + typename): |
|
9d61c5749bf8
use getattr() instead of eval(), fix string representations
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
22 return getattr(self, 'repr_' + typename)(x, level) |
|
9d61c5749bf8
use getattr() instead of eval(), fix string representations
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
23 else: |
| 622 | 24 s = `x` |
| 25 if len(s) > self.maxother: | |
| 26 i = max(0, (self.maxother-3)/2) | |
| 27 j = max(0, self.maxother-3-i) | |
| 28 s = s[:i] + '...' + s[len(s)-j:] | |
| 29 return s | |
| 30 def repr_tuple(self, x, level): | |
| 31 n = len(x) | |
| 32 if n == 0: return '()' | |
| 33 if level <= 0: return '(...)' | |
| 34 s = '' | |
| 35 for i in range(min(n, self.maxtuple)): | |
| 36 if s: s = s + ', ' | |
| 37 s = s + self.repr1(x[i], level-1) | |
| 38 if n > self.maxtuple: s = s + ', ...' | |
| 39 elif n == 1: s = s + ',' | |
| 40 return '(' + s + ')' | |
| 41 def repr_list(self, x, level): | |
| 42 n = len(x) | |
| 43 if n == 0: return '[]' | |
| 44 if level <= 0: return '[...]' | |
| 45 s = '' | |
| 46 for i in range(min(n, self.maxlist)): | |
| 47 if s: s = s + ', ' | |
| 48 s = s + self.repr1(x[i], level-1) | |
| 49 if n > self.maxlist: s = s + ', ...' | |
| 50 return '[' + s + ']' | |
| 51 def repr_dictionary(self, x, level): | |
| 52 n = len(x) | |
| 53 if n == 0: return '{}' | |
| 54 if level <= 0: return '{...}' | |
| 55 s = '' | |
| 56 keys = x.keys() | |
| 57 keys.sort() | |
| 58 for i in range(min(n, self.maxdict)): | |
| 59 if s: s = s + ', ' | |
| 60 key = keys[i] | |
| 61 s = s + self.repr1(key, level-1) | |
| 62 s = s + ': ' + self.repr1(x[key], level-1) | |
| 63 if n > self.maxlist: s = s + ', ...' | |
| 64 return '{' + s + '}' | |
| 65 def repr_string(self, x, level): | |
| 66 s = `x[:self.maxstring]` | |
| 67 if len(s) > self.maxstring: | |
| 68 i = max(0, (self.maxstring-3)/2) | |
| 69 j = max(0, self.maxstring-3-i) | |
|
2134
9d61c5749bf8
use getattr() instead of eval(), fix string representations
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
70 s = `x[:i] + x[len(x)-j:]` |
| 622 | 71 s = s[:i] + '...' + s[len(s)-j:] |
| 72 return s | |
| 73 def repr_long_int(self, x, level): | |
| 74 s = `x` # XXX Hope this isn't too slow... | |
| 75 if len(s) > self.maxlong: | |
| 76 i = max(0, (self.maxlong-3)/2) | |
| 77 j = max(0, self.maxlong-3-i) | |
| 78 s = s[:i] + '...' + s[len(s)-j:] | |
| 79 return s | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
80 def repr_instance(self, x, level): |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
81 try: |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
82 s = `x` |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
83 # Bugs in x.__repr__() can cause arbitrary |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
84 # exceptions -- then make up something |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
85 except: |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
86 return '<' + x.__class__.__name__ + ' instance at ' + \ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
87 hex(id(x))[2:] + '>' |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
88 if len(s) > self.maxstring: |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
89 i = max(0, (self.maxstring-3)/2) |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
90 j = max(0, self.maxstring-3-i) |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
91 s = s[:i] + '...' + s[len(s)-j:] |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
92 return s |
| 622 | 93 |
|
1435
560ec7030817
* Mass change: get rid of all init() methods, in favor of __init__()
Guido van Rossum <guido@python.org>
parents:
622
diff
changeset
|
94 aRepr = Repr() |
| 622 | 95 repr = aRepr.repr |
