ZG manual python2.7.7 Source Code Analysis (4)--PYc bytecode file

Source: Internet
Author: User
Tags python script

What is a byte code
    1. The Python interpreter compiles the Python source code in the file when it executes the Python script file, and the result of the compilation is the byte code (bytecode)

    2. Python virtual machine executes compiled bytecode to complete program operation

    3. Python creates bytecode files for imported modules


Bytecode file creation process
    1. When a.py relies on b.py, such as import B in a.py

    2. Python first checks to see if there is a B.pyc file (bytecode file), if any, and the modification time is later than b.py, call B.pyc directly

    3. Otherwise compile b.py generate B.PYC and then load the newly generated bytecode file


BYTE-code Object
    1. Each py file will contain many blocks (code block)

    2. Each code block compiles the creation of a bytecode object (Pycodeobject)

    3. The Pycodeobject object itself is nested, nested according to the structure of the code block

    4. The child Pycodeobject object is saved in the co_consts variable of the parent object


code block
# entire file is a code block # code block 2class TestA (object): pass# code block 3class Testb (object): pass# code block 4DEF Show (): print ' show ... ' A = TestA () show ()


Structure of bytecode objects
/*  byte-code objects  */typedef struct {    PyObject_HEAD     int co_argcount;        /* code block  Number of parameters  */     int co_nlocals;     /*  number of local variables  */     int co_stacksize;       /*  Stack Space  */     int co_flags;       /* co_..., see below  */    PyObject *co_code;      /*  byte code instruction sequence  */    PyObject *co_consts;    /*  List of constants (child code blocks are also here)  * /    pyobject *co_names;     /*  string Symbol list  */     PyObject *co_varnames;  /*  local variable name  */    pyobject  *co_freevars; &Variables required for nbsp;/*  closures  */    PyObject *co_cellvars;       /*  local variable names referenced by nested functions  */    /* the rest doesn ' t count  For hash/cmp */    pyobject *co_filename;  /* py file path  */     pyobject *co_name;      /* code block   Function name or class name  */    int co_firstlineno;     /*  code block  Start line  */    pyobject *co_lnotab;    /*   byte code directive and source code correspondence relation  */    void *co_zombieframe;     /*  for optimization only  (see frameobject.c)  */    pyobject  *co_weakreflist;   /* to support weakrefs to code objects  */} pycodeobjeCt 


Internal implementation of bytecode files

The result of serializing a bytecode object to the hard disk is the bytecode file. This process is a recursive write process.

Static void w_object (pyobject *v, wfile *p) {    py_ssize_t i , n;    p->depth++;    /* ... */     else if  (Pycode_check (v))  {        pycodeobject  *co =  (pycodeobject *) v;        w_byte (TYPE_CODE,  P);         w_long (co->co_argcount, p);         w_long (co->co_nlocals, p);         w_long (co->co_stacksize, p);         w_long (co->co_ FLAGS, P);         w_object (co->co_code, p);         w_object (co->co_consts, p);         w_object (CO->CO_NAMES, P);         w_object (co->co_varnames, p);         w_object (co->co_freevars, p);         w_object (co->co_cellvars, p);         w _object (co->co_filename, p);         w_object (Co->co_name,  P);         w_long (co->co_firstlineno, p);         w_object (co->co_lnotab, p);    }     /* ... */}


Deserialization execution of bytecode files

Bytecode files Create the structure of bytecode objects at deserialization time, and the virtual machine executes the program functions according to the bytecode object at execution time.


Original link: ZG manual python2.7.7 Source Analysis (4)--PYc bytecode file

ZG manual python2.7.7 Source Code Analysis (4)--PYc bytecode file

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.