/* ARGSUSED */staticintint_print(PyIntObject *v, FILE *fp,int flags)/* flags -- not used but required by interface */{PyObject* str =PyString_FromString("I am in int_print");// 用于從C中的原生字符數組中創建出python中的字符串對象/* 第二個參數指明的是輸出目標. stdout指定了輸出目標為標準輸出. 命令行激活的python, 使用的是stdout, idle的輸出目標不是stdout, 就不會顯示信息*/PyObject_Print(str,stdout,0);printf("\n");fprintf(fp,"%ld", v->ob_ival);return0;}
#ifdef Py_TRACE_REFS/* Define pointers to support a doubly-linked list of all live heap objects. */#define _PyObject_HEAD_EXTRA \struct _object *_ob_next; \struct _object *_ob_prev;#define _PyObject_EXTRA_INIT 0, 0,#else#define _PyObject_HEAD_EXTRA#define _PyObject_EXTRA_INIT#endif/* PyObject_HEAD defines the initial segment of every PyObject. */#define PyObject_HEAD \_PyObject_HEAD_EXTRA \ // 雙向鏈表, 垃圾回收需要用到Py_ssize_t ob_refcnt; \ // 引用計數struct _typeobject *ob_type;// 指向類型對象的指針, 決定了對象的類型
[object.h]#define PyObject_VAR_HEAD \PyObject_HEADint ob_size;/* Number of items in variable part 元素的個數*/typedefstruct{PyObject_VAR_HEAD
} PyVarObject;
[object.h]typedefstruct _typeobject {PyObject_VAR_HEADchar*tp_name;/* For printing, in format "<module>.<name>" */int tp_basicsize, tp_itemsize;/* For allocation *//* Methods to implement standard operations */destructor tp_dealloc;printfunc tp_print;....../* More standard operations (here for binary compatibility) */hashfunc tp_hash;ternaryfunc tp_call;......} PyTypeObject;
[object.h]#ifdef Py_TRACE_REFS /* Define pointers to support a doubly-linked list of all live heap objects. */#define _PyObject_HEAD_EXTRA \struct _object *_ob_next; \struct _object *_ob_prev;#define _PyObject_EXTRA_INIT 0, 0,#else#define _PyObject_HEAD_EXTRA#define _PyObject_EXTRA_INIT#endif/* PyObject_HEAD defines the initial segment of every PyObject. */#define PyObject_HEAD \_PyObject_HEAD_EXTRA \Py_ssize_t ob_refcnt; \struct _typeobject *ob_type;#define PyObject_HEAD_INIT(type) \_PyObject_EXTRA_INIT \1, type,
PyObject和PyVarObject的定義
[object.h]/* PyObject_VAR_HEAD defines the initial segment of all variable-size* container objects. These end with a declaration of an array with 1* element, but enough space is malloc'ed so that the array actually* has room for ob_size elements. Note that ob_size is an element count,* not necessarily a byte count.*/#define PyObject_VAR_HEAD \PyObject_HEAD \Py_ssize_t ob_size; /* Number of items in variable part */#define Py_INVALID_SIZE (Py_ssize_t)-1/* Nothing is actually declared to be a PyObject, but every pointer to* a Python object can be cast to a PyObject*. This is inheritance built* by hand. Similarly every pointer to a variable-size Python object can,* in addition, be cast to PyVarObject*.*/typedefstruct _object {PyObject_HEAD
} PyObject;typedefstruct{ PyObject_VAR_HEAD
} PyVarObject;
[object.h]#define _Py_NewReference(op) ((op)->ob_refcnt = 1)#define _Py_dealloc(op) ((*(op)->ob_type->tp_dealloc)((PyObject *)(op)))#define Py_INCREF(op) ((op)->ob_refcnt++)#define Py_DECREF(op)if(--(op)->ob_refcnt !=0);else_Py_Dealloc((PyObject *)(op))/* Macros to use in case the object pointer may be NULL; */#define Py_XINCREF(op) if ((op) == NULL); else Py_INCREF(op)#define Py_XDECREF(op) if ((op) == NULL); else PY_DECREF(op)
#ifndef NSMALLPOSINTS // Number_Small_Positive_Integers#define NSMALLPOSINTS 257#endif#ifndef NSMALLNEGINTS // Number_Small_Negtive_Integers#define NSMALLNEGINTS 5#endif#if NSMALLNEGINTS + NSMALLPOSINTS > 0/* References to small integers are saved in this array so that theycan be shared.The integers that are saved are those in the range-NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
*/static PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS];// 小整數對象的對象池, 為PyIntObject對象指針數組。[]符號的優先級大于*#endif
[intobject.c]
static PyIntObject *
fill_free_list(void){PyIntObject *p,*q;/* Python's object allocator isn't appropriate for large blocks.*/// 申請大小為sizeof(PyIntBlock)的內存空間, 并鏈接到已有的block list中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 returnthe address of the last intobjectin the block.*/// 將PyIntBlock中的PyIntObject數組——objects-轉變成單向鏈表p =&((PyIntBlock *)p)->objects[0];q = p + N_INTOBJECTS;while(--q > p)q->ob_type =(struct _typeobject *)(q-1);q->ob_type = NULL;return p + N_INTOBJECTS -1;}