Implementation principle of Python Program (1)

Source: Internet
Author: User

1. Process Overview

Python first compiles the code. py file) into bytecode and delivers it to the bytecode Virtual Machine. Then, the virtual machine executes the bytecode command one by one to complete program execution.

2. bytecode

The bytecode corresponds to the PyCodeObject object in the Python virtual machine program.
The. pyc file is the representation of bytecode on the disk.

3. pyc File

The Creation Time of the PyCodeObject object is when the module is loaded, that is, import.
Python test. py will compile test. py into bytecode and explain the execution, but will not generate test. pyc.
If test. py loads other modules, such as import util, Python will compile util. py into bytecode, generate util. pyc, and then explain and execute the bytecode.
To generate test. pyc, we can use the Python built-in module py_compile for compilation.
When a module is loaded, if it exists at the same time. py and. pyc, Python will try to use. pyc, if. pyc Compilation Time is earlier. py, then re-compile. py and update. pyc.

4. PyCodeObject

The Python code compilation result is a PyCodeObject object.

 
 
  1. Typedef struct {
  2. PyObject_HEAD
  3. Int co_argcount;/* Number of location parameters */
  4. Int co_nlocals;/* Number of local variables */
  5. Int co_stacksize;/* stack size */
  6. Int co_flags;
  7. PyObject * co_code;/* sequence of bytecode instructions */
  8. PyObject * co_consts;/* all constants */
  9. PyObject * co_names;/* set of all Symbol names */
  10. PyObject * co_varnames;/* set of local variable names */
  11. PyObject * co_freevars;/* Set of variable names used for closure */
  12. PyObject * co_cellvars;/* Set of variable names referenced by internal nested functions */
  13. /* The rest doesn' t count for hash/cmp */
  14. PyObject * co_filename;/* Name of the code file */
  15. PyObject * co_name;/* Module name | function name | class name */
  16. Int co_firstlineno;/* start line number of the code block in the file */
  17. PyObject * co_lnotab;/* ing between bytecode commands and row numbers */
  18. Void * co_zombieframe;/* for optimization only (see frameobject. c )*/
  19. } PyCodeObject;
5. pyc File Format

When the module is loaded, the PyCodeObject object corresponding to the module is written into the. pyc file. The format is as follows:

6. Analyze bytecode

6.1 parse PyCodeObject

Python provides the built-in function compile to compile Python code and view PyCodeObject objects, as follows:

Python code [test. py]

 
 
  1. s = ”hello” 
  2.  
  3. def func(): 
  4.     print s 
  5.  
  6. func() 

Compile the code in the Python Interactive shell to get the PyCodeObject object:

Dir (co) has listed various co domains. To view a specific domain, output it directly on the terminal:

PyCodeObject of test. py

 
 
  1. co.co_argcount    0 
  2. co.co_nlocals     0 
  3. co.co_names       (‘s’, ’func’) 
  4. co.co_varnames    (‘s’, ’func’) 
  5. co.co_consts      (‘hello’, <code object func at 0x2aaeeec57110, file ”test.py”, line 3>, None) 
  6. 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 the bytecode PyCodeObject object for the function. For details, refer to co_consts [1].

PyCodeObject of func

 
 
  1. func.co_argcount   0 
  2. func.co_nlocals    0 
  3. func.co_names      (‘s’,) 
  4. func.co_varnames   () 
  5. func.co_consts     (None,) 
  6. func.co_code       ‘t\x00\x00GHd\x00\x00S’ 

Co_code is a command sequence and a binary stream. For the format and resolution method of co_code, see 6.2.

6.2 parsing command sequence

Command Sequence co_code format

Opcode Oparg Opcode Opcode Oparg ...
1 byte 2 bytes 1 byte 1 byte 2 bytes  


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.