Python源代码是如何运行的;自由列表=(PyIntObject*)Py类型(v)&引用;是否将无指针列表移动到下一个对象?
在python源代码中,int对象创建方法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
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;
}