Python Program Execution Principle +__python

Source: Internet
Author: User
1. Process Overview

Python compiles the code (. py file) into bytecode, handing it to the bytecode virtual machine, and then the virtual machine executes a byte-code instruction, which completes the execution of the program. 2. Byte code

The byte code corresponds to the Pycodeobject object in the Python virtual machine program.
The. PYc file is a representation of the bytecode on disk. 3. PYC Documents

The time to create a Pycodeobject object is when the module loads, that is, import.
The Python test.py compiles the test.py into bytecode and interprets execution, but does not generate TEST.PYC.
If the test.py loads other modules, such as import Util,python compiles the util.py into bytecode, generates UTIL.PYC, and then interprets the byte code for execution.
If you want to generate TEST.PYC, we can compile it using the Python built-in module py_compile.
When you load a module, if both. py and. Pyc,python will attempt to use. PYc, If. PYc is compiled before the. Py modification time, recompile. PY and update. PYc. 4. Pycodeobject

The result of compiling Python code is the Pycodeobject object. typedef struct {Pyobject_head int co_argcount;/* position parameter number/int co_nlocals;/* local variable number/int co_stacksize;/* Stack size */INT Co_flags; Pyobject *co_code; /* Byte code instruction sequence * * Pyobject *co_consts; /* All Constants Set * * Pyobject *co_names; * * All symbolic names set/Pyobject *co_varnames; /* local variable name set * * Pyobject *co_freevars; /* Closed package variable name set */Pyobject *co_cellvars; /* Internal nested function reference variable name collection/* The rest doesn ' t count for hash/cmp */Pyobject *co_filename; /* code file name * * * Pyobject *co_name; /* Module name | function name | class name */int co_firstlineno; /* code block in the file in the starting line number * * Pyobject *co_lnotab; /* Byte code instruction and line number of the corresponding relationship * * void *co_zombieframe; * For optimization only (FRAMEOBJECT.C) */} Pycodeobject;

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19-20 typedef struct {     pyobject_head      int co_argcount;    & nbsp;    /* Position parameter number * *      int co_nlocals;         /* Number of local variables */     int co_stacksize;       /* Stack size * *      int co_flags;         pyobject * co_code;       /* Byte code instruction sequence */     pyobject * co_consts;     /* All Constants Set */     pyobject * co_names;     /* All symbol names set */     pyobject * co_varnames;   /* local variable name set */     pyobject * co_freevars; Set of variable names for   /* Closures/     pyobject * co_cellvars;   /* The set of variable names referenced by the internal nested function/    /* The rest doesn ' t count for hash/cmp */     Pyobject * CO_FILENAME;   //* code file name * * *      pyobject * CO_NAME;       /* Module name | function name | class name */     int co_firstlineno;     /* code block in the file starting line number */     pyobject * CO_LNOTAB;     /* Byte code instruction and line number of the corresponding relationship/     void * CO_ZOMBIEFRAME;   /* for optimization only (FRAMEOBJECT.C) */} Pycodeobject;  

5. pyc file Format

When the module is loaded, the module's corresponding Pycodeobject object is written to the. pyc file, in the following format:

6. Parsing byte code 6.1 parse Pycodeobject

Python provides built-in functions compile can compile Python code and view Pycodeobject objects as follows:

python code [test.py] s = "Hello" def func (): Print S Func ()

1 2 3 4 5 6 7 s = "Hello" def func (): Print S Func ()

Compile code in the Python interactive shell to get the Pycodeobject object:

DIR (CO) has listed the fields for Co, and would like to see a domain directly on the terminal output:

test.py's Pycodeobject 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\x 00d\x01\x00\x84\x00\x00z\x01\x00e\x01\x00\x83\x00\x00\x01d\x02\x00s '

1 2 3 4 5 6 7 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 a byte code Pycodeobject object for the function, see above co_consts[1]

func pycodeobject 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 '

1 2 3 4 5 6 7 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 sequence of instructions, a string of binary streams, and its format and parsing methods are shown in 6.2. 6.2 Analytic instruction sequence

format of instruction sequence Co_code

..
opcode Oparg opcode opcode Oparg .
1 byte 2 bytes 1 byte 1 byte 2 bytes

Python's built-in dis module can parse Co_code, as shown in the following figure:

test.py sequence of instructions

instruction sequence of func function

The first column represents the line number of the following instructions in the Py file;
The second column is the offset of the instruction in the sequence co_code;
The third column is the name of the instruction opcode, divided into two kinds of operands and no operands, and opcode is a byte integer in the instruction sequence;
The fourth column is the operand oparg, occupies two bytes in the instruction sequence, basically is the co_consts or the co_names subscript;
The fifth column with parentheses is the operand description. 7. Execute byte code

The principle of the Python virtual machine is to simulate the executable program and then X86 the operation on the machine, X86 the runtime stack frame as follows:

If test.py use C language to achieve, it will be the following: const char *s = "Hello"; void func () {printf ("%s\n", s);} int main () {func (); return 0;}

1 2 3 4 5 6 7 8 9 10 11 const char * s = "Hello"; void
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.