How Python programs are executed

Source: Internet
Author: User

How Python programs are executed

1. Process overview
Python compiles the code (. py file) into bytecode, gives it to the bytecode virtual machine, and then the interpreter executes the bytecode instruction in one line, thus completing the execution of the program.

1.1python Compile the code (. py file) into bytecode, give it to the bytecode virtual machine, and the interpreter will execute the bytecode instruction from the compiled Pycodeobject object.
and executes this bytecode instruction in the current context to complete the execution of the program. The Python interpreter is actually the process of executing the file in a simulation operation. Pycodeobject Object
Contains the bytecode directive and all the static information of the program, but does not contain dynamic information when the program runs--execution Environment (Pyframeobject)

2. Byte code
The byte code in the Python interpreter program corresponds to the Pycodeobject object.
. pyc files are the representation of bytecode on disk

2.1 Overall: The execution of the program in the OS is inseparable from the two concepts: process and thread. These two concepts are simulated in Python, and the simulation processes and threads are pyinterpreterstate and
Pytreadstate. That is: Each pythreadstate corresponds to a frame stack, and the Python interpreter switches on multiple threads. When the Python interpreter starts executing, it first performs a
Some initialization operations, and finally into the Pyeval_evalframex function, it is the role of continuous reading compiled bytecode, and a piece of execution, similar to the CPU execution of instructions process. Inside the function
is primarily a switch structure that executes different code depending on the bytecode.

3.. pyc File
The Pycodeobject object is created when the module is loaded, and the import
Python test.py compiles the test.py into bytecode and interprets execution, but does not generate TEST.PYC
If test.py loads other modules, such as import urlib2, Python compiles the urlib2.py into bytecode, generates URLIB2.PYC, and interprets the bytecode
If you want to generate TEST.PYC, we can use the Python built-in module Py_compile to compile.
When the module is loaded, if both. Py and Pyc,python try to use. PYC, if the. PYc compilation time is earlier than the. Py modification time, recompile the. py and update. PYc.

4. Pycodeobject
The result of the Python code compilation is the Pycodeobject object

typedef struct {
Pyobject_head
int co_argcount; /* Number of positional parameters */
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 constant sets */
Pyobject *co_names; /* All symbol name sets */
Pyobject *co_varnames; /* Local variable name collection */
Pyobject *co_freevars; /* Variable name set for the closure of the packet */
Pyobject *co_cellvars; /* variable name set referenced by inner nested function */
/* The rest doesn ' t count for hash/cmp */
Pyobject *co_filename; /* The name of the code */
Pyobject *co_name; /* Module name | function name | class name */
int Co_firstlineno; /* The starting line number of the code block in the file */
Pyobject *co_lnotab; /* Byte code instruction and line number corresponding relation */
void *co_zombieframe; /* for optimization only (see FRAMEOBJECT.C) */
} Pycodeobject;


5.. pyc file format
When the module is loaded, the corresponding Pycodeobject object of the module is written to the. pyc file

6. Parsing byte code

6.1 Parsing Pycodeobject
Python provides built-in functions compile can compile Python code and view Pycodeobject objects

6.2 Format of the instruction sequence Co_code

Opcodeopargopcodeopcodeoparg ...
1 byte2 bytes1 byte1 byte2 bytes
Python's built-in dis module can parse Co_code

7. Execute byte code
The Python interpreter works by simulating the executable program and then X86 the machine, X86 the runtime stack frame as

The Python interpreter works by simulating this behavior. When a function call occurs, a new stack frame is created, and the implementation of the python corresponds to the Pyframeobject object.
Pyframeobject dynamic information when the object creator runs, that is, the execution environment

7.1 Pyframeobject

typedef struct _frame{
Pyobject_var_head//"Runtime stack" size is indeterminate
struct _frame *f_back; Executing the previous frame on the environment chain, many pyframeobject are linked together to form the execution environment chain list
Pycodeobject *f_code; Pycodeobject object, this frame is the context of this Pycodeobject object
Pyobject *f_builtins; Builtin name space
Pyobject *f_globals; Global name Space
Pyobject *f_locals; Local name space
Pyobject **f_valuestack; Stack bottom position of "runtime stack"
Pyobject **f_stacktop; Stack top position of "runtime stack"
//...
int f_lasti; The offset position of the previous bytecode instruction in F_code
int F_lineno; Source code line for the current byte code
//...

Dynamic memory, maintenance (local variable +cell object collection +free object collection + runtime stack) required space
Pyobject *f_localsplus[1];
} Pyframeobject;

Each Pyframeobject object maintains a Pycodeobject object, which indicates that the dynamic memory space object in each pyframeobject corresponds to a piece of code in the source code.

How Python programs are executed

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.