[Python源码学习]之bytecode
来源:互联网 发布:网站数据久了数据很多 编辑:程序博客网 时间:2024/05/18 01:02
源码:
xxx.py文件
或 字符串
==>
字节码:
可缓存在xxx.pyc
==>
结果
pythonX.dll
libpythonX.X.a
pythonX.dll
libpythonX.X.a
Py_CompileString***(...)
PyEval_Eval***(...)
compile
eval
- Python 代码首先被编译成 bytecode,然后才被解释器进行执行。
- bytecode 可被缓存动.pyc或.pyo文件内。
bytecode 对应源码中的 PyCodeObject 结构体对象
生成 .pyc 文件
代码中通过import使用到的.py文件会自动编译成.pyc文件,如何手动来编译呢?
- 交互模式或者代码中
>>> import py_compile>>> py_compile.compile('hello.py')>>>
- 或者使用 命令行
python3 -m py_compile hello.py
生成的文件(个人机子上的结果):
__pycache__/hello.cpython-32.pyc
将当前目录下的文件都编译成 .pyc 使用 compileall模块
python -m compileall .
这儿的py_compile和compileall使用的都是builtins模块的compile()函数
builtins
在python执行环境中,builtins模块中:
compile()
编译成字节码,code对象(PyCodeObject)
eval()、exec()
执行
一个例子:
>>> a = "1+2">>> b = compile(a, "test.py", 'single')>>> type(b)<class 'code'>>>> eval(b)3
它们对应C高层接口中的下面两类函数:
Py_CompileString***(...)
将python代码编译成bytecode
PyEval_Eval***(...)
执行这个bytecode
代码
compile() 和 eval()、exec() 是内建模块中的函数,所以瞅瞅
- Python/bltinmodule.c
中定义的方法:
static PyMethodDef builtin_methods[] = {//... {"compile", (PyCFunction)builtin_compile, METH_VARARGS|METH_KEYWORDS, compile_doc},//... {"eval", builtin_eval, METH_VARARGS, eval_doc}, {"exec", builtin_exec, METH_VARARGS, exec_doc},//... {NULL, NULL},};
其中:
builtin_compile() 调用 PyAST_CompileEx 或 Py_CompileStringExFlags
static PyObject *builtin_compile(PyObject *self, PyObject *args, PyObject *kwds){.... is_ast = PyAST_Check(cmd); if (is_ast) {... result = (PyObject*)PyAST_CompileEx(mod, filename,... goto finally; }... result = Py_CompileStringExFlags(str, filename, start[mode], &cf, optimize); goto finally;finally: Py_DECREF(filename_obj); return result;}
eval() 调用PyEval_EvalCode(对于bytecode)或PyRun_StringFlags(对字符串)
static PyObject *builtin_eval(PyObject *self, PyObject *args){... if (PyCode_Check(cmd)) { return PyEval_EvalCode(cmd, globals, locals); } cf.cf_flags = PyCF_SOURCE_IS_UTF8; str = source_as_string(cmd, "eval", "string, bytes or code", &cf);... (void)PyEval_MergeCompilerFlags(&cf); result = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf); Py_XDECREF(tmp); return result;}
恩,这样一来,总算将C代码和python代码联系上了。
PyCodeObject
前面提到的 bytecode,具体到源码中,就是PyCodeObject对象了(对应python环境中的code):
定义
先看一下该结构体的定义:
/* Bytecode object */typedef struct { PyObject_HEAD int co_argcount; /* #arguments, except *args */ int co_kwonlyargcount; /* #keyword only arguments */ int co_nlocals; /* #local variables */ int co_stacksize; /* #entries needed for evaluation stack */ int co_flags; /* CO_..., see below */ PyObject *co_code; /* instruction opcodes */ PyObject *co_consts; /* list (constants used) */ PyObject *co_names; /* list of strings (names used) */ PyObject *co_varnames; /* tuple of strings (local variable names) */ PyObject *co_freevars; /* tuple of strings (free variable names) */ PyObject *co_cellvars; /* tuple of strings (cell variable names) */ /* The rest doesn't count for hash or comparisons */ PyObject *co_filename; /* unicode (where it was loaded from) */ PyObject *co_name; /* unicode (name, for reference) */ int co_firstlineno; /* first source line number */ PyObject *co_lnotab; /* string (encoding addr<->lineno mapping) See Objects/lnotab_notes.txt for details. */ void *co_zombieframe; /* for optimization only (see frameobject.c) */ PyObject *co_weakreflist; /* to support weakrefs to code objects */} PyCodeObject;
- 各个成员什么含义?源码中有解释了,下面我们直接看看:如何在python中查看这些成员
查看code的成员
Python提供了简单的封装,于是,我们可以直接查看这些成员。例子:
>>> c = compile("1+2", "test.py", "single")>>> c.co_argcount0>>> c.co_codeb'd\x03\x00Fd\x02\x00S'>>> c.co_consts(1, 2, None, 3)>>> c.co_name'<module>'>>> c.co_filename'test.py'
其中 co_code 就是字节码了:d\x03\x00Fd\x02\x00S
那么如何理解这些代码??
字节码
co_code 写成10进制:100 3 0 70 100 2 0 83
100
指令码: LOAD_CONST
3
co_consts中的第3个常数
0
70
指令码: PRINT_EXPR
100
指令码: LOAD_CONST
2
co_consts中的第2个常数
0
83
指令码: RETURN_VALUE
指令码定义在文件 Include/opcode.h 中。
不过这样阅读指令码真的很难受,幸好,python提供了 dis 模块
dis
用它来看看前面的例子
>>> c = compile("1+2", "test.py", "single")>>> import dis>>> dis.dis(c) 1 0 LOAD_CONST 3 (3) 3 PRINT_EXPR 4 LOAD_CONST 2 (None) 7 RETURN_VALUE
恩,一目了然。最开始的那个1是行号,指令码前面的数字是它在co_code中的索引。
恩,dis 是很有用的东西,不过偶还没学会怎么利用它。
参考
http://docs.python.org/py3k/glossary.html#term-bytecode
http://docs.python.org/py3k/library/functions.html#compile
http://docs.python.org/py3k/library/dis.html#bytecodes
http://docs.python.org/py3k/library/code.html#module-code
http://docs.python.org/py3k/library/stdtypes.html#code-objects
http://www.python-course.eu/execute_script.php
- [Python源码学习]之bytecode
- python ByteCode
- IDF实验室之倒行逆施python ByteCode
- 一个python bytecode decompiler
- [Python源码学习]之数据类型
- [Python源码学习]之Py_InitializeEx
- [Python源码学习] 之 Python解释器
- IDF实验室:倒行逆施--python ByteCode
- IDF 逆向题 python ByteCode
- [Python源码学习]之PyObject和PyTypeObject
- [Python源码学习]之整数类型PyIntObject
- [Python源码学习]之模块路径
- Python源码学习之初始化(二)
- Eclipse插件之Bytecode Outline
- Protect Python Code with Bytecode Obfuscation
- [Python源码学习]之Very High Level Layer
- [Python源码学习]之对象创建与销毁
- Python源码学习之初始化(三)-PyDictObject的初始化
- 34个有用的ActionScript 3.0的API
- 加快Myeclipse启动速度
- Tweener 語法實習
- 不同的编译器、不同的硬件平台以及不同的操作系统,最终编译出来的结果一样吗?为什么?
- 快速排序
- [Python源码学习]之bytecode
- 人生感悟:如何做一个高价值的员工
- Flash开发者要了解的若干内容
- 死锁
- bash shell shortcuts
- 最短路
- seo中什么样的外链才算是高质量的
- MFC之计算器
- 用弦截法求解方程的根