Python源码分析之dis

一、简单例子

def add(a, b):
    return a + b
add_nums.py
import foo
a = [1, 'python']
a = 'a string'
def func():
    a = 1
    b = 257
    print(a + b)
print(a)
if __name__ == '__main__':
    func()
    foo.add(1, 2)
demo.py

运行python demo.py

在Python3 中,会自动生成一个 __pycache__ 文件夹,.pyc 文件会在这个文件夹中

二、背后的原理

1、编译

  Python在运行过程会有一个main module,也就是包含if __name__ == '__main__'或者是直接运行的py文件

  将用户自定义的module编译成PyCodeObject对象加载到内存里,编译结束后,将其以pyc文件保存在磁盘上

 

  这个 PyCodeObject 对象包含了 Python 源代码中的字符串,常量值,以及通过语法解析后编译生成的字节码指令。

2、如何生成pyc(Python bytecode字节码文件)文件

  1、运行的时候自定义的模块会自动生成

  2、python -m compileall file_path利用Python命令手动生成

  3、利用py_compile包进行编译

  >>> import py_compile
  >>> py_compile.compile('abc.py')

3、如何反编译字节码文件

  pyc文件是二进制文件,打开以后直接查看是乱码,python提供了dis包

  dis中常用的方法:

    dis.dis([bytesource]) bytesource可以是模块,类,方法或code对象

    利用dis处理文件

s = open('demo.py').read()
co = compile(s, 'demo.py', 'exec')
import dis
dis.dis(co)
dis处理文件
def myfunc(alist):
    return len(alist)
>>> dis.dis(myfunc)
  2           0 LOAD_GLOBAL              0 (len)
              3 LOAD_FAST                0 (alist)
              6 CALL_FUNCTION            1
              9 RETURN_VALUE
  1. the line number, for the first instruction of each line--------------------------对应源码文件的行号,每一行的首条指令才会显示
  2. the current instruction, indicated as -->,-----------------------------------------当前指令 -->
  3. a labelled instruction, indicated with >>, ---------------------------------------- 标签指令 >>
  4. the address of the instruction, ----------------------------------------------------- 指令的地址
  5. the operation code name, ----------------------------------------------------------- 操作代码名字
  6. operation parameters, and----------------------------------------------------------- 操作参数
  7. interpretation of the parameters in parentheses.------------------------------- 括号内参数的解释
  链接32.12.3. Python Bytecode Instructions有相应操作的简单说明大家可以具体参考

 具体的内容请参考:淡水网志的博客

 





 

posted @ 2017-06-22 08:17  skiler  阅读(1592)  评论(0编辑  收藏  举报