The execution principle of the Python program

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 symbol names SET * *

Pyobject *co_varnames; /* Local variable name collection * *

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; /* The starting line number of the code block in the file * * *

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. Analysis of byte code

6.1 Parsing 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 ()

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 ',, None)

Co.co_code ' dx00x00zx00x00dx01x00x84x00x00zx01x00ex01x00x83x00x00x01dx02x00s '

The Python interpreter will also generate a byte code Pycodeobject object for the function, see above co_consts[1]

Func's Pycodeobject

Func.co_argcount 0

Func.co_nlocals 0

Func.co_names (' s ',)

Func.co_varnames ()

Func.co_consts (None,)

Func.co_code ' tx00x00ghdx00x00s '

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

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, will be the following:

const char *s = "Hello";

void Func () {

printf ("%sn", s);

}

int main () {

Func ();

return 0;

}

The principle of Python virtual machines is to simulate these behaviors. When a function call occurs, a new stack frame is created, and the corresponding Python implementation is the Pyframeobject object.

7.1 Pyframeobject

typedef struct _FRAME {

Pyobject_var_head

struct _frame *f_back; /* Caller's frame/*

Pycodeobject *f_code; /* frame corresponding to the byte code object * *

Pyobject *f_builtins; /* Built-in name space * *

Pyobject *f_globals; /* Global name space * *

Pyobject *f_locals; /* Local name space * *

Pyobject **f_valuestack; /* Run time Stack Bottom * * *

Pyobject **f_stacktop; /* Run time stack top * *

.......

}

So that's what the Python runtime stack looks like:

7.2 Execution Instructions

When executing a test.py byte code, a stack frame is created first, the following is the current stack frame with F, and the execution procedure is commented as follows:

test.py collection of symbolic names and constants

Co.co_names (' s ', ' func ')

Co.co_consts (' Hello ',, None)

  test.py的指令序列

  上面的CALL_FUNCTION指令执行时,会创建新的栈帧,并执行func的字节码指令,以下用f表示当前栈帧,func的字节码执行过程如下:

  func函数的符号名集合和常量集合

  func.co_names (‘s’,)

  func.co_consts (None,)

  func函数的指令序列

  7.3 查看栈帧

  如果你想查看当前栈帧,Python提供了sys._getframe()方法可以获取当前栈帧,你只需要在代码里加入代码如下:

  def func():

  import sys

  frame = sys._getframe()

  print frame.f_locals

  print frame.f_globals

  print frame.f_back.f_locals

  #你可以打印frame的各个域

  print s

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.