home *** CD-ROM | disk | FTP | other *** search
/ com!online 2005 April / com_0405_1.iso / opensource / BTpp-0.5.4-bin.exe / $INSTDIR / BT++.exe / dis.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2003-04-19  |  9.8 KB  |  330 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.2)
  3.  
  4. import sys
  5. import types
  6. __all__ = [
  7.     'dis',
  8.     'disassemble',
  9.     'distb',
  10.     'disco',
  11.     'opname',
  12.     'cmp_op',
  13.     'hasconst',
  14.     'hasname',
  15.     'hasjrel',
  16.     'hasjabs',
  17.     'haslocal',
  18.     'hascompare',
  19.     'hasfree']
  20.  
  21. def dis(x = None):
  22.     if not x:
  23.         distb()
  24.         return None
  25.     
  26.     if type(x) is types.InstanceType:
  27.         x = x.__class__
  28.     
  29.     if hasattr(x, 'im_func'):
  30.         x = x.im_func
  31.     
  32.     if hasattr(x, 'func_code'):
  33.         x = x.func_code
  34.     
  35.     if hasattr(x, '__dict__'):
  36.         items = x.__dict__.items()
  37.         items.sort()
  38.         for name, x1 in items:
  39.             if type(x1) in (types.MethodType, types.FunctionType, types.CodeType):
  40.                 print 'Disassembly of %s:' % name
  41.                 
  42.                 try:
  43.                     dis(x1)
  44.                 except TypeError:
  45.                     msg = None
  46.                     print 'Sorry:', msg
  47.  
  48.                 print 
  49.             
  50.         
  51.     elif hasattr(x, 'co_code'):
  52.         disassemble(x)
  53.     else:
  54.         raise TypeError, "don't know how to disassemble %s objects" % type(x).__name__
  55.  
  56.  
  57. def distb(tb = None):
  58.     if not tb:
  59.         
  60.         try:
  61.             tb = sys.last_traceback
  62.         except AttributeError:
  63.             raise RuntimeError, 'no last traceback to disassemble'
  64.  
  65.         while tb.tb_next:
  66.             tb = tb.tb_next
  67.     
  68.     disassemble(tb.tb_frame.f_code, tb.tb_lasti)
  69.  
  70.  
  71. def disassemble(co, lasti = -1):
  72.     code = co.co_code
  73.     labels = findlabels(code)
  74.     n = len(code)
  75.     i = 0
  76.     extended_arg = 0
  77.     free = None
  78.     while i < n:
  79.         c = code[i]
  80.         op = ord(c)
  81.         if op == SET_LINENO and i > 0:
  82.             print 
  83.         
  84.         if i == lasti:
  85.             print '-->',
  86.         else:
  87.             print '   ',
  88.         if i in labels:
  89.             print '>>',
  90.         else:
  91.             print '  ',
  92.         print `i`.rjust(4), opname[op].ljust(20),
  93.         i = i + 1
  94.         if op >= HAVE_ARGUMENT:
  95.             oparg = ord(code[i]) + ord(code[i + 1]) * 256 + extended_arg
  96.             extended_arg = 0
  97.             i = i + 2
  98.             if op == EXTENDED_ARG:
  99.                 extended_arg = oparg * 0x10000L
  100.             
  101.             print `oparg`.rjust(5),
  102.             if op in hasconst:
  103.                 print '(' + `co.co_consts[oparg]` + ')',
  104.             elif op in hasname:
  105.                 print '(' + co.co_names[oparg] + ')',
  106.             elif op in hasjrel:
  107.                 print '(to ' + `i + oparg` + ')',
  108.             elif op in haslocal:
  109.                 print '(' + co.co_varnames[oparg] + ')',
  110.             elif op in hascompare:
  111.                 print '(' + cmp_op[oparg] + ')',
  112.             elif op in hasfree:
  113.                 if free is None:
  114.                     free = co.co_cellvars + co.co_freevars
  115.                 
  116.                 print '(' + free[oparg] + ')',
  117.             
  118.         
  119.         print 
  120.  
  121. disco = disassemble
  122.  
  123. def findlabels(code):
  124.     labels = []
  125.     n = len(code)
  126.     i = 0
  127.     while i < n:
  128.         c = code[i]
  129.         op = ord(c)
  130.         i = i + 1
  131.         if op >= HAVE_ARGUMENT:
  132.             oparg = ord(code[i]) + ord(code[i + 1]) * 256
  133.             i = i + 2
  134.             label = -1
  135.             if op in hasjrel:
  136.                 label = i + oparg
  137.             elif op in hasjabs:
  138.                 label = oparg
  139.             
  140.             if label >= 0:
  141.                 if label not in labels:
  142.                     labels.append(label)
  143.                 
  144.             
  145.         
  146.     return labels
  147.  
  148. cmp_op = ('<', '<=', '==', '!=', '>', '>=', 'in', 'not in', 'is', 'is not', 'exception match', 'BAD')
  149. hasconst = []
  150. hasname = []
  151. hasjrel = []
  152. hasjabs = []
  153. haslocal = []
  154. hascompare = []
  155. hasfree = []
  156. opname = [
  157.     ''] * 256
  158. for op in range(256):
  159.     opname[op] = '<' + `op` + '>'
  160.  
  161.  
  162. def def_op(name, op):
  163.     opname[op] = name
  164.  
  165.  
  166. def name_op(name, op):
  167.     opname[op] = name
  168.     hasname.append(op)
  169.  
  170.  
  171. def jrel_op(name, op):
  172.     opname[op] = name
  173.     hasjrel.append(op)
  174.  
  175.  
  176. def jabs_op(name, op):
  177.     opname[op] = name
  178.     hasjabs.append(op)
  179.  
  180. def_op('STOP_CODE', 0)
  181. def_op('POP_TOP', 1)
  182. def_op('ROT_TWO', 2)
  183. def_op('ROT_THREE', 3)
  184. def_op('DUP_TOP', 4)
  185. def_op('ROT_FOUR', 5)
  186. def_op('UNARY_POSITIVE', 10)
  187. def_op('UNARY_NEGATIVE', 11)
  188. def_op('UNARY_NOT', 12)
  189. def_op('UNARY_CONVERT', 13)
  190. def_op('UNARY_INVERT', 15)
  191. def_op('BINARY_POWER', 19)
  192. def_op('BINARY_MULTIPLY', 20)
  193. def_op('BINARY_DIVIDE', 21)
  194. def_op('BINARY_MODULO', 22)
  195. def_op('BINARY_ADD', 23)
  196. def_op('BINARY_SUBTRACT', 24)
  197. def_op('BINARY_SUBSCR', 25)
  198. def_op('BINARY_FLOOR_DIVIDE', 26)
  199. def_op('BINARY_TRUE_DIVIDE', 27)
  200. def_op('INPLACE_FLOOR_DIVIDE', 28)
  201. def_op('INPLACE_TRUE_DIVIDE', 29)
  202. def_op('SLICE+0', 30)
  203. def_op('SLICE+1', 31)
  204. def_op('SLICE+2', 32)
  205. def_op('SLICE+3', 33)
  206. def_op('STORE_SLICE+0', 40)
  207. def_op('STORE_SLICE+1', 41)
  208. def_op('STORE_SLICE+2', 42)
  209. def_op('STORE_SLICE+3', 43)
  210. def_op('DELETE_SLICE+0', 50)
  211. def_op('DELETE_SLICE+1', 51)
  212. def_op('DELETE_SLICE+2', 52)
  213. def_op('DELETE_SLICE+3', 53)
  214. def_op('INPLACE_ADD', 55)
  215. def_op('INPLACE_SUBTRACT', 56)
  216. def_op('INPLACE_MULTIPLY', 57)
  217. def_op('INPLACE_DIVIDE', 58)
  218. def_op('INPLACE_MODULO', 59)
  219. def_op('STORE_SUBSCR', 60)
  220. def_op('DELETE_SUBSCR', 61)
  221. def_op('BINARY_LSHIFT', 62)
  222. def_op('BINARY_RSHIFT', 63)
  223. def_op('BINARY_AND', 64)
  224. def_op('BINARY_XOR', 65)
  225. def_op('BINARY_OR', 66)
  226. def_op('INPLACE_POWER', 67)
  227. def_op('GET_ITER', 68)
  228. def_op('PRINT_EXPR', 70)
  229. def_op('PRINT_ITEM', 71)
  230. def_op('PRINT_NEWLINE', 72)
  231. def_op('PRINT_ITEM_TO', 73)
  232. def_op('PRINT_NEWLINE_TO', 74)
  233. def_op('INPLACE_LSHIFT', 75)
  234. def_op('INPLACE_RSHIFT', 76)
  235. def_op('INPLACE_AND', 77)
  236. def_op('INPLACE_XOR', 78)
  237. def_op('INPLACE_OR', 79)
  238. def_op('BREAK_LOOP', 80)
  239. def_op('LOAD_LOCALS', 82)
  240. def_op('RETURN_VALUE', 83)
  241. def_op('IMPORT_STAR', 84)
  242. def_op('EXEC_STMT', 85)
  243. def_op('YIELD_STMT', 86)
  244. def_op('POP_BLOCK', 87)
  245. def_op('END_FINALLY', 88)
  246. def_op('BUILD_CLASS', 89)
  247. HAVE_ARGUMENT = 90
  248. name_op('STORE_NAME', 90)
  249. name_op('DELETE_NAME', 91)
  250. def_op('UNPACK_SEQUENCE', 92)
  251. jrel_op('FOR_ITER', 93)
  252. name_op('STORE_ATTR', 95)
  253. name_op('DELETE_ATTR', 96)
  254. name_op('STORE_GLOBAL', 97)
  255. name_op('DELETE_GLOBAL', 98)
  256. def_op('DUP_TOPX', 99)
  257. def_op('LOAD_CONST', 100)
  258. hasconst.append(100)
  259. name_op('LOAD_NAME', 101)
  260. def_op('BUILD_TUPLE', 102)
  261. def_op('BUILD_LIST', 103)
  262. def_op('BUILD_MAP', 104)
  263. name_op('LOAD_ATTR', 105)
  264. def_op('COMPARE_OP', 106)
  265. hascompare.append(106)
  266. name_op('IMPORT_NAME', 107)
  267. name_op('IMPORT_FROM', 108)
  268. jrel_op('JUMP_FORWARD', 110)
  269. jrel_op('JUMP_IF_FALSE', 111)
  270. jrel_op('JUMP_IF_TRUE', 112)
  271. jabs_op('JUMP_ABSOLUTE', 113)
  272. jrel_op('FOR_LOOP', 114)
  273. name_op('LOAD_GLOBAL', 116)
  274. jabs_op('CONTINUE_LOOP', 119)
  275. jrel_op('SETUP_LOOP', 120)
  276. jrel_op('SETUP_EXCEPT', 121)
  277. jrel_op('SETUP_FINALLY', 122)
  278. def_op('LOAD_FAST', 124)
  279. haslocal.append(124)
  280. def_op('STORE_FAST', 125)
  281. haslocal.append(125)
  282. def_op('DELETE_FAST', 126)
  283. haslocal.append(126)
  284. def_op('SET_LINENO', 127)
  285. SET_LINENO = 127
  286. def_op('RAISE_VARARGS', 130)
  287. def_op('CALL_FUNCTION', 131)
  288. def_op('MAKE_FUNCTION', 132)
  289. def_op('BUILD_SLICE', 133)
  290. def_op('MAKE_CLOSURE', 134)
  291. def_op('LOAD_CLOSURE', 135)
  292. hasfree.append(135)
  293. def_op('LOAD_DEREF', 136)
  294. hasfree.append(136)
  295. def_op('STORE_DEREF', 137)
  296. hasfree.append(137)
  297. def_op('CALL_FUNCTION_VAR', 140)
  298. def_op('CALL_FUNCTION_KW', 141)
  299. def_op('CALL_FUNCTION_VAR_KW', 142)
  300. def_op('EXTENDED_ARG', 143)
  301. EXTENDED_ARG = 143
  302.  
  303. def _test():
  304.     if sys.argv[1:]:
  305.         if sys.argv[2:]:
  306.             sys.stderr.write('usage: python dis.py [-|file]\n')
  307.             sys.exit(2)
  308.         
  309.         fn = sys.argv[1]
  310.         if not fn or fn == '-':
  311.             fn = None
  312.         
  313.     else:
  314.         fn = None
  315.     if not fn:
  316.         f = sys.stdin
  317.     else:
  318.         f = open(fn)
  319.     source = f.read()
  320.     if fn:
  321.         f.close()
  322.     else:
  323.         fn = '<stdin>'
  324.     code = compile(source, fn, 'exec')
  325.     dis(code)
  326.  
  327. if __name__ == '__main__':
  328.     _test()
  329.  
  330.