.pyc文件的结构体PyCodeObject

python执行程序时生成的pyc文件里面是,PyCodeObject 的结构体构成,每个命名空间(函数名、import模块等)都会形成一个core block,一个python程序的所有命名空间生成core block记录到pyc里,就是Python的在执行时可以把pyc的内容调用到内存,进行执行。有时候是不产生pyc文件的,也就是执行时候内存的东西没有写到pyc文件,就没有pyc文件产生。
 
/* Bytecode object */
typedef struct {
    PyObject_HEAD
    int co_argcount;            /* #arguments, except *args */
    int co_nlocals;             /* #local variables */
    int co_stacksize;           /* #entries needed for evaluation stack */
    int co_flags;               /* CO_..., see below */
    PyObject *co_code;          /* instruction opcodes */
    PyObject *co_consts;        /* list (constants used) */
    PyObject *co_names;         /* list of strings (names used) */
    PyObject *co_varnames;      /* tuple of strings (local variable names) */
    PyObject *co_freevars;      /* tuple of strings (free variable names) */
    PyObject *co_cellvars;      /* tuple of strings (cell variable names) */
    /* The rest doesn't count for hash/cmp */
    PyObject *co_filename;      /* string (where it was loaded from) */
    PyObject *co_name;          /* string (name, for reference) */
    int co_firstlineno;         /* first source line number */
    PyObject *co_lnotab;        /* string (encoding addr<->lineno mapping) */
    void *co_zombieframe;     /* for optimization only (see frameobject.c) */
} PyCodeObject;
 
 
co_argcount :  Code Block的位置参数个数,比如说一个函数的位置参数个数
co_nlocals:       Code Block中局部变量的个数,包括其中位置参数的个数
co_stacksize:    执行该段Code Block需要的栈空间
co_flags:           N/A
co_code:          Code Block编译所得的字节码指令序列。以PyStingObjet的形式存在
co_consts:       PyTupleObject对象,保存CodeBlock中的所常量
co_names:       PyTupleObject对象,保存CodeBlock中的所有符号
co_varnames:    Code Block中的局部变量名集合
co_freevars:     Python实现闭包需要用的东西
co_cellvars:     Code Block中内部嵌套函数所引用的局部变量名集合
co_filename:   Code Block所对应的.py文件的完整路径
co_name:        Code Block的名字,通常是函数名或类名
co_firstlineno: Code Block在对应的.py文件中起始行
co_lnotab:        字节码指令与.py文件中source code行号的对应关系,以PyStringObject的形式存在
 
 
 
下面进行一个例子:
 
demo.py
class A:
    pass
 
def Fun():
    pass
 
a=A()
Fun()
 
 
F:\>python
Python 3.6.3 (v3.6.3:2c5fed8, Oct  3 2017, 18:11:49) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> source=open('demo.py').read()
>>> co=compile(source,'demo.py','exec')
>>> type(co)
<class 'code'>
>>> dir(co)
['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'co_argcount', 'co_cellvars', 'co_code', 'co_consts', 'co_filename', 'co_firstlineno', 'co_flags', 'co_freevars', 'co_kwonlyargcount', 'co_lnotab', 'co_name', 'co_names', 'co_nlocals', 'co_stacksize', 'co_varnames']
>>> print(co.co_names)
('demo', 'py', 'A', 'Fun', 'a')
>>> print(co.co_filename)
demo.py
>>>
 
 
 
 
 
 
 
 
 
 
 
.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

posted on 2018-01-19 17:56  凯特春  阅读(794)  评论(0编辑  收藏  举报