Python Program execution principle

Source: Internet
Author: User

Python program execution principle 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 one at a time to complete the execution of the program. 1.1python first compiles the code (. py file) into bytecode, gives it to the bytecode virtual machine, and then the interpreter executes the bytecode instruction from the compiled Pycodeobject object and executes the bytecode instruction in the current context, thus completing the execution of the program. The Python interpreter is actually the process of executing the file in a simulation operation. The Pycodeobject object contains the bytecode instruction and all the static information of the program, but does not contain the dynamic information when the program runs-execution Environment (Pyframeobject) 2. Bytecode bytecode corresponds to the Pycodeobject object in the Python interpreter program. The PYc file is a representation of the bytecode on disk 2.1 Overall: The execution of programs in the OS is inseparable from two concepts: processes and threads. These two concepts are simulated in Python, and the simulation processes and threads are pyinterpreterstate and pytreadstate respectively. That is: Each pythreadstate corresponds to a frame stack, and the Python interpreter switches on multiple threads. When the Python interpreter starts to execute, it performs some initialization and finally enters the Pyeval_evalframex function, which is to read the compiled bytecode continuously and execute a process similar to the CPU execution instructions. Inside the function is basically a switch structure that executes different code depending on the bytecode. 3. The. pyc file Pycodeobject object is created when the module is loaded, and Importpython 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. The compilation result of the Pycodeobjectpython code is the Pycodeobject object typedef struct {Pyobject_headint co_argcount;/* Number of positional parameters */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 constant sets */pyobject *co_names; /* All symbol names set */pyobject *co_varnames; /* local variable name collection */pyobject *co_freevars; /* The variable name set of the closure */pyobject *co_cellvars; /* The variable name set referenced by the inner nested function *//* the rest doesn ' t count for hash/cmp */pyobject *co_filename; /* Code name */pyobject *co_name; /* Module name | function name | class name */int Co_firstlineno; /* The starting line number of the code block in the file is */pyobject *co_lnotab; /* Byte code instruction and line number correspondence relation */void *co_zombieframe; /* for optimization only (see FRAMEOBJECT.C) */} pycodeobject;5. . pyc file format when loading modules, The Pycodeobject object that corresponds to the module is written to the. pyc file 6. Parse byte Code 6.1 parsing Pycodeobjectpython provides built-in functions compile can compile Python code and view Pycodeobject Object 6.2 instruction sequence Co_ Code format Opcodeopargopcodeopcodeoparg ... 1 byte2 bytes1 byte1 byte2 bytes python built-in dis module can parse co_code7. The principle of executing the bytecode Python interpreter is to simulate 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 Object Creator Runtime dynamic information, that is, execution Environment 7.1 pyframeobjecttypedef struct _frame{pyobject_var_head//"Runtime stack" size is indeterminate struct _ Frame *f_back; Implementation of the environment chain on the previous frame, a number of pyframeobject connected to form the implementation of the environment linked 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; "Runtime Stack" at the bottom of the stack position pyobject **f_stacktop; "Runtime stack" stack top position//... int f_lasti; The offset position of the previous bytecode instruction in F_code int f_lineno; The current bytecode corresponds to the source code line//...//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 each dynamic memory space object in the pyframeobject corresponds to a piece of code in the source code.

  

Python Program execution principle

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.