PyObject
1
#define
PyObject_HEAD \
2 _PyObject_HEAD_EXTRA \
3 Py_ssize_t ob_refcnt; \
4 struct _typeobject * ob_type;
5
6 typedef struct _object {
7 PyObject_HEAD
8 } PyObject;
2 _PyObject_HEAD_EXTRA \
3 Py_ssize_t ob_refcnt; \
4 struct _typeobject * ob_type;
5
6 typedef struct _object {
7 PyObject_HEAD
8 } PyObject;
Python里对象机制的核心其实非常简单,一个是引用计数,一个是类型信息。
PyVarObject
1
#define
PyObject_VAR_HEAD \
2 PyObject_HEAD \
3 Py_ssize_t ob_size; /* Number of items in variable part */
4 #define Py_INVALID_SIZE (Py_ssize_t)-1
5
6 typedef struct {
7 PyObject_VAR_HEAD
8 } PyVarObject;
2 PyObject_HEAD \
3 Py_ssize_t ob_size; /* Number of items in variable part */
4 #define Py_INVALID_SIZE (Py_ssize_t)-1
5
6 typedef struct {
7 PyObject_VAR_HEAD
8 } PyVarObject;
变长对象多了ob_size表明元素的个数。
PyTypeObject
1
typedef
struct
_typeobject {
2 PyObject_VAR_HEAD
3 const char * tp_name; /* For printing, in format "<module>.<name>" */
4 Py_ssize_t tp_basicsize, tp_itemsize; /* For allocation */
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 cmpfunc 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 /* Added in release 2.2 */
52 /* Iterators */
53 getiterfunc tp_iter;
54 iternextfunc tp_iternext;
55
56 /* Attribute descriptor and subclassing stuff */
57 struct PyMethodDef * tp_methods;
58 struct PyMemberDef * tp_members;
59 struct PyGetSetDef * tp_getset;
60 struct _typeobject * tp_base;
61 PyObject * tp_dict;
62 descrgetfunc tp_descr_get;
63 descrsetfunc tp_descr_set;
64 Py_ssize_t tp_dictoffset;
65 initproc tp_init;
66 allocfunc tp_alloc;
67 newfunc tp_new;
68 freefunc tp_free; /* Low-level free-memory routine */
69 inquiry tp_is_gc; /* For PyObject_IS_GC */
70 PyObject * tp_bases;
71 PyObject * tp_mro; /* method resolution order */
72 PyObject * tp_cache;
73 PyObject * tp_subclasses;
74 PyObject * tp_weaklist;
75 destructor tp_del;
76
77 /* Type attribute cache version tag. Added in version 2.6 */
78 unsigned int tp_version_tag;
79
80 #ifdef COUNT_ALLOCS
81 /* these must be last and never explicitly initialized */
82 Py_ssize_t tp_allocs;
83 Py_ssize_t tp_frees;
84 Py_ssize_t tp_maxalloc;
85 struct _typeobject * tp_prev;
86 struct _typeobject * tp_next;
87 #endif
88 } PyTypeObject;
89
2 PyObject_VAR_HEAD
3 const char * tp_name; /* For printing, in format "<module>.<name>" */
4 Py_ssize_t tp_basicsize, tp_itemsize; /* For allocation */
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 cmpfunc 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 /* Added in release 2.2 */
52 /* Iterators */
53 getiterfunc tp_iter;
54 iternextfunc tp_iternext;
55
56 /* Attribute descriptor and subclassing stuff */
57 struct PyMethodDef * tp_methods;
58 struct PyMemberDef * tp_members;
59 struct PyGetSetDef * tp_getset;
60 struct _typeobject * tp_base;
61 PyObject * tp_dict;
62 descrgetfunc tp_descr_get;
63 descrsetfunc tp_descr_set;
64 Py_ssize_t tp_dictoffset;
65 initproc tp_init;
66 allocfunc tp_alloc;
67 newfunc tp_new;
68 freefunc tp_free; /* Low-level free-memory routine */
69 inquiry tp_is_gc; /* For PyObject_IS_GC */
70 PyObject * tp_bases;
71 PyObject * tp_mro; /* method resolution order */
72 PyObject * tp_cache;
73 PyObject * tp_subclasses;
74 PyObject * tp_weaklist;
75 destructor tp_del;
76
77 /* Type attribute cache version tag. Added in version 2.6 */
78 unsigned int tp_version_tag;
79
80 #ifdef COUNT_ALLOCS
81 /* these must be last and never explicitly initialized */
82 Py_ssize_t tp_allocs;
83 Py_ssize_t tp_frees;
84 Py_ssize_t tp_maxalloc;
85 struct _typeobject * tp_prev;
86 struct _typeobject * tp_next;
87 #endif
88 } PyTypeObject;
89