What is a byte code
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)
Python virtual machine executes compiled bytecode to complete program operation
Python creates bytecode files for imported modules
Bytecode file creation process
When a.py relies on b.py, such as import B in a.py
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
Otherwise compile b.py generate B.PYC and then load the newly generated bytecode file
BYTE-code Object
Each py file will contain many blocks (code block)
Each code block compiles the creation of a bytecode object (Pycodeobject)
The Pycodeobject object itself is nested, nested according to the structure of the code block
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