Python源代码是如何运行的;自由列表=(PyIntObject*)Py类型(v)&引用;是否将无指针列表移动到下一个对象?

Python源代码是如何运行的;自由列表=(PyIntObject*)Py类型(v)&引用;是否将无指针列表移动到下一个对象?,python,c,Python,C,在python源代码中,int对象创建方法PyInt\u FromLong,python在free\u列表的第一个元素指向的位置创建一个新的PyIntObject。 代码如下: PyObject * PyInt_FromLong(long ival) { register PyIntObject *v; #if NSMALLNEGINTS + NSMALLPOSINTS > 0 if (-NSMALLNEGINTS <= ival && ival &l

在python源代码中,int对象创建方法
PyInt\u FromLong
,python在free\u列表的第一个元素指向的位置创建一个新的PyIntObject。 代码如下:

PyObject *
PyInt_FromLong(long ival)
{
    register PyIntObject *v;
#if NSMALLNEGINTS + NSMALLPOSINTS > 0
    if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) {
        v = small_ints[ival + NSMALLNEGINTS];
        Py_INCREF(v);
        return (PyObject *) v;
    }
#endif
    if (free_list == NULL) {
        if ((free_list = fill_free_list()) == NULL)
            return NULL;
    }
    /* Inline PyObject_New */
    v = free_list;
    free_list = (PyIntObject *)Py_TYPE(v);
    PyObject_INIT(v, &PyInt_Type);
    v->ob_ival = ival;
    return (PyObject *) v;
}
free_list=(PyIntObject*)Py_类型如何(v)工作

它移动自由列表以指向列表中的下一个对象


我认为
Py\u-TYPE(v)
将返回
PyInt\u-TYPE
,那么
(PyIntObject*)PyInt\u-TYPE
将不会是下一个对象

此行为在文件顶部的注释中描述:

free_list
是可用
PyIntObjects
的单链接列表,链接 通过滥用其
ob_类型
成员

您还可以查看
fill\u free\u list
函数,该函数根据标题注释分配
PyIntObject
s:

static PyIntObject *
fill_free_list(void)
{
    PyIntObject *p, *q;
    /* Python's object allocator isn't appropriate for large blocks. */
    p = (PyIntObject *) PyMem_MALLOC(sizeof(PyIntBlock));
    if (p == NULL)
        return (PyIntObject *) PyErr_NoMemory();
    ((PyIntBlock *)p)->next = block_list;
    block_list = (PyIntBlock *)p;
    /* Link the int objects together, from rear to front, then return
       the address of the last int object in the block. */
    p = &((PyIntBlock *)p)->objects[0];
    q = p + N_INTOBJECTS;
    while (--q > p)
        Py_TYPE(q) = (struct _typeobject *)(q-1);
    Py_TYPE(q) = NULL;
    return p + N_INTOBJECTS - 1;
}

主线是
Py_TYPE(q)=(struct_typeobject*)(q-1)

thx,哈哈,需要更仔细地阅读代码,再次感谢。
static PyIntObject *
fill_free_list(void)
{
    PyIntObject *p, *q;
    /* Python's object allocator isn't appropriate for large blocks. */
    p = (PyIntObject *) PyMem_MALLOC(sizeof(PyIntBlock));
    if (p == NULL)
        return (PyIntObject *) PyErr_NoMemory();
    ((PyIntBlock *)p)->next = block_list;
    block_list = (PyIntBlock *)p;
    /* Link the int objects together, from rear to front, then return
       the address of the last int object in the block. */
    p = &((PyIntBlock *)p)->objects[0];
    q = p + N_INTOBJECTS;
    while (--q > p)
        Py_TYPE(q) = (struct _typeobject *)(q-1);
    Py_TYPE(q) = NULL;
    return p + N_INTOBJECTS - 1;
}