Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
dis Namespace Reference

Functions

def dis
 
def distb
 
def disassemble
 
def findlabels
 
def def_op
 
def name_op
 
def jrel_op
 
def jabs_op
 

Variables

list __all__
 
 disco = disassemble
 
tuple cmp_op
 
list hasconst = []
 
list hasname = []
 
list hasjrel = []
 
list hasjabs = []
 
list haslocal = []
 
list hascompare = []
 
list hasfree = []
 
list opname = ['']
 
int HAVE_ARGUMENT = 90
 
int SET_LINENO = 127
 
int EXTENDED_ARG = 143
 

Detailed Description

Disassembler of Python byte code into mnemonics.

Function Documentation

def dis.def_op (   name,
  op 
)

Definition at line 139 of file dis.py.

140 def def_op(name, op):
141  opname[op] = name
def dis.dis (   x = None)
Disassemble classes, methods, functions, or code.

With no argument, disassemble the last traceback.

Definition at line 10 of file dis.py.

References disassemble(), and distb().

10 
11 def dis(x=None):
12  """Disassemble classes, methods, functions, or code.
13 
14  With no argument, disassemble the last traceback.
15 
16  """
17  if not x:
18  distb()
19  return
20  if type(x) is types.InstanceType:
21  x = x.__class__
22  if hasattr(x, 'im_func'):
23  x = x.im_func
24  if hasattr(x, 'func_code'):
25  x = x.func_code
26  if hasattr(x, '__dict__'):
27  items = x.__dict__.items()
28  items.sort()
29  for name, x1 in items:
30  if type(x1) in (types.MethodType,
31  types.FunctionType,
32  types.CodeType):
33  print "Disassembly of %s:" % name
34  try:
35  dis(x1)
36  except TypeError, msg:
37  print "Sorry:", msg
38  print
39  elif hasattr(x, 'co_code'):
40  disassemble(x)
41  else:
42  raise TypeError, \
43  "don't know how to disassemble %s objects" % \
44  type(x).__name__
def dis.disassemble (   co,
  lasti = -1 
)
Disassemble a code object.

Definition at line 55 of file dis.py.

References findlabels(), string.ljust(), and string.rjust().

55 
56 def disassemble(co, lasti=-1):
57  """Disassemble a code object."""
58  code = co.co_code
59  labels = findlabels(code)
60  n = len(code)
61  i = 0
62  extended_arg = 0
63  free = None
64  while i < n:
65  c = code[i]
66  op = ord(c)
67  if op == SET_LINENO and i > 0: print # Extra blank line
68  if i == lasti: print '-->',
69  else: print ' ',
70  if i in labels: print '>>',
71  else: print ' ',
72  print `i`.rjust(4),
73  print opname[op].ljust(20),
74  i = i+1
75  if op >= HAVE_ARGUMENT:
76  oparg = ord(code[i]) + ord(code[i+1])*256 + extended_arg
77  extended_arg = 0
78  i = i+2
79  if op == EXTENDED_ARG:
80  extended_arg = oparg*65536L
81  print `oparg`.rjust(5),
82  if op in hasconst:
83  print '(' + `co.co_consts[oparg]` + ')',
84  elif op in hasname:
85  print '(' + co.co_names[oparg] + ')',
86  elif op in hasjrel:
87  print '(to ' + `i + oparg` + ')',
88  elif op in haslocal:
89  print '(' + co.co_varnames[oparg] + ')',
90  elif op in hascompare:
91  print '(' + cmp_op[oparg] + ')',
92  elif op in hasfree:
93  if free is None:
94  free = co.co_cellvars + co.co_freevars
95  print '(' + free[oparg] + ')',
96  print
def dis.distb (   tb = None)
Disassemble a traceback (default: last traceback).

Definition at line 45 of file dis.py.

References disassemble().

45 
46 def distb(tb=None):
47  """Disassemble a traceback (default: last traceback)."""
48  if not tb:
49  try:
50  tb = sys.last_traceback
51  except AttributeError:
52  raise RuntimeError, "no last traceback to disassemble"
53  while tb.tb_next: tb = tb.tb_next
54  disassemble(tb.tb_frame.f_code, tb.tb_lasti)
def dis.findlabels (   code)
Detect all offsets in a byte code which are jump targets.

Return the list of offsets.

Definition at line 99 of file dis.py.

99 
100 def findlabels(code):
101  """Detect all offsets in a byte code which are jump targets.
102 
103  Return the list of offsets.
104 
105  """
106  labels = []
107  n = len(code)
108  i = 0
109  while i < n:
110  c = code[i]
111  op = ord(c)
112  i = i+1
113  if op >= HAVE_ARGUMENT:
114  oparg = ord(code[i]) + ord(code[i+1])*256
115  i = i+2
116  label = -1
117  if op in hasjrel:
118  label = i+oparg
119  elif op in hasjabs:
120  label = oparg
121  if label >= 0:
122  if label not in labels:
123  labels.append(label)
124  return labels
def dis.jabs_op (   name,
  op 
)

Definition at line 150 of file dis.py.

References def_op().

151 def jabs_op(name, op):
152  opname[op] = name
153  hasjabs.append(op)
154 
155 # Instruction opcodes for compiled code
156 
157 def_op('STOP_CODE', 0)
158 def_op('POP_TOP', 1)
159 def_op('ROT_TWO', 2)
160 def_op('ROT_THREE', 3)
161 def_op('DUP_TOP', 4)
162 def_op('ROT_FOUR', 5)
163 
164 def_op('UNARY_POSITIVE', 10)
165 def_op('UNARY_NEGATIVE', 11)
166 def_op('UNARY_NOT', 12)
167 def_op('UNARY_CONVERT', 13)
168 
169 def_op('UNARY_INVERT', 15)
170 
171 def_op('BINARY_POWER', 19)
172 
173 def_op('BINARY_MULTIPLY', 20)
174 def_op('BINARY_DIVIDE', 21)
175 def_op('BINARY_MODULO', 22)
176 def_op('BINARY_ADD', 23)
177 def_op('BINARY_SUBTRACT', 24)
178 def_op('BINARY_SUBSCR', 25)
179 def_op('BINARY_FLOOR_DIVIDE', 26)
180 def_op('BINARY_TRUE_DIVIDE', 27)
181 def_op('INPLACE_FLOOR_DIVIDE', 28)
182 def_op('INPLACE_TRUE_DIVIDE', 29)
183 
184 def_op('SLICE+0', 30)
185 def_op('SLICE+1', 31)
186 def_op('SLICE+2', 32)
187 def_op('SLICE+3', 33)
188 
189 def_op('STORE_SLICE+0', 40)
190 def_op('STORE_SLICE+1', 41)
191 def_op('STORE_SLICE+2', 42)
192 def_op('STORE_SLICE+3', 43)
193 
194 def_op('DELETE_SLICE+0', 50)
195 def_op('DELETE_SLICE+1', 51)
196 def_op('DELETE_SLICE+2', 52)
197 def_op('DELETE_SLICE+3', 53)
198 
199 def_op('INPLACE_ADD', 55)
200 def_op('INPLACE_SUBTRACT', 56)
201 def_op('INPLACE_MULTIPLY', 57)
202 def_op('INPLACE_DIVIDE', 58)
203 def_op('INPLACE_MODULO', 59)
204 def_op('STORE_SUBSCR', 60)
205 def_op('DELETE_SUBSCR', 61)
206 
207 def_op('BINARY_LSHIFT', 62)
208 def_op('BINARY_RSHIFT', 63)
209 def_op('BINARY_AND', 64)
210 def_op('BINARY_XOR', 65)
211 def_op('BINARY_OR', 66)
212 def_op('INPLACE_POWER', 67)
213 def_op('GET_ITER', 68)
214 
215 def_op('PRINT_EXPR', 70)
216 def_op('PRINT_ITEM', 71)
217 def_op('PRINT_NEWLINE', 72)
218 def_op('PRINT_ITEM_TO', 73)
219 def_op('PRINT_NEWLINE_TO', 74)
220 def_op('INPLACE_LSHIFT', 75)
221 def_op('INPLACE_RSHIFT', 76)
222 def_op('INPLACE_AND', 77)
223 def_op('INPLACE_XOR', 78)
224 def_op('INPLACE_OR', 79)
225 def_op('BREAK_LOOP', 80)
226 
227 def_op('LOAD_LOCALS', 82)
228 def_op('RETURN_VALUE', 83)
229 def_op('IMPORT_STAR', 84)
230 def_op('EXEC_STMT', 85)
231 def_op('YIELD_STMT', 86)
232 
233 def_op('POP_BLOCK', 87)
234 def_op('END_FINALLY', 88)
235 def_op('BUILD_CLASS', 89)
def dis.jrel_op (   name,
  op 
)

Definition at line 146 of file dis.py.

147 def jrel_op(name, op):
148  opname[op] = name
149  hasjrel.append(op)
def dis.name_op (   name,
  op 
)

Definition at line 142 of file dis.py.

143 def name_op(name, op):
144  opname[op] = name
145  hasname.append(op)

Variable Documentation

list __all__
Initial value:
1 = ["dis","disassemble","distb","disco","opname","cmp_op",
2  "hasconst","hasname","hasjrel","hasjabs","haslocal",
3  "hascompare", "hasfree"]

Definition at line 6 of file dis.py.

tuple cmp_op
Initial value:
1 = ('<', '<=', '==', '!=', '>', '>=', 'in', 'not in', 'is',
2  'is not', 'exception match', 'BAD')

Definition at line 125 of file dis.py.

disco = disassemble

Definition at line 97 of file dis.py.

int EXTENDED_ARG = 143

Definition at line 301 of file dis.py.

list hascompare = []

Definition at line 133 of file dis.py.

list hasconst = []

Definition at line 128 of file dis.py.

list hasfree = []

Definition at line 134 of file dis.py.

list hasjabs = []

Definition at line 131 of file dis.py.

list hasjrel = []

Definition at line 130 of file dis.py.

list haslocal = []

Definition at line 132 of file dis.py.

list hasname = []

Definition at line 129 of file dis.py.

int HAVE_ARGUMENT = 90

Definition at line 236 of file dis.py.

list opname = ['']

Definition at line 136 of file dis.py.

int SET_LINENO = 127

Definition at line 281 of file dis.py.