import marshal
fd = open('path/to/my.pyc', 'rb')
magic = fd.read(4) # 魔术数,与python版本相关
date = fd.read(4) # 编译日期
code_object = marshal.load(fd)
fd.close()
import types def inspect_code_object(co_obj, indent=''): print indent, "%s(lineno:%d)" % (co_obj.co_name, co_obj.co_firstlineno) for c in co_obj.co_consts: if isinstance(c, types.CodeType): inspect_code_object(c, indent + ' ') inspect_code_object(code_object) # 从第一个对象开始
class A: def __init__(self): pass def __repr__(self): return 'A()' a = A() print a
<module>(lineno:2) A(lineno:2) __init__(lineno:3) __repr__(lineno:5)
co_obj = compile(python_source_code, '<string>', 'exec')
2 0 LOAD_CONST 0 ('A')
3 LOAD_CONST 3 (())
6 LOAD_CONST 1 (<code object A at 0x42424242, file "<string>", line 2>)
9 MAKE_FUNCTION 0
12 CALL_FUNCTION 0
15 BUILD_CLASS
16 STORE_NAME 0 (A)
8 19 LOAD_NAME 0 (A)
22 CALL_FUNCTION 0
25 STORE_NAME 1 (a)
9 28 LOAD_NAME 1 (a)
31 PRINT_ITEM
32 PRINT_NEWLINE
33 LOAD_CONST 2 (None)
36 RETURN_VALUE
(1) a, b = 1, '2' (2) a, b = 1, e (3) a, b, c = 1, 2, e (4) a, b, c, d = 1, 2, 3, e
0 LOAD_CONST 5 ((1, '2')) 3 UNPACK_SEQUENCE 2 6 STORE_FAST 0 (a) 9 STORE_FAST 1 (b)
12 LOAD_CONST 1 (1) 15 LOAD_GLOBAL 0 (e) 18 ROT_TWO 19 STORE_FAST 0 (a) 22 STORE_FAST 1 (b)
25 LOAD_CONST 1 (1) 28 LOAD_CONST 3 (2) 31 LOAD_GLOBAL 0 (e) 34 ROT_THREE 35 ROT_TWO 36 STORE_FAST 0 (a) 39 STORE_FAST 1 (b) 42 STORE_FAST 2 (c)
45 LOAD_CONST 1 (1) 48 LOAD_CONST 3 (2) 51 LOAD_CONST 4 (3) 54 LOAD_GLOBAL 0 (e) 57 BUILD_TUPLE 4 60 UNPACK_SEQUENCE 4 63 STORE_FAST 0 (a) 66 STORE_FAST 1 (b) 69 STORE_FAST 2 (c) 72 STORE_FAST 3 (d)
func(arg1, arg2, keyword=SOME_VALUE, *unpack_list, **unpack_dict)
0 LOAD_NAME 0 (func)
3 LOAD_NAME 1 (arg1)
6 LOAD_NAME 2 (arg2)
9 LOAD_CONST 0 ('keyword')
12 LOAD_NAME 3 (SOME_VALUE)
15 LOAD_NAME 4 (unpack_list)
18 LOAD_NAME 5 (unpack_dict)
21 CALL_FUNCTION_VAR_KW 258
na = arg & 0xff # num args nk = (arg >> 8) & 0xff # num keywords n_to_pop = na + 2 * nk + CALL_EXTRA_ARG_OFFSET[op]
def factorial(n): if n <= 1: return 1 elif n == 2: return 2 return n * factorial(n - 1)
module_co = compile(python_source, '', 'exec') meth_co = module_co.co_consts[0]
3 0 LOAD_FAST 0 (n) 3 LOAD_CONST 1 (1) 6 COMPARE_OP 1 (<=) 9 POP_JUMP_IF_FALSE 16 <<< control flow 4 12 LOAD_CONST 1 (1) 15 RETURN_VALUE <<< control flow 5 >> 16 LOAD_FAST 0 (n) 19 LOAD_CONST 2 (2) 22 COMPARE_OP 2 (==) 25 POP_JUMP_IF_FALSE 32 <<< control flow 6 28 LOAD_CONST 2 (2) 31 RETURN_VALUE <<< control flow 7 >> 32 LOAD_FAST 0 (n) 35 LOAD_GLOBAL 0 (factorial) 38 LOAD_FAST 0 (n) 41 LOAD_CONST 1 (1) 44 BINARY_SUBTRACT 45 CALL_FUNCTION 1 48 BINARY_MULTIPLY 49 RETURN_VALUE <<< control flow
import opcode
RETURN_VALUE = 83
JUMP_FORWARD, JUMP_ABSOLUTE = 110, 113
FALSE_BRANCH_JUMPS = (111, 114) # JUMP_IF_FALSE_OR_POP, POP_JUMP_IF_FALSE
def find_blocks(meth_co):
blocks = {}
code = meth_co.co_code
finger_start_block = 0
i, length = 0, len(code)
while i < length:
op = ord(code[i])
i += 1
if op == RETURN_VALUE: # We force finishing the block after the return,
# dead code might still exist after though...
blocks[finger_start_block] = {
'length': i - finger_start_block - 1,
'exit': True
}
finger_start_block = i
elif op >= opcode.HAVE_ARGUMENT:
oparg = ord(code[i]) + (ord(code[i+1]) << 8)
i += 2
if op in opcode.hasjabs: # Absolute jump to oparg
blocks[finger_start_block] = {
'length': i - finger_start_block
}
if op == JUMP_ABSOLUTE: # Only uncond absolute jump
blocks[finger_start_block]['conditions'] = {
'uncond': oparg
}
else:
false_index, true_index = (oparg, i) if op in FALSE_BRANCH_JUMPS else (i, oparg)
blocks[finger_start_block]['conditions'] = {
'true': true_index,
'false': false_index
}
finger_start_block = i
elif op in opcode.hasjrel:
# Essentially do the same...
pass
return blocks
Block 0: {'length': 12, 'conditions': {'false': 16, 'true': 12}}
Block 12: {'length': 3, 'exit': True}
Block 16: {'length': 12, 'conditions': {'false': 32, 'true': 28}}
Block 28: {'length': 3, 'exit': True}
Block 32: {'length': 17, 'exit': True}
Basic blocks start_block_index := length := size of instructions condition := true | false | uncond -> target_index exit* := true
def to_dot(blocks):
cache = {}
def get_node_id(idx, buf):
if idx not in cache:
cache[idx] = 'node_%d' % idx
buf.append('%s [label="Block Index %d"];' % (cache[idx], idx))
return cache[idx]
buffer = ['digraph CFG {']
buffer.append('entry [label="CFG Entry"]; ')
buffer.append('exit [label="CFG Implicit Return"]; ')
for block_idx in blocks:
node_id = get_node_id(block_idx, buffer)
if block_idx == 0:
buffer.append('entry -> %s;' % node_id)
if 'conditions' in blocks[block_idx]:
for cond_kind in blocks[block_idx]['conditions']:
target_id = get_node_id(blocks[block_idx]['conditions'][cond_kind], buffer)
buffer.append('%s -> %s [label="%s"];' % (node_id, target_id, cond_kind))
if 'exit' in blocks[block_idx]:
buffer.append('%s -> exit;' % node_id)
buffer.append('}')
return 'n'.join(buffer)
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有