Mercurial > cpython-lru_cache-weakref
annotate Lib/dis.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 | 388f9c2220ce |
| children | 39745574bf07 |
| rev | line source |
|---|---|
| 103 | 1 # Disassembler |
| 2 | |
| 3 import sys | |
| 4 import string | |
| 5 | |
| 6 def dis(): | |
| 7 tb = sys.last_traceback | |
| 8 while tb.tb_next: tb = tb.tb_next | |
| 9 distb(tb) | |
| 10 | |
| 11 def distb(tb): | |
| 12 disassemble(tb.tb_frame.f_code, tb.tb_lasti) | |
| 13 | |
| 14 def disco(co): | |
| 15 disassemble(co, -1) | |
| 16 | |
| 17 def disassemble(co, lasti): | |
| 18 code = co.co_code | |
| 19 labels = findlabels(code) | |
| 20 n = len(code) | |
| 21 i = 0 | |
| 22 while i < n: | |
| 23 c = code[i] | |
| 24 op = ord(c) | |
| 559 | 25 if op == SET_LINENO and i > 0: print # Extra blank line |
| 26 if i == lasti: print '-->', | |
| 103 | 27 else: print ' ', |
| 28 if i in labels: print '>>', | |
| 29 else: print ' ', | |
| 30 print string.rjust(`i`, 4), | |
| 31 print string.ljust(opname[op], 15), | |
| 32 i = i+1 | |
| 33 if op >= HAVE_ARGUMENT: | |
| 34 oparg = ord(code[i]) + ord(code[i+1])*256 | |
| 35 i = i+2 | |
| 36 print string.rjust(`oparg`, 5), | |
| 37 if op in hasconst: | |
| 38 print '(' + `co.co_consts[oparg]` + ')', | |
| 39 elif op in hasname: | |
| 40 print '(' + co.co_names[oparg] + ')', | |
| 41 elif op in hasjrel: | |
| 42 print '(to ' + `i + oparg` + ')', | |
|
4303
388f9c2220ce
Show names of locals in disco (Ka-Ping Yee)
Guido van Rossum <guido@python.org>
parents:
4271
diff
changeset
|
43 elif op in haslocal: |
|
388f9c2220ce
Show names of locals in disco (Ka-Ping Yee)
Guido van Rossum <guido@python.org>
parents:
4271
diff
changeset
|
44 print '(' + co.co_varnames[oparg] + ')', |
| 103 | 45 print |
| 46 | |
| 47 def findlabels(code): | |
| 48 labels = [] | |
| 49 n = len(code) | |
| 50 i = 0 | |
| 51 while i < n: | |
| 52 c = code[i] | |
| 53 op = ord(c) | |
| 54 i = i+1 | |
| 55 if op >= HAVE_ARGUMENT: | |
| 56 oparg = ord(code[i]) + ord(code[i+1])*256 | |
| 57 i = i+2 | |
| 58 label = -1 | |
| 59 if op in hasjrel: | |
| 60 label = i+oparg | |
| 61 elif op in hasjabs: | |
| 62 label = oparg | |
| 63 if label >= 0: | |
| 64 if label not in labels: | |
| 65 labels.append(label) | |
| 66 return labels | |
| 67 | |
| 68 hasconst = [] | |
| 69 hasname = [] | |
| 70 hasjrel = [] | |
| 71 hasjabs = [] | |
|
4303
388f9c2220ce
Show names of locals in disco (Ka-Ping Yee)
Guido van Rossum <guido@python.org>
parents:
4271
diff
changeset
|
72 haslocal = [] |
| 103 | 73 |
|
1403
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1264
diff
changeset
|
74 opname = [''] * 256 |
|
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1264
diff
changeset
|
75 for op in range(256): opname[op] = '<' + `op` + '>' |
| 103 | 76 |
| 77 def def_op(name, op): | |
| 78 opname[op] = name | |
| 79 | |
| 80 def name_op(name, op): | |
| 81 opname[op] = name | |
| 82 hasname.append(op) | |
| 83 | |
| 84 def jrel_op(name, op): | |
| 85 opname[op] = name | |
| 86 hasjrel.append(op) | |
| 87 | |
| 88 def jabs_op(name, op): | |
| 89 opname[op] = name | |
| 90 hasjabs.append(op) | |
| 91 | |
| 92 # Instruction opcodes for compiled code | |
| 93 | |
| 94 def_op('STOP_CODE', 0) | |
| 95 def_op('POP_TOP', 1) | |
| 96 def_op('ROT_TWO', 2) | |
| 97 def_op('ROT_THREE', 3) | |
| 98 def_op('DUP_TOP', 4) | |
| 99 | |
| 100 def_op('UNARY_POSITIVE', 10) | |
| 101 def_op('UNARY_NEGATIVE', 11) | |
| 102 def_op('UNARY_NOT', 12) | |
| 103 def_op('UNARY_CONVERT', 13) | |
| 104 def_op('UNARY_CALL', 14) | |
|
1264
6c58dc1d2f9d
Updated because of new opcodes introduced for "fast" local variables.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
105 def_op('UNARY_INVERT', 15) |
| 103 | 106 |
|
3753
97d2278a331b
New opcodes BINARY_POWER, RAISE_VARARGS, CALL_FUNCTION, MAKE_FUNCTION
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
107 def_op('BINARY_POWER', 19) |
| 103 | 108 def_op('BINARY_MULTIPLY', 20) |
| 109 def_op('BINARY_DIVIDE', 21) | |
| 110 def_op('BINARY_MODULO', 22) | |
| 111 def_op('BINARY_ADD', 23) | |
| 112 def_op('BINARY_SUBTRACT', 24) | |
| 113 def_op('BINARY_SUBSCR', 25) | |
| 114 def_op('BINARY_CALL', 26) | |
| 115 | |
| 116 def_op('SLICE+0', 30) | |
| 117 def_op('SLICE+1', 31) | |
| 118 def_op('SLICE+2', 32) | |
| 119 def_op('SLICE+3', 33) | |
| 120 | |
| 121 def_op('STORE_SLICE+0', 40) | |
| 122 def_op('STORE_SLICE+1', 41) | |
| 123 def_op('STORE_SLICE+2', 42) | |
| 124 def_op('STORE_SLICE+3', 43) | |
| 125 | |
| 126 def_op('DELETE_SLICE+0', 50) | |
| 127 def_op('DELETE_SLICE+1', 51) | |
| 128 def_op('DELETE_SLICE+2', 52) | |
| 129 def_op('DELETE_SLICE+3', 53) | |
| 130 | |
| 131 def_op('STORE_SUBSCR', 60) | |
| 132 def_op('DELETE_SUBSCR', 61) | |
| 133 | |
| 134 def_op('PRINT_EXPR', 70) | |
| 135 def_op('PRINT_ITEM', 71) | |
| 136 def_op('PRINT_NEWLINE', 72) | |
| 137 | |
| 138 def_op('BREAK_LOOP', 80) | |
| 139 def_op('RAISE_EXCEPTION', 81) | |
| 140 def_op('LOAD_LOCALS', 82) | |
| 141 def_op('RETURN_VALUE', 83) | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1403
diff
changeset
|
142 def_op('EXEC_STMT', 85) |
| 103 | 143 def_op('BUILD_FUNCTION', 86) |
| 144 def_op('POP_BLOCK', 87) | |
| 145 def_op('END_FINALLY', 88) | |
| 146 def_op('BUILD_CLASS', 89) | |
| 147 | |
| 148 HAVE_ARGUMENT = 90 # Opcodes from here have an argument: | |
| 149 | |
| 150 name_op('STORE_NAME', 90) # Index in name list | |
| 151 name_op('DELETE_NAME', 91) # "" | |
| 152 def_op('UNPACK_TUPLE', 92) # Number of tuple items | |
| 153 def_op('UNPACK_LIST', 93) # Number of list items | |
|
490
023c7f69d240
New opcodes: UNPACK_ARG, STORE_GLOBAL, DELETE_GLOBAL, LOAD_LOCAL, LOAD_GLOBAL
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
154 def_op('UNPACK_ARG', 94) # Number of arguments expected |
| 103 | 155 name_op('STORE_ATTR', 95) # Index in name list |
|
490
023c7f69d240
New opcodes: UNPACK_ARG, STORE_GLOBAL, DELETE_GLOBAL, LOAD_LOCAL, LOAD_GLOBAL
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
156 name_op('DELETE_ATTR', 96) # "" |
|
023c7f69d240
New opcodes: UNPACK_ARG, STORE_GLOBAL, DELETE_GLOBAL, LOAD_LOCAL, LOAD_GLOBAL
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
157 name_op('STORE_GLOBAL', 97) # "" |
|
023c7f69d240
New opcodes: UNPACK_ARG, STORE_GLOBAL, DELETE_GLOBAL, LOAD_LOCAL, LOAD_GLOBAL
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
158 name_op('DELETE_GLOBAL', 98) # "" |
|
1264
6c58dc1d2f9d
Updated because of new opcodes introduced for "fast" local variables.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
159 name_op('UNPACK_VARARG', 99) # Minimal number of arguments |
| 103 | 160 |
| 161 def_op('LOAD_CONST', 100) # Index in const list | |
| 162 hasconst.append(100) | |
| 163 name_op('LOAD_NAME', 101) # Index in name list | |
| 164 def_op('BUILD_TUPLE', 102) # Number of tuple items | |
| 165 def_op('BUILD_LIST', 103) # Number of list items | |
| 166 def_op('BUILD_MAP', 104) # Always zero for now | |
| 167 name_op('LOAD_ATTR', 105) # Index in name list | |
| 168 def_op('COMPARE_OP', 106) # Comparison operator | |
| 169 name_op('IMPORT_NAME', 107) # Index in name list | |
| 170 name_op('IMPORT_FROM', 108) # Index in name list | |
| 171 | |
| 172 jrel_op('JUMP_FORWARD', 110) # Number of bytes to skip | |
| 173 jrel_op('JUMP_IF_FALSE', 111) # "" | |
| 174 jrel_op('JUMP_IF_TRUE', 112) # "" | |
| 175 jabs_op('JUMP_ABSOLUTE', 113) # Target byte offset from beginning of code | |
| 176 jrel_op('FOR_LOOP', 114) # Number of bytes to skip | |
| 177 | |
|
490
023c7f69d240
New opcodes: UNPACK_ARG, STORE_GLOBAL, DELETE_GLOBAL, LOAD_LOCAL, LOAD_GLOBAL
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
178 name_op('LOAD_LOCAL', 115) # Index in name list |
|
023c7f69d240
New opcodes: UNPACK_ARG, STORE_GLOBAL, DELETE_GLOBAL, LOAD_LOCAL, LOAD_GLOBAL
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
179 name_op('LOAD_GLOBAL', 116) # Index in name list |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1403
diff
changeset
|
180 def_op('SET_FUNC_ARGS', 117) # Argcount |
|
490
023c7f69d240
New opcodes: UNPACK_ARG, STORE_GLOBAL, DELETE_GLOBAL, LOAD_LOCAL, LOAD_GLOBAL
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
181 |
| 103 | 182 jrel_op('SETUP_LOOP', 120) # Distance to target address |
| 183 jrel_op('SETUP_EXCEPT', 121) # "" | |
| 184 jrel_op('SETUP_FINALLY', 122) # "" | |
| 185 | |
|
1264
6c58dc1d2f9d
Updated because of new opcodes introduced for "fast" local variables.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
186 def_op('RESERVE_FAST', 123) # Number of local variables |
|
6c58dc1d2f9d
Updated because of new opcodes introduced for "fast" local variables.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
187 hasconst.append(123) |
|
6c58dc1d2f9d
Updated because of new opcodes introduced for "fast" local variables.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
188 def_op('LOAD_FAST', 124) # Local variable number |
|
4303
388f9c2220ce
Show names of locals in disco (Ka-Ping Yee)
Guido van Rossum <guido@python.org>
parents:
4271
diff
changeset
|
189 haslocal.append(124) |
|
1264
6c58dc1d2f9d
Updated because of new opcodes introduced for "fast" local variables.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
190 def_op('STORE_FAST', 125) # Local variable number |
|
4303
388f9c2220ce
Show names of locals in disco (Ka-Ping Yee)
Guido van Rossum <guido@python.org>
parents:
4271
diff
changeset
|
191 haslocal.append(125) |
|
1264
6c58dc1d2f9d
Updated because of new opcodes introduced for "fast" local variables.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
192 def_op('DELETE_FAST', 126) # Local variable number |
|
4303
388f9c2220ce
Show names of locals in disco (Ka-Ping Yee)
Guido van Rossum <guido@python.org>
parents:
4271
diff
changeset
|
193 haslocal.append(126) |
|
1264
6c58dc1d2f9d
Updated because of new opcodes introduced for "fast" local variables.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
194 |
| 103 | 195 def_op('SET_LINENO', 127) # Current line number |
| 196 SET_LINENO = 127 | |
|
3753
97d2278a331b
New opcodes BINARY_POWER, RAISE_VARARGS, CALL_FUNCTION, MAKE_FUNCTION
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
197 |
|
97d2278a331b
New opcodes BINARY_POWER, RAISE_VARARGS, CALL_FUNCTION, MAKE_FUNCTION
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
198 def_op('RAISE_VARARGS', 130) |
|
97d2278a331b
New opcodes BINARY_POWER, RAISE_VARARGS, CALL_FUNCTION, MAKE_FUNCTION
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
199 def_op('CALL_FUNCTION', 131) |
|
97d2278a331b
New opcodes BINARY_POWER, RAISE_VARARGS, CALL_FUNCTION, MAKE_FUNCTION
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
200 def_op('MAKE_FUNCTION', 132) |
|
3820
f7f3e4b5693c
Added BUILD_SLICE opcode.
Guido van Rossum <guido@python.org>
parents:
3753
diff
changeset
|
201 def_op('BUILD_SLICE', 133) |
