home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / Calibre / calibre-0.8.18.msi / file_262 / dis.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-09-09  |  5.4 KB  |  228 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.7)
  3.  
  4. import sys
  5. import types
  6. from opcode import *
  7. from opcode import __all__ as _opcodes_all
  8. __all__ = [
  9.     'dis',
  10.     'disassemble',
  11.     'distb',
  12.     'disco',
  13.     'findlinestarts',
  14.     'findlabels'] + _opcodes_all
  15. del _opcodes_all
  16. _have_code = (types.MethodType, types.FunctionType, types.CodeType, types.ClassType, type)
  17.  
  18. def dis(x = None):
  19.     if x is None:
  20.         distb()
  21.         return None
  22.     if None(x, types.InstanceType):
  23.         x = x.__class__
  24.     if hasattr(x, 'im_func'):
  25.         x = x.im_func
  26.     if hasattr(x, 'func_code'):
  27.         x = x.func_code
  28.     if hasattr(x, '__dict__'):
  29.         items = x.__dict__.items()
  30.         items.sort()
  31.         for name, x1 in items:
  32.             if isinstance(x1, _have_code):
  33.                 print 'Disassembly of %s:' % name
  34.                 
  35.                 try:
  36.                     dis(x1)
  37.                 except TypeError:
  38.                     msg = None
  39.                     print 'Sorry:', msg
  40.  
  41.                 print 
  42.                 continue
  43.     if hasattr(x, 'co_code'):
  44.         disassemble(x)
  45.     elif isinstance(x, str):
  46.         disassemble_string(x)
  47.     else:
  48.         raise TypeError, "don't know how to disassemble %s objects" % type(x).__name__
  49.  
  50.  
  51. def distb(tb = None):
  52.     if tb is None:
  53.         
  54.         try:
  55.             tb = sys.last_traceback
  56.         except AttributeError:
  57.             raise RuntimeError, 'no last traceback to disassemble'
  58.  
  59.         while tb.tb_next:
  60.             tb = tb.tb_next
  61.     disassemble(tb.tb_frame.f_code, tb.tb_lasti)
  62.  
  63.  
  64. def disassemble(co, lasti = -1):
  65.     code = co.co_code
  66.     labels = findlabels(code)
  67.     linestarts = dict(findlinestarts(co))
  68.     n = len(code)
  69.     i = 0
  70.     extended_arg = 0
  71.     free = None
  72.     while i < n:
  73.         c = code[i]
  74.         op = ord(c)
  75.         if i in linestarts:
  76.             if i > 0:
  77.                 print 
  78.             print '%3d' % linestarts[i],
  79.         else:
  80.             print '   ',
  81.         if i == lasti:
  82.             print '-->',
  83.         else:
  84.             print '   ',
  85.         if i in labels:
  86.             print '>>',
  87.         else:
  88.             print '  ',
  89.         print repr(i).rjust(4), opname[op].ljust(20),
  90.         i = i + 1
  91.         if op >= HAVE_ARGUMENT:
  92.             oparg = ord(code[i]) + ord(code[i + 1]) * 256 + extended_arg
  93.             extended_arg = 0
  94.             i = i + 2
  95.             if op == EXTENDED_ARG:
  96.                 extended_arg = oparg * 0x10000L
  97.             print repr(oparg).rjust(5),
  98.             if op in hasconst:
  99.                 print '(' + repr(co.co_consts[oparg]) + ')',
  100.             elif op in hasname:
  101.                 print '(' + co.co_names[oparg] + ')',
  102.             elif op in hasjrel:
  103.                 print '(to ' + repr(i + oparg) + ')',
  104.             elif op in haslocal:
  105.                 print '(' + co.co_varnames[oparg] + ')',
  106.             elif op in hascompare:
  107.                 print '(' + cmp_op[oparg] + ')',
  108.             elif op in hasfree:
  109.                 if free is None:
  110.                     free = co.co_cellvars + co.co_freevars
  111.                 print '(' + free[oparg] + ')',
  112.             
  113.         print 
  114.  
  115.  
  116. def disassemble_string(code, lasti = -1, varnames = None, names = None, constants = None):
  117.     labels = findlabels(code)
  118.     n = len(code)
  119.     i = 0
  120.     while i < n:
  121.         c = code[i]
  122.         op = ord(c)
  123.         if i == lasti:
  124.             print '-->',
  125.         else:
  126.             print '   ',
  127.         if i in labels:
  128.             print '>>',
  129.         else:
  130.             print '  ',
  131.         print repr(i).rjust(4), opname[op].ljust(15),
  132.         i = i + 1
  133.         if op >= HAVE_ARGUMENT:
  134.             oparg = ord(code[i]) + ord(code[i + 1]) * 256
  135.             i = i + 2
  136.             print repr(oparg).rjust(5),
  137.             if op in hasconst:
  138.                 if constants:
  139.                     print '(' + repr(constants[oparg]) + ')',
  140.                 else:
  141.                     print '(%d)' % oparg,
  142.             elif op in hasname:
  143.                 if names is not None:
  144.                     print '(' + names[oparg] + ')',
  145.                 else:
  146.                     print '(%d)' % oparg,
  147.             elif op in hasjrel:
  148.                 print '(to ' + repr(i + oparg) + ')',
  149.             elif op in haslocal:
  150.                 if varnames:
  151.                     print '(' + varnames[oparg] + ')',
  152.                 else:
  153.                     print '(%d)' % oparg,
  154.             elif op in hascompare:
  155.                 print '(' + cmp_op[oparg] + ')',
  156.             
  157.         print 
  158.  
  159. disco = disassemble
  160.  
  161. def findlabels(code):
  162.     labels = []
  163.     n = len(code)
  164.     i = 0
  165.     while i < n:
  166.         c = code[i]
  167.         op = ord(c)
  168.         i = i + 1
  169.         if op >= HAVE_ARGUMENT:
  170.             oparg = ord(code[i]) + ord(code[i + 1]) * 256
  171.             i = i + 2
  172.             label = -1
  173.             if op in hasjrel:
  174.                 label = i + oparg
  175.             elif op in hasjabs:
  176.                 label = oparg
  177.             if label >= 0:
  178.                 if label not in labels:
  179.                     labels.append(label)
  180.                 
  181.             
  182.         return labels
  183.  
  184.  
  185. def findlinestarts(code):
  186.     byte_increments = [ ord(c) for c in code.co_lnotab[0::2] ]
  187.     line_increments = [ ord(c) for c in code.co_lnotab[1::2] ]
  188.     lastlineno = None
  189.     lineno = code.co_firstlineno
  190.     addr = 0
  191.     for byte_incr, line_incr in zip(byte_increments, line_increments):
  192.         if byte_incr:
  193.             if lineno != lastlineno:
  194.                 yield (addr, lineno)
  195.                 lastlineno = lineno
  196.             addr += byte_incr
  197.         lineno += line_incr
  198.     
  199.     if lineno != lastlineno:
  200.         yield (addr, lineno)
  201.  
  202.  
  203. def _test():
  204.     if sys.argv[1:]:
  205.         if sys.argv[2:]:
  206.             sys.stderr.write('usage: python dis.py [-|file]\n')
  207.             sys.exit(2)
  208.         fn = sys.argv[1]
  209.         if not fn or fn == '-':
  210.             fn = None
  211.         
  212.     else:
  213.         fn = None
  214.     if fn is None:
  215.         f = sys.stdin
  216.     else:
  217.         f = open(fn)
  218.     source = f.read()
  219.     if fn is not None:
  220.         f.close()
  221.     else:
  222.         fn = '<stdin>'
  223.     code = compile(source, fn, 'exec')
  224.     dis(code)
  225.  
  226. if __name__ == '__main__':
  227.     _test()
  228.