python文件输出exe文件反汇编,如何完全反汇编Python源代码

I have been playing with the dis library to disassemble some Python source code, but I see that this does not recurse into functions or classes:

import dis

source_py = "test.py"

with open(source_py) as f_source:

source_code = f_source.read()

byte_code = compile(source_code, source_py, "exec")

dis.dis(byte_code)

All I see are entries such as:

54 456 LOAD_CONST 63 ()

459 MAKE_FUNCTION 0

462 STORE_NAME 20 (foo)

If the source file had a function foo(), I could obviously add something like the following to the sourcefile:

dis.dis(foo)

I cannot figure out how to do this without changing the source file and executing it. I would like to be able to extract the pertinent bytes from the compiled byte_code and pass them to dis.dis().

def sub_byte_code(byte_code, function_or_class_name):

sub_byte_code = xxxxxx

dis.dis(sub_byte_code)

I have considered wrapping the source code and executing dis.dis() as follows but I do not wish to execute the script:

source_code_dis = "import dis\n%s\ndis.dis(foo)\n" % (source_code)

exec(source_code_dis)

Is there perhaps a trick to calling it? e.g. dis.dis(byte_code, recurse=True)

解决方案

Import the file as a module and call dis.dis() on that module.

import dis

import test

dis.dis(test)

You can also do this from the command-line:

python -m dis test.py

For a module, it disassembles all functions.

Edit: As of python 3.7, dis.dis is recursive.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值