【python源码剖析】——pyc文件的生成与解析Pycparser

pycparser

注意:需要用python2.5,实测2.5.2是通过的

1. py转pyc文件

写一个 demo.py 文件

#demo.py
class A:
    pass

def Fun():
    pass

a = A()
Fun()
cd $pycparser/bin
python2 pyc_generator.py demo

得到pyc文件

2. pyc解析成xml

cd $pycparser/bin
python2 to_xml.py demo.pyc

3. 结果

<PycFile>
<codeObject>
<argCount value="0"/>
<localCount value="0"/>
<stackSize value="3"/>
<flags value="64"/>
<code>
<str length="48" value="binary"/>
</code>
<consts>
<internStr index="0" length="1" value="A"/>
<codeObject>
<argCount value="0"/>
<localCount value="0"/>
<stackSize value="1"/>
<flags value="66"/>
<code>
<str length="8" value="binary"/>
</code>
<consts>
</consts>
<names>
<internStr index="1" length="8" value="__name__"/>
<internStr index="2" length="10" value="__module__"/>
</names>
<varNames>
</varNames>
<freeVars>
</freeVars>
<cellVars>
</cellVars>
<fileName>
<str length="49" value="/home/data/CM/17_python_code/Python-2.5.2/demo.py"/>
</fileName>
<name>
<strRef index="0" value="A"/>
</name>
<firstLineNo>
</firstLineNo>
<lnotab>
<str length="2" value="binary"/>
<op code="101" codeName="LOAD_NAME" arg="0" line="1"/>
<op code="90" codeName="STORE_NAME" arg="1" line="1"/>
<op code="82" codeName="LOAD_LOCALS" arg="none" line="2"/>
<op code="83" codeName="RETURN_VALUE" arg="none" line="2"/>
</lnotab>
</codeObject>
<codeObject>
<argCount value="0"/>
<localCount value="0"/>
<stackSize value="1"/>
<flags value="67"/>
<code>
<str length="4" value="binary"/>
</code>
<consts>
<NoneObject/>
</consts>
<names>
</names>
<varNames>
</varNames>
<freeVars>
</freeVars>
<cellVars>
</cellVars>
<fileName>
<str length="49" value="/home/data/CM/17_python_code/Python-2.5.2/demo.py"/>
</fileName>
<name>
<internStr index="3" length="3" value="Fun"/>
</name>
<firstLineNo>
</firstLineNo>
<lnotab>
<str length="2" value="binary"/>
<op code="100" codeName="LOAD_CONST" arg="0" line="5"/>
<op code="83" codeName="RETURN_VALUE" arg="none" line="5"/>
</lnotab>
</codeObject>
<NoneObject/>
</consts>
<names>
<strRef index="0" value="A"/>
<strRef index="3" value="Fun"/>
<internStr index="4" length="1" value="a"/>
</names>
<varNames>
</varNames>
<freeVars>
</freeVars>
<cellVars>
</cellVars>
<fileName>
<str length="49" value="/home/data/CM/17_python_code/Python-2.5.2/demo.py"/>
</fileName>
<name>
<str length="8" value="[module]"/>
</name>
<firstLineNo>
</firstLineNo>
<lnotab>
<str length="6" value="binary"/>
<op code="100" codeName="LOAD_CONST" arg="0" line="1"/>
<op code="100" codeName="LOAD_CONST" arg="4" line="1"/>
<op code="100" codeName="LOAD_CONST" arg="1" line="1"/>
<op code="132" codeName="MAKE_FUNCTION" arg="0" line="1"/>
<op code="131" codeName="CALL_FUNCTION" arg="0" line="1"/>
<op code="89" codeName="BUILD_CLASS" arg="none" line="1"/>
<op code="90" codeName="STORE_NAME" arg="0" line="1"/>
<op code="100" codeName="LOAD_CONST" arg="2" line="4"/>
<op code="132" codeName="MAKE_FUNCTION" arg="0" line="4"/>
<op code="90" codeName="STORE_NAME" arg="1" line="4"/>
<op code="101" codeName="LOAD_NAME" arg="0" line="7"/>
<op code="131" codeName="CALL_FUNCTION" arg="0" line="7"/>
<op code="90" codeName="STORE_NAME" arg="2" line="7"/>
<op code="101" codeName="LOAD_NAME" arg="1" line="8"/>
<op code="131" codeName="CALL_FUNCTION" arg="0" line="8"/>
<op code="1" codeName="POP_TOP" arg="none" line="8"/>
<op code="100" codeName="LOAD_CONST" arg="3" line="8"/>
<op code="83" codeName="RETURN_VALUE" arg="none" line="8"/>
</lnotab>
</codeObject>
</PycFile>

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值