1. Process Overview
Python first compiles the code. py file) into bytecode and delivers it to the bytecode Virtual Machine. Then, the virtual machine executes the bytecode command one by one to complete program execution.
2. bytecode
The bytecode corresponds to the PyCodeObject object in the Python virtual machine program.
The. pyc file is the representation of bytecode on the disk.
3. pyc File
The Creation Time of the PyCodeObject object is when the module is loaded, that is, import.
Python test. py will compile test. py into bytecode and explain the execution, but will not generate test. pyc.
If test. py loads other modules, such as import util, Python will compile util. py into bytecode, generate util. pyc, and then explain and execute the bytecode.
To generate test. pyc, we can use the Python built-in module py_compile for compilation.
When a module is loaded, if it exists at the same time. py and. pyc, Python will try to use. pyc, if. pyc Compilation Time is earlier. py, then re-compile. py and update. pyc.
4. PyCodeObject
The Python code compilation result is a PyCodeObject object.
- Typedef struct {
- PyObject_HEAD
- Int co_argcount;/* Number of location parameters */
- Int co_nlocals;/* Number of local variables */
- Int co_stacksize;/* stack size */
- Int co_flags;
- PyObject * co_code;/* sequence of bytecode instructions */
- PyObject * co_consts;/* all constants */
- PyObject * co_names;/* set of all Symbol names */
- PyObject * co_varnames;/* set of local variable names */
- PyObject * co_freevars;/* Set of variable names used for closure */
- PyObject * co_cellvars;/* Set of variable names referenced by internal nested functions */
- /* The rest doesn' t count for hash/cmp */
- PyObject * co_filename;/* Name of the code file */
- PyObject * co_name;/* Module name | function name | class name */
- Int co_firstlineno;/* start line number of the code block in the file */
- PyObject * co_lnotab;/* ing between bytecode commands and row numbers */
- Void * co_zombieframe;/* for optimization only (see frameobject. c )*/
- } PyCodeObject;
5. pyc File Format
When the module is loaded, the PyCodeObject object corresponding to the module is written into the. pyc file. The format is as follows:
6. Analyze bytecode
6.1 parse PyCodeObject
Python provides the built-in function compile to compile Python code and view PyCodeObject objects, as follows:
Python code [test. py]
- s = ”hello”
-
- def func():
- print s
-
- func()
Compile the code in the Python Interactive shell to get the PyCodeObject object:
Dir (co) has listed various co domains. To view a specific domain, output it directly on the terminal:
PyCodeObject of test. py
- co.co_argcount 0
- co.co_nlocals 0
- co.co_names (‘s’, ’func’)
- co.co_varnames (‘s’, ’func’)
- co.co_consts (‘hello’, <code object func at 0x2aaeeec57110, file ”test.py”, line 3>, None)
- co.co_code ’d\x00\x00Z\x00\x00d\x01\x00\x84\x00\x00Z\x01\x00e\x01\x00\x83\x00\x00\x01d\x02\x00S’
The Python interpreter will also generate the bytecode PyCodeObject object for the function. For details, refer to co_consts [1].
PyCodeObject of func
- func.co_argcount 0
- func.co_nlocals 0
- func.co_names (‘s’,)
- func.co_varnames ()
- func.co_consts (None,)
- func.co_code ‘t\x00\x00GHd\x00\x00S’
Co_code is a command sequence and a binary stream. For the format and resolution method of co_code, see 6.2.
6.2 parsing command sequence
Command Sequence co_code format
Opcode |
Oparg |
Opcode |
Opcode |
Oparg |
... |
1 byte |
2 bytes |
1 byte |
1 byte |
2 bytes |
|