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 | METH_VARARGS,
     object_subclasshook_doc},
    {"__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);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章