Py***_Type这些都是结构体PyTypeObject的实例。
以PyLong_Type为例来说明:
(1)变量声明在Include/longobject.h中:
PyAPI_DATA(PyTypeObject) PyLong_Type;
注意:PyAPI_DATA是一个宏,定义在Include/pyport.h,用来隐藏动态库平台的差异,片段如下:
#define PyAPI_FUNC(RTYPE) __declspec(dllexport) RTYPE #define PyAPI_DATA(RTYPE) extern __declspec(dllexport) RTYPE
(2)变量定义在Objects/longobject.c中:
1 PyTypeObject PyLong_Type = { 2 PyVarObject_HEAD_INIT(&PyType_Type, 0) 3 "int", /* tp_name */ 4 offsetof(PyLongObject, ob_digit), /* tp_basicsize */ 5 sizeof(digit), /* tp_itemsize */ 6 long_dealloc, /* tp_dealloc */ 7 0, /* tp_print */ 8 0, /* tp_getattr */ 9 0, /* tp_setattr */ 10 0, /* tp_reserved */ 11 long_to_decimal_string, /* tp_repr */ 12 &long_as_number, /* tp_as_number */ 13 0, /* tp_as_sequence */ 14 0, /* tp_as_mapping */ 15 (hashfunc)long_hash, /* tp_hash */ 16 0, /* tp_call */ 17 long_to_decimal_string, /* tp_str */ 18 PyObject_GenericGetAttr, /* tp_getattro */ 19 0, /* tp_setattro */ 20 0, /* tp_as_buffer */ 21 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | 22 Py_TPFLAGS_LONG_SUBCLASS, /* tp_flags */ 23 long_doc, /* tp_doc */ 24 0, /* tp_traverse */ 25 0, /* tp_clear */ 26 long_richcompare, /* tp_richcompare */ 27 0, /* tp_weaklistoffset */ 28 0, /* tp_iter */ 29 0, /* tp_iternext */ 30 long_methods, /* tp_methods */ 31 0, /* tp_members */ 32 long_getset, /* tp_getset */ 33 0, /* tp_base */ 34 0, /* tp_dict */ 35 0, /* tp_descr_get */ 36 0, /* tp_descr_set */ 37 0, /* tp_dictoffset */ 38 0, /* tp_init */ 39 0, /* tp_alloc */ 40 long_new, /* tp_new */ 41 PyObject_Del, /* tp_free */ 42 };
PyTypeObject本身也是一个PyObject,它的类型是PyType_Type。
(1)声明:Include/object.h
PyAPI_DATA(PyTypeObject) PyType_Type; /* built-in 'type' */ PyAPI_DATA(PyTypeObject) PyBaseObject_Type; /* built-in 'object' */ PyAPI_DATA(PyTypeObject) PySuper_Type; /* built-in 'super' */
(2)定义:Objects/typeobject.c
1 PyTypeObject PyType_Type = { 2 PyVarObject_HEAD_INIT(&PyType_Type, 0) 3 "type", /* tp_name */ 4 sizeof(PyHeapTypeObject), /* tp_basicsize */ 5 sizeof(PyMemberDef), /* tp_itemsize */ 6 (destructor)type_dealloc, /* tp_dealloc */ 7 0, /* tp_print */ 8 0, /* tp_getattr */ 9 0, /* tp_setattr */ 10 0, /* tp_reserved */ 11 (reprfunc)type_repr, /* tp_repr */ 12 0, /* tp_as_number */ 13 0, /* tp_as_sequence */ 14 0, /* tp_as_mapping */ 15 0, /* tp_hash */ 16 (ternaryfunc)type_call, /* tp_call */ 17 0, /* tp_str */ 18 (getattrofunc)type_getattro, /* tp_getattro */ 19 (setattrofunc)type_setattro, /* tp_setattro */ 20 0, /* tp_as_buffer */ 21 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | 22 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TYPE_SUBCLASS, /* tp_flags */ 23 type_doc, /* tp_doc */ 24 (traverseproc)type_traverse, /* tp_traverse */ 25 (inquiry)type_clear, /* tp_clear */ 26 0, /* tp_richcompare */ 27 offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */ 28 0, /* tp_iter */ 29 0, /* tp_iternext */ 30 type_methods, /* tp_methods */ 31 type_members, /* tp_members */ 32 type_getsets, /* tp_getset */ 33 0, /* tp_base */ 34 0, /* tp_dict */ 35 0, /* tp_descr_get */ 36 0, /* tp_descr_set */ 37 offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */ 38 type_init, /* tp_init */ 39 0, /* tp_alloc */ 40 type_new, /* tp_new */ 41 PyObject_GC_Del, /* tp_free */ 42 (inquiry)type_is_gc, /* tp_is_gc */ 43 };