资源简介

Python源码剖析——深度探索动态语言核心技术 一书中的两个demo代码,其一是Small Python,其二是对pyc文件的解析器,这两个均是windows下工程。另外附加了一个在linux下编译运行的smallPython。 上传原因:原来书中下载链接失效 http://bv.csdn.net/resource/pythonympx.rar

资源截图

代码片段和文件信息

#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define Pyobject_HEAD \
int refCount; \
struct tagPyTypeobject *type

#define Pyobject_HEAD_INIT(typePtr) \
0typePtr

typedef struct tagPyobject
{
Pyobject_HEAD;
}Pyobject;

typedef void(*PrintFun)(Pyobject*object);
typedef Pyobject* (*AddFun)(Pyobject* left Pyobject*right);
typedef long(*HashFun)(Pyobject* object);

typedef struct tagPyTypeobject
{
Pyobject_HEAD;
char* name;
PrintFun print;
AddFun add;
HashFun hash;
}PyTypeobject;

PyTypeobject PyType_Type =
{
Pyobject_HEAD_INIT(&PyType_Type)
“type“
0
0
0
};

typedef struct tagIntobject
{
Pyobject_HEAD;
int value;
}PyIntobject;

static  void int_print(Pyobject*object);
static Pyobject* int_add(Pyobject*left Pyobject* right);
static long int_hash(Pyobject* object);

PyTypeobject PyInt_Type =
{
Pyobject_HEAD_INIT(&PyType_Type)
“int“
int_print
int_add
int_hash
};


Pyobject* PyInt_Create(int value)
{
PyIntobject *object = new PyIntobject;
object->refCount = 1;
object->type = &PyInt_Type;
object->value = value;
return (Pyobject*)object;
}

static  void int_print(Pyobject*object)
{
PyIntobject* intobject = (PyIntobject*)object;
printf(“%d\n“ intobject->value);
}

static Pyobject* int_add(Pyobject*left Pyobject* right)
{
PyIntobject* leftInt = (PyIntobject*)left;
PyIntobject* rightInt = (PyIntobject*)right;
PyIntobject* result = (PyIntobject*)PyInt_Create(0);
if (result == NULL)
{
printf(“we have no enough memory“);
exit(1);
}
else
{
result->value = leftInt->value + rightInt->value;
}
return (Pyobject*)result;
}

static long int_hash(Pyobject* object)
{
return (long)((PyIntobject*)object)->value;
}


typedef struct tagPyStrobject
{
Pyobject_HEAD;
int length;
long hashValue;
char value[50];
}PyStringobject;
static void string_print(Pyobject*object);
static long string_hash(Pyobject*object);
static Pyobject* string_add(Pyobject*left Pyobject*right);
PyTypeobject PyString_Type =
{
Pyobject_HEAD_INIT(&PyType_Type)
“str“
string_print
string_add
string_hash
};
Pyobject* PyStr_Create(const char*value)
{
PyStringobject* object = new PyStringobject;
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;
}

static void string_print(Pyobject*object)
{
PyStringobject*strobject = (PyStringobject*)object;
printf(“%s\n“ strobject->value);
}

static long string_hash(Pyobject*object)
{
PyStringobject* strobject = (PyStringobject*)object;
register int len;
register unsigned char* p;
register long x;

if (strobject->hashValue != -1)
r

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件      17646  2008-05-19 09:56  Python源码剖析——深度探索动态语言核心技术_代码下载\pyc_parser.zip

     文件      11372  2008-05-19 09:56  Python源码剖析——深度探索动态语言核心技术_代码下载\samll_python.zip

     文件       7831  2018-03-03 13:03  Python源码剖析——深度探索动态语言核心技术_代码下载\smallPython(Linux下运行).cpp

     目录          0  2018-03-03 13:40  Python源码剖析——深度探索动态语言核心技术_代码下载

----------- ---------  ---------- -----  ----

                36849                    4


评论

共有 条评论

相关资源