您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 信息化管理 > Python源码剖析(Robert+Chen)
第1页共13页Python源码剖析——SmallPythonsearch.pythoner@gmail.com)1.SmallPython在详细考察了Python中最常用的几个对象之后,我们现在完全可以利用这些对象做出一个最简单的Python。这一章的目的就是模拟出一个最简单的Python——SmallPython。在SmallPython中,我们首先需要实现之前已经分析过的那些对象,比如PyIntObject,与CPython不同的是,我们并没有实现象CPython那样复杂的机制,作为一个模拟程序,我们只实现了简单的功能,也没有引入对象缓冲池的机制。这一切都是为了简洁而清晰地展示出Python运行时的脉络。在SmallPython中,实际上还需要实现Python的运行时环境,Python的运行时环境是我们在以后的章节中将要剖析的重点。在这里只是展示了其核心的思想——利用PyDictObject对象来维护变量名到变量值的映射。当然,在CPython中,还有许多其他的主题,比如Python源代码的编译,Python字节码的生成和执行等等,在SmallPython中,我们都不会涉及,因为到目前为止,我们没有任何资本做出如此逼真的模拟。不过当我们完成这本书的探索之后,就完全有能力实现一个真正的Python了。在SmallPython中,我们仅仅实现了PyIntObject,PyStringObject以及PyDictObject对象,仅仅实现了加法运算和输出操作。同时编译的过程也被简化到了极致,因此我们的SmallPython只能处理非常受限的表达式。虽然很简陋,但从中可以看到Python的骨架,同时,这也是我们深入Python解释器和运行时的起点。2.对象机制在SmallPython中,对象机制与CPython完全相同:[PyObject]#definePyObject_HEAD\intrefCount;\structtagPyTypeObject*type#definePyObject_HEAD_INIT(typePtr)\0,typePtr第1页共13页本文作者:RobertChen(第2页共13页typedefstructtagPyObject{PyObject_HEAD;}PyObject;但是对于类型对象,我们进行了大规模的删减。最终在类型对象中,只定义了加法操作,Hash操作以及输出操作:[PyTypeObject]//definitionofPyTypeObjecttypedefvoid(*PrintFun)(PyObject*object);typedefPyObject*(*AddFun)(PyObject*left,PyObject*right);typedeflong(*HashFun)(PyObject*object);typedefstructtagPyTypeObject{PyObject_HEAD;char*name;PrintFunprint;AddFunadd;HashFunhash;}PyTypeObject;PyIntObject的实现与CPython几乎是一样的,不过没有复杂的对象缓冲机制:[PyIntObject]typedefstructtagPyIntObject{PyObject_HEAD;intvalue;}PyIntObject;第2页共13页第3页共13页PyObject*PyInt_Create(intvalue){PyIntObject*object=newPyIntObject;object-refCount=1;object-type=&PyInt_Type;object-value=value;return(PyObject*)object;}staticvoidint_print(PyObject*object){PyIntObject*intObject=(PyIntObject*)object;printf(%d\n,intObject-value);}staticPyObject*int_add(PyObject*left,PyObject*right){PyIntObject*leftInt=(PyIntObject*)left;PyIntObject*rightInt=(PyIntObject*)right;PyIntObject*result=(PyIntObject*)PyInt_Create(0);if(result==NULL){printf(Wehavenoenoughmemory!!);}else{result-value=leftInt-value+rightInt-value;}return(PyObject*)result;}第3页共13页第4页共13页staticlongint_hash(PyObject*object){return(long)((PyIntObject*)object)-value;}PyTypeObjectPyInt_Type={PyObject_HEAD_INIT(&PyType_Type),int,int_print,int_add,int_hash};SmallPython中的PyStringObject与CPython中大不相同,在CPython中,它是一个变长对象,而SmallPython中只是一个简单的定长对象,因为SmallPython的定位就是个演示的程序:[PyStrObject]typedefstructtagPyStrObject{PyObject_HEAD;intlength;longhashValue;charvalue[50];}PyStringObject;PyObject*PyStr_Create(constchar*value){第4页共13页第5页共13页PyStringObject*object=newPyStringObject;object-refCount=1;object-type=&PyString_Type;object-length=(value==NULL)?0:strlen(value);object-hashValue=-1;memset(object-value,0,50);if(value!=NULL){strcpy(object-value,value);}return(PyObject*)object;}staticvoidstring_print(PyObject*object){PyStringObject*strObject=(PyStringObject*)object;printf(%s\n,strObject-value);}staticlongstring_hash(PyObject*object){PyStringObject*strObject=(PyStringObject*)object;registerintlen;registerunsignedchar*p;registerlongx;if(strObject-hashValue!=-1)returnstrObject-hashValue;len=strObject-length;p=(unsignedchar*)strObject-value;x=*p7;while(--len=0)x=(1000003*x)^*p++;第5页共13页第6页共13页x^=strObject-length;if(x==-1)x=-2;strObject-hashValue=x;returnx;}staticPyObject*string_add(PyObject*left,PyObject*right){PyStringObject*leftStr=(PyStringObject*)left;PyStringObject*rightStr=(PyStringObject*)right;PyStringObject*result=(PyStringObject*)PyStr_Create(NULL);if(result==NULL){printf(Wehavenoenoughmemory!!);}else{strcpy(result-value,leftStr-value);strcat(result-value,rightStr-value);}return(PyObject*)result;}PyTypeObjectPyString_Type={PyObject_HEAD_INIT(&PyType_Type),str,string_print,string_add,string_hash};第6页共13页第7页共13页在Python的解释器工作时,还有一个非常重要的对象,PyDictObject对象。PyDictObject对象在Python运行时会维护(变量名,变量值)的映射关系,Python所有的动作都是基于这种映射关系。在SmallPython中,我们基于C++中的map来实现PyDictObject对象。当然,map的运行效率比CPython中所采用的hash技术会慢上一些,但是对于我们的SmallPython,map就足够了:[PyDictObject]typedefstructtagPyDictObject{PyObject_HEAD;maplong,PyObject*dict;}PyDictObject;PyObject*PyDict_Create(){//createobjectPyDictObject*object=newPyDictObject;object-refCount=1;object-type=&PyDict_Type;return(PyObject*)object;}PyObject*PyDict_GetItem(PyObject*target,PyObject*key){longkeyHashValue=(key-type)-hash(key);maplong,PyObject*&dict=((PyDictObject*)target)-dict;maplong,PyObject*::iteratorit=dict.find(keyHashValue);maplong,PyObject*::iteratorend=dict.end();if(it==end){第7页共13页第8页共13页returnNULL;}returnit-second;}intPyDict_SetItem(PyObject*target,PyObject*key,PyObject*value){longkeyHashValue=(key-type)-hash(key);PyDictObject*dictObject=(PyDictObject*)target;(dictObject-dict)[keyHashValue]=value;return0;}//functionforPyDict_Typestaticvoiddict_print(PyObject*object){PyDictObject*dictObject=(PyDictObject*)object;printf({);maplong,PyObject*::iteratorit=(dictObject-dict).begin();maplong,PyObject*::iteratorend=(dictObject-dict).end();for(;it!=end;++it){//printkeyprintf(%d:,it-first);//printvaluePyObject*value=it-second;(value-type)-print(value);printf(,);}printf(
本文标题:Python源码剖析(Robert+Chen)
链接地址:https://www.777doc.com/doc-6245471 .html