现在的位置: 主页 > 企业简介 > 文章列表

Python源码入门学习心得-初始化(一)

作者:合肥瑶海区海美电器服务部 来源:www.an128l.com 未知发布时间:2017-09-05 12:26:39
Python源码入门学习心得-初始化(一) 有点杂...林乱记了几个点,以后整理,主要是一些基本的数据结构

Build好python的源码以后,直接debug运行,python就对进入 main.c

int Py_Main(int argc, wchar_t **argv)

完成一系列的初始化操作.

================================================

PyObject是Python源码里面最基本的struct

in pystate.c

static PyInterpreterState *interp_head = NULL;

PyInterpreterState * PyInterpreterState_New(void)

...

这个PyInterpreterState就像python的状态管理器之类

每调用一次函数,就生成一个新节点,并且用interp_head指向之, 挂在链表最前端

in pythonrun.c

PyThreadState *tstate;

tstate = PyThreadState_New(interp);

tstate->next = interp->tstate_head;

interp->tstate_head = tstate;

借用了interp->tstate_head 作为PyThreadState链表的头指针

初始化以后,各有一个指针指向对方如下图

默认的函数定义列表

static PyMethodDef object_methods[] = {

"__reduce_ex__", object_reduce_ex, METH_VARARGS,

PyDoc_STR("helper for pickle"),

"__reduce__", object_reduce, METH_VARARGS,

PyDoc_STR("helper for pickle"),

"__subclasshook__", object_subclasshook, METH_CLASS,

"__format__", object_format, METH_VARARGS,

PyDoc_STR("default object formatter"),

"__sizeof__", object_sizeof, METH_NOARGS,

PyDoc_STR("__sizeof__() -> size of object in memory, in bytes"),

0

};

PyTypeObject PyBaseObject_Type =

...

object_methods, /* tp_methods */

...

;

PyBaseObject_Type里面有系统预定义好的一些方法

bltinmodule.c

包含了一些内置函数的实现,文档定义,例如

static PyObject *

builtin_dir(PyObject *self, PyObject *args)

PyObject *arg = NULL;

if (!PyArg_UnpackTuple(args, "dir", 0, 1, &arg))

return NULL;

return PyObject_Dir(arg);

PyDoc_STRVAR(dir_doc,

"dir([object]) -> list of strings\n"

"\n"

"If called without an argument, return the names in the current scope.\n"

"Else, return an alphabetized list of names comprising (some of) the attributes\n"

....

" recursively the attributes of its class's base classes.");

static struct PyModuleDef builtinsmodule =

PyModuleDef_HEAD_INIT,

"builtins",

builtin_doc,

-1, /* multiple "initialization" just copies the module dict. */

builtin_methods,

NULL,

NULL,

NULL,

NULL

;

static PyMethodDef builtin_methods[] = {

...

"dir", builtin_dir, METH_VARARGS, dir_doc,

"divmod", builtin_divmod, METH_VARARGS, divmod_doc,

...

NULL, NULL,

};

初始化的时候调用 mod = PyModule_Create(&builtinsmodule);

==============================================

In object.h

/* PyObject_HEAD defines the initial segment of every PyObject. */

#define PyObject_HEAD PyObject ob_base;

于是PyModuleObject的类图

In moduleobject.c

有PyTypeObject对象 PyModule_Type的定义

PyTypeObject PyModule_Type =

PyVarObject_HEAD_INIT(&PyType_Type, 0)

"module", /* tp_name */

sizeof(PyModuleObject), /* tp_size */

0, /* tp_itemsize */

(destructor)module_dealloc, /* tp_dealloc */

...

PyObject_GenericGetAttr, /* tp_getattro */

PyObject_GenericSetAttr, /* tp_setattro */

...

(traverseproc)module_traverse, /* tp_traverse */

(inquiry)module_clear, /* tp_clear */

...

module_members, /* tp_members */

... /* tp_descr_set */

offsetof(PyModuleObject, md_dict), /* tp_dictoffset */

(initproc)module_init, /* tp_init */

PyType_GenericAlloc, /* tp_alloc */

PyType_GenericNew, /* tp_new */

PyObject_GC_Del, /* tp_free */

;

其中module_members的定义

static PyMemberDef module_members[] = {

"__dict__", T_OBJECT, offsetof(PyModuleObject, md_dict), READONLY,

0

};

object.c

void _Py_AddToAllObjects(PyObject *op, int force)

Insert op at the front of the list of all objects

Ref->OP2->OP1->OP0....

pythonrun.c

void

Py_InitializeEx(int install_sigs)

bimod = _PyBuiltin_Init();

interp->builtins = PyModule_GetDict(bimod);

sysmod = _PySys_Init();

interp->sysdict = PyModule_GetDict(sysmod

,专题系统,专题

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:武汉网页制作 http://www.feimao666.com

上一篇:营销型网站良好用户体验度提高转化率 下一篇:最后一页