|
3 | 3 | import sys |
4 | 4 | # from pprint import pprint |
5 | 5 |
|
6 | | -filename = sys.argv[1] |
7 | | -with open(filename, 'rU') as f: |
8 | | - code = f.read() |
9 | 6 |
|
10 | | -code = compile(code, filename, "exec") |
11 | | -print("CONSTS: {0}".format(code.co_consts)) |
12 | | -print("NAMES: {0}".format(code.co_names)) |
| 7 | +def parse_co_code_to_str(code): |
| 8 | + c = byteplay.Code.from_code(code) |
| 9 | +# pprint(c.code) |
| 10 | +# print(list(c.code)) |
| 11 | + |
| 12 | + def format_label(l): |
| 13 | + return "LABEL, {0}".format(id(l)) |
| 14 | + |
| 15 | + for op in c.code: |
| 16 | + # Byteplay print the actual argument instead of the index. But we are |
| 17 | + # Converting them back to the index in case we want to use other bytecode |
| 18 | + # disassembler in the future. |
| 19 | + # Here is a list of meanings for the arguments |
| 20 | + # The argument of opcodes in hasconst is the actual constant. |
| 21 | + # The argument of opcodes in hasname is the name, as a string. |
| 22 | + # The argument of opcodes in hasjump is a Label instance, which should point to a specific location in the code list. |
| 23 | + # The argument of opcodes in haslocal is the local variable name, as a string. |
| 24 | + # The argument of opcodes in hascompare is the string representing the comparison operator. |
| 25 | + # The argument of opcodes in hasfree is the name of the cell or free variable, as a string. |
| 26 | + if op[0] in byteplay.hasconst: |
| 27 | + print("{0}, {1}".format(op[0], code.co_consts.index(op[1]))) |
| 28 | + #print("{0}, {1}".format(op[0], None)) |
| 29 | + elif op[0] in byteplay.hasname: |
| 30 | + print("{0}, {1}".format(op[0], code.co_names.index(op[1]))) |
| 31 | + elif op[0] in byteplay.hasjump: |
| 32 | + print("{0}, {1}".format(op[0], id(op[1]))) |
| 33 | + elif op[0] in byteplay.hascompare: |
| 34 | + print("{0}, {1}".format(op[0], byteplay.cmp_op.index(op[1]))) |
| 35 | + elif type(op[0]) == byteplay.Label: |
| 36 | + assert(op[1] == None) |
| 37 | + print(format_label(op[0])) # Is the second argument always None? |
| 38 | + # print("{0}, {1}".format(format_label(op[0]), op[1])) |
| 39 | + else: |
| 40 | + print("{0}, {1}".format(op[0], op[1])) |
| 41 | + |
| 42 | +def main(): |
| 43 | + |
| 44 | + filename = sys.argv[1] |
| 45 | + with open(filename, 'rU') as f: |
| 46 | + code = f.read() |
| 47 | + |
| 48 | + code = compile(code, filename, "exec") |
| 49 | + print("CONSTS: {0}".format(code.co_consts)) |
| 50 | + print("NAMES: {0}".format(code.co_names)) |
13 | 51 | # print(code.co_varnames) |
14 | 52 | # print(list(bytearray(code.co_code))) |
15 | 53 | # for elem in list(bytearray(code.co_code)): |
16 | 54 | # print(dis.opname[elem]) |
17 | 55 | # dis_output = dis.dis(code) |
18 | 56 |
|
19 | | -c = byteplay.Code.from_code(code) |
20 | | -# pprint(c.code) |
21 | | -# print(list(c.code)) |
| 57 | + parse_co_code_to_str(code) |
22 | 58 |
|
23 | | -def format_label(l): |
24 | | - return "LABEL, {0}".format(id(l)) |
25 | | - |
26 | | -for op in c.code: |
27 | | - # Byteplay print the actual argument instead of the index. But we are |
28 | | - # Converting them back to the index in case we want to use other bytecode |
29 | | - # disassembler in the future. |
30 | | - # Here is a list of meanings for the arguments |
31 | | - # The argument of opcodes in hasconst is the actual constant. |
32 | | - # The argument of opcodes in hasname is the name, as a string. |
33 | | - # The argument of opcodes in hasjump is a Label instance, which should point to a specific location in the code list. |
34 | | - # The argument of opcodes in haslocal is the local variable name, as a string. |
35 | | - # The argument of opcodes in hascompare is the string representing the comparison operator. |
36 | | - # The argument of opcodes in hasfree is the name of the cell or free variable, as a string. |
37 | | - if op[0] in byteplay.hasconst: |
38 | | - print("{0}, {1}".format(op[0], code.co_consts.index(op[1]))) |
39 | | - #print("{0}, {1}".format(op[0], None)) |
40 | | - elif op[0] in byteplay.hasname: |
41 | | - print("{0}, {1}".format(op[0], code.co_names.index(op[1]))) |
42 | | - elif op[0] in byteplay.hasjump: |
43 | | - print("{0}, {1}".format(op[0], id(op[1]))) |
44 | | - elif op[0] in byteplay.hascompare: |
45 | | - print("{0}, {1}".format(op[0], byteplay.cmp_op.index(op[1]))) |
46 | | - elif type(op[0]) == byteplay.Label: |
47 | | - assert(op[1] == None) |
48 | | - print(format_label(op[0])) # Is the second argument always None? |
49 | | - # print("{0}, {1}".format(format_label(op[0]), op[1])) |
50 | | - else: |
51 | | - print("{0}, {1}".format(op[0], op[1])) |
| 59 | +if __name__ == "__main__": |
| 60 | + main() |
0 commit comments