Python Python interior internal execution process execution

 

Python internal execution process

First, an overview of the compilation process

  When we execute Python code in the Python interpreter using the four process "dismantling" our code, the CPU executes eventually returned to the user.

  First, when a user types the code to deal with Python when will carry out a lexical analysis, for example, when a user types a keyword or keywords entered incorrectly, lexical analysis will be triggered, incorrect code will not be executed.

  The next step will be parsed Python, such as "for i in test:" in, test after the colon is written as if the other symbols, the code still will not be executed.

  Here enters the most critical process, before executing Python, Python will generate .pyc file that is byte code, and it will be the last generation if we are not careful modify the bytecode, Python next recompile the program bytecode file compare, if you do not match will be modified bytecode file cover to ensure the accuracy of the compiled byte code each time.

  So what is bytecode? Byte code corresponding to the virtual machine program in the Python is PyCodeObject object. .pyc byte code file is in the form of a disk. Is simply the process of the compiled code, the first code will function, object classification process etc., and then generates a byte code file. With bytecode files, the CPU can directly identify the byte code files are processed, and then can perform the Python.

Second, the process illustrated

Third, compiled bytecode

  Python has a built-in function compile (), the source file can be compiled into codeobject, first look at the description of the function:

  compile(...) compile(source, filename, mode[, flags[, dont_inherit]]) -> code object

  Parameter 1: The contents of the source file string

  Parameter 2: source file name

  Parameter 3: exec- compiled module, single- compile a statement, eval- an expression commonly used to compile the first three parameters is enough

  Example of use:

#src_file.py

#some function

def f(d=0):

    c=1

    print "hello"

a=9

b=8

f()

 

A = Open >>> ( ' src_file.py ' , ' R & lt ' ) .read ()     # compiled mode command open the source file

>>> co=compile(a,'src_file','exec')

>>> type(co)

<type ' code ' >     # compile a target codeobject

 

Fourth, the object attribute codeobject

  codeobject what variables, analyze the content of the section connected:

>>> Print co.co_names     # all symbolic names 

( ' F ' , ' A ' , ' B ' )

 

>>> Print co.co_name     # module name, function name, class name

<module>

 

>>> Print co.co_consts     # set of constants, and the function f int two constants A, B, D 

(0, <Object code AT 0xb7273b18 f, File " in SRC_FILE of " , Line 2>,. 9,. 8 , None)

 

>>> Print co.co_consts [. 1] .co_varnames     # can be seen that the function f is a codeobject, the local variable print f 

( ' C ' ,)

 

>>> Print co.co_code     # bytecode instruction

dZdZdZedS

 

>>> Print co.co_consts [. 1] .co_firstlineno     # code blocks in the file starting line number

2

 

>>> Print co.co_stacksize     # code stack size

2

 

>>> Print co.co_filename     # filename 

in SRC_FILE of     # module name, function name, class name

 

  codeobject of co_code represents the byte code, the bytecode What is the meaning? We can use the dis module decompile python of:

import dis

dis.dis (co)

>>> output

 2        0 LOAD_CONST               0 (0)

          3 LOAD_CONST               1 (<code object f at 0xb7273b18, file "src_file", line 2>)

          6 MAKE_FUNCTION            1

          9 STORE_NAME               0 (f)

 5        12 LOAD_CONST              2 (9)

          15 STORE_NAME              1 (a)

 
 6        18 LOAD_CONST              3 (8)

          21 STORE_NAME              2 (b)

 
 7        24 LOAD_NAME               0 (f)

          27 CALL_FUNCTION           0

          30 POP_TOP            

          31 LOAD_CONST              4 (None)

          34 RETURN_VALUE

 

  From the point of view of the results of decompiling, python bytecode actually imitate a compilation of x86, the code will be compiled into a one instruction to a virtual cpu to do it.

  • The first column: row number
  • Second column: instruction in the code block offset
  • The third column: Instruction
  • The fourth column: operand
  • Fifth column: operand instructions

 

 

https://blog.csdn.net/helloxiaozhe/article/details/78104975

 

Guess you like

Origin www.cnblogs.com/rgxx/p/12029124.html