Include/object.h:摘录
1 /* Object and type object interface */ 2 3 /* 4 Objects are structures allocated on the heap. Special rules apply to 5 the use of objects to ensure they are properly garbage-collected. 6 Objects are never allocated statically or on the stack; they must be 7 accessed through special macros and functions only. (Type objects are 8 exceptions to the first rule; the standard types are represented by 9 statically initialized type objects, although work on type/class unification 10 for Python 2.2 made it possible to have heap-allocated type objects too). 11 12 An object has a 'reference count' that is increased or decreased when a 13 pointer to the object is copied or deleted; when the reference count 14 reaches zero there are no references to the object left and it can be 15 removed from the heap. 16 17 An object has a 'type' that determines what it represents and what kind 18 of data it contains. An object's type is fixed when it is created. 19 Types themselves are represented as objects; an object contains a 20 pointer to the corresponding type object. The type itself has a type 21 pointer pointing to the object representing the type 'type', which 22 contains a pointer to itself!). 23 24 Objects do not float around in memory; once allocated an object keeps 25 the same size and address. Objects that must hold variable-size data 26 can contain pointers to variable-size parts of the object. Not all 27 objects of the same type have the same size; but the size cannot change 28 after allocation. (These restrictions are made so a reference to an 29 object can be simply a pointer -- moving an object would require 30 updating all the pointers, and changing an object's size would require 31 moving it if there was another object right next to it.) 32 33 Objects are always accessed through pointers of the type 'PyObject *'. 34 The type 'PyObject' is a structure that only contains the reference count 35 and the type pointer. The actual memory allocated for an object 36 contains other data that can only be accessed after casting the pointer 37 to a pointer to a longer structure type. This longer type must start 38 with the reference count and type fields; the macro PyObject_HEAD should be 39 used for this (to accommodate for future changes). The implementation 40 of a particular object type can cast the object pointer to the proper 41 type and back. 42 43 A standard interface exists for objects that contain an array of items 44 whose size is determined when the object is allocated. 45 */
1.PyObject:Include/object.h
1 typedef struct _object { 2 Py_ssize_t ob_refcnt;/* 引用计数器 */ 3 struct _typeobject *ob_type;/* 对象类型 */ 4 } PyObject;/* 定义PyObject类型 */ 5 6 #define PyObject_HEAD PyObject ob_base;/* 声明PyObject类型变量 */
2.PyVarObject:Include/object.h
1 typedef struct { 2 PyObject ob_base; 3 Py_ssize_t ob_size; /* Number of items in variable part */ 4 } PyVarObject; 5 6 #define PyObject_VAR_HEAD PyVarObject ob_base; 7 /* 以下3个宏获取结构体成员的值 */ 8 #define Py_REFCNT(ob) (((PyObject*)(ob))->ob_refcnt) 9 #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type) 10 #define Py_SIZE(ob) (((PyVarObject*)(ob))->ob_size)
3.PyTypeObject(类型对象):Include/object.h
1 typedef struct _typeobject { 2 PyObject_VAR_HEAD 3 const char *tp_name; /* For printing, in format ". */ 4 Py_ssize_t tp_basicsize, tp_itemsize; /* For allocation (tp_basicsize+tp_itemsize*ob_size) */ 5 6 /* Methods to implement standard operations */ 7 8 destructor tp_dealloc;/* 函数指针变量 */ 9 printfunc tp_print; 10 getattrfunc tp_getattr; 11 setattrfunc tp_setattr; 12 void *tp_reserved; /* formerly known as tp_compare */ 13 reprfunc tp_repr; 14 15 /* Method suites for standard classes */ 16 17 PyNumberMethods *tp_as_number; 18 PySequenceMethods *tp_as_sequence; 19 PyMappingMethods *tp_as_mapping; 20 21 /* More standard operations (here for binary compatibility) */ 22 23 hashfunc tp_hash; 24 ternaryfunc tp_call; 25 reprfunc tp_str; 26 getattrofunc tp_getattro; 27 setattrofunc tp_setattro; 28 29 /* Functions to access object as input/output buffer */ 30 PyBufferProcs *tp_as_buffer; 31 32 /* Flags to define presence of optional/expanded features */ 33 long tp_flags; 34 35 const char *tp_doc; /* Documentation string */ 36 37 /* Assigned meaning in release 2.0 */ 38 /* call function for all accessible objects */ 39 traverseproc tp_traverse; 40 41 /* delete references to contained objects */ 42 inquiry tp_clear; 43 44 /* Assigned meaning in release 2.1 */ 45 /* rich comparisons */ 46 richcmpfunc tp_richcompare; 47 48 /* weak reference enabler */ 49 Py_ssize_t tp_weaklistoffset; 50 51 /* Iterators */ 52 getiterfunc tp_iter; 53 iternextfunc tp_iternext; 54 55 /* Attribute descriptor and subclassing stuff */ 56 struct PyMethodDef *tp_methods; 57 struct PyMemberDef *tp_members; 58 struct PyGetSetDef *tp_getset; 59 struct _typeobject *tp_base; 60 PyObject *tp_dict; 61 descrgetfunc tp_descr_get; 62 descrsetfunc tp_descr_set; 63 Py_ssize_t tp_dictoffset; 64 initproc tp_init; 65 allocfunc tp_alloc; 66 newfunc tp_new; 67 freefunc tp_free; /* Low-level free-memory routine */ 68 inquiry tp_is_gc; /* For PyObject_IS_GC */ 69 PyObject *tp_bases; 70 PyObject *tp_mro; /* method resolution order */ 71 PyObject *tp_cache; 72 PyObject *tp_subclasses; 73 PyObject *tp_weaklist; 74 destructor tp_del; 75 76 /* Type attribute cache version tag. Added in version 2.6 */ 77 unsigned int tp_version_tag; 78 79 #ifdef COUNT_ALLOCS 80 /* these must be last and never explicitly initialized */ 81 Py_ssize_t tp_allocs; 82 Py_ssize_t tp_frees; 83 Py_ssize_t tp_maxalloc; 84 struct _typeobject *tp_prev; 85 struct _typeobject *tp_next; 86 #endif 87 } PyTypeObject;" (类型的字符串名)
Python中,每一种类型也是一个对象。类型对象的类型是PyType_Type。下一篇里会讲到。
4.PyObject_HEAD_INIT和PyVarObject_HEAD_INIT
Include/object.h:
1 #define PyObject_HEAD_INIT(type) \ 2 { _PyObject_EXTRA_INIT \ 3 1, type }, 4 5 #define PyVarObject_HEAD_INIT(type, size) \ 6 { PyObject_HEAD_INIT(type) size },
PyObject_HEAD_INIT:用来初始化PyVarObject的2个成员(引用计数始终初始化为1)
PyVarObject_HEAD_INIT:用来初始化PyVarObject的3个成员(引用计数始终初始化为1)
5.PyNumberMethods、PySequenceMethods和PyMappingMethods:Include/object.h
1 typedef struct { 2 /* Number implementations must check *both* 3 arguments for proper type and implement the necessary conversions 4 in the slot functions themselves. */ 5 6 binaryfunc nb_add;/* 函数指针变量,该指针指向的函数定义了对象进行加法操作的具体行为 */ 7 binaryfunc nb_subtract; 8 binaryfunc nb_multiply; 9 binaryfunc nb_remainder; 10 binaryfunc nb_divmod; 11 ternaryfunc nb_power; 12 unaryfunc nb_negative; 13 unaryfunc nb_positive; 14 unaryfunc nb_absolute; 15 inquiry nb_bool; 16 unaryfunc nb_invert; 17 binaryfunc nb_lshift; 18 binaryfunc nb_rshift; 19 binaryfunc nb_and; 20 binaryfunc nb_xor; 21 binaryfunc nb_or; 22 unaryfunc nb_int; 23 void *nb_reserved; /* the slot formerly known as nb_long */ 24 unaryfunc nb_float; 25 26 binaryfunc nb_inplace_add; 27 binaryfunc nb_inplace_subtract; 28 binaryfunc nb_inplace_multiply; 29 binaryfunc nb_inplace_remainder; 30 ternaryfunc nb_inplace_power; 31 binaryfunc nb_inplace_lshift; 32 binaryfunc nb_inplace_rshift; 33 binaryfunc nb_inplace_and; 34 binaryfunc nb_inplace_xor; 35 binaryfunc nb_inplace_or; 36 37 binaryfunc nb_floor_divide; 38 binaryfunc nb_true_divide; 39 binaryfunc nb_inplace_floor_divide; 40 binaryfunc nb_inplace_true_divide; 41 42 unaryfunc nb_index; 43 } PyNumberMethods;
PyNumberMethods中,定义了作为数值对象应该支持的操作行为。
1 typedef struct { 2 lenfunc sq_length; 3 binaryfunc sq_concat; 4 ssizeargfunc sq_repeat; 5 ssizeargfunc sq_item; 6 void *was_sq_slice; 7 ssizeobjargproc sq_ass_item; 8 void *was_sq_ass_slice; 9 objobjproc sq_contains; 10 11 binaryfunc sq_inplace_concat; 12 ssizeargfunc sq_inplace_repeat; 13 } PySequenceMethods;
PySequenceMethods中,定义了作为序列对象应该支持的操作行为。
1 typedef struct { 2 lenfunc mp_length; 3 binaryfunc mp_subscript; 4 objobjargproc mp_ass_subscript; 5 } PyMappingMethods;
PyMappingMethods中,定义了作为关联对象应该支持的操作行为。
注意:Python中PythonTypeObject允许一种类型同时指定三种不同对象的行为特征。
1 typedef struct { 2 getbufferproc bf_getbuffer; 3 releasebufferproc bf_releasebuffer; 4 } PyBufferProcs;
6.PyMethodDef:Include/methodobject.h
1 struct PyMethodDef { 2 const char *ml_name; /* The name of the built-in function/method */ 3 PyCFunction ml_meth; /* The C function that implements it */ 4 int ml_flags; /* Combination of METH_xxx flags, which mostly 5 describe the args expected by the C func */ 6 const char *ml_doc; /* The __doc__ attribute, or NULL */ 7 }; 8 typedef struct PyMethodDef PyMethodDef;
7.PyMemberDef:Include/structmember.h
1 typedef struct PyMemberDef { 2 char *name; 3 int type; 4 Py_ssize_t offset; 5 int flags; 6 char *doc; 7 } PyMemberDef;
8.PyGetSetDef:Include/descrobject.h
1 typedef struct PyGetSetDef { 2 char *name; 3 getter get; 4 setter set; 5 char *doc; 6 void *closure; 7 } PyGetSetDef;