File size: 1,954 Bytes
01f2fb9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
import dis
import io
import re
import sys
import textwrap
from op_codes import op_codes_37
def fix_spacing(unformatted_decompiled_python):
decompiled_python = re.sub(r"[ \t]+", " ", unformatted_decompiled_python)
decompiled_python = re.sub(r"\n ", "\n", decompiled_python)
return decompiled_python
def get_byte_code(python_code):
code_object = compile(python_code, "f.py", "exec")
original_stdout = sys.stdout
string_output = io.StringIO()
sys.stdout = string_output
dis.dis(code_object)
sys.stdout = original_stdout
disassembled_pyc = string_output.getvalue()
return fix_spacing(textwrap.dedent(str(disassembled_pyc)).strip())
def extract_text_between_parentheses(input_string):
start_index = input_string.find("(")
end_index = input_string.rfind(")")
if start_index != -1 and end_index != -1 and start_index < end_index:
return input_string[start_index + 1 : end_index]
else:
return ""
def reformat_bytecode_line(input_string):
if input_string.startswith("Disassembly of <"):
return None
for code in op_codes_37:
if code in input_string:
reformatted_string = extract_text_between_parentheses(input_string)
return f"{code} {reformatted_string}".strip()
return input_string
def format_bytecode(bytecode: str):
formatted_strings = []
for line in bytecode.split("\n"):
reformatted_string = reformat_bytecode_line(line)
if reformatted_string is not None:
formatted_strings.append(reformatted_string)
return "\n".join(formatted_strings).strip()
if __name__ == "__main__":
file_path = sys.argv[1]
print(f"Converting python source code to byte code {file_path}")
with open(file_path, "r") as file:
file_contents = file.read()
print(file_contents)
byte_code = get_byte_code(file_contents)
print(format_bytecode(bytecode=byte_code))
|