类型对象
********

PyTypeObject

   对象的 C 结构用于描述 built-in 类型。

PyObject* PyType_Type

   这是属于 type 对象的 type object，它在 Python 层面和 "type" 是相同
   的对象。

int PyType_Check(PyObject *o)

   如果对象 *o* 是一个类型对象，包括继承于标准类型对象的类型实例，返回
   真。在其它所有情况下返回假。

int PyType_CheckExact(PyObject *o)

   如果对象 *o* 是一个类型对象，但不是标准类型对象的子类型时，返回真。
   在其它所有情况下返回假。

unsigned int PyType_ClearCache()

   清空内部查找缓存。 返回当前版本标签。

unsigned long PyType_GetFlags(PyTypeObject* type)

   返回 *type* 的 "tp_flags" 成员。 此函数主要是配合 *Py_LIMITED_API*
   使用；单独的旗标位会确保在各个 Python 发行版之间保持稳定，但对
   "tp_flags" 本身的访问并不是受限 API 的一部分。

   3.2 新版功能.

   在 3.4 版更改: 返回类型现在是 "unsigned long" 而不是 "long"。

void PyType_Modified(PyTypeObject *type)

   使该类型及其所有子类型的内部查找缓存失效。 此函数必须在对该类型的属
   性或基类进行任何手动修改之后调用。

int PyType_HasFeature(PyTypeObject *o, int feature)

   Return true if the type object *o* sets the feature *feature*.
   Type features are denoted by single bit flags.

int PyType_IS_GC(PyTypeObject *o)

   如果类型对象包括对循环检测器的支持则返回真值；这会测试类型旗标
   "Py_TPFLAGS_HAVE_GC"。

int PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b)

   如果 *a* 是 *b* 的子类型则返回真值。

   此函数只检查实际的子类型，这意味着 "__subclasscheck__()" 不会在 *b*
   上被调用。 请调用 "PyObject_IsSubclass()" 来执行与 "issubclass()"
   所做的相同检查。

PyObject* PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
    *Return value: New reference.*

   类型对象的 "tp_alloc" 槽位的通用处理句柄。 请使用 Python 的默认内存
   分配机制来分配一个新的实例并将其所有内容初始化为 "NULL"。

PyObject* PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
    *Return value: New reference.*

   类型对象的 "tp_new" 槽位的通用处理句柄。 请使用类型的 "tp_alloc" 槽
   位来创建一个新的实例。

int PyType_Ready(PyTypeObject *type)

   最终化一个类型对象。 这应当在所有类型对象上调用以完成它们的初始化。
   此函数会负责从一个类型的基类添加被继承的槽位。 成功时返回 "0"，或是
   在出错时返回 "-1" 并设置一个异常。

void* PyType_GetSlot(PyTypeObject *type, int slot)

   返回存储在给定槽位中的函数指针。 如果结果为 "NULL"，则表示或者该槽
   位为 "NULL"，或者该函数调用传入了无效的形参。 调用方通常要将结果指
   针转换到适当的函数类型。

   请参阅 "PyType_Slot.slot" 查看可用的 *slot* 参数值。

   An exception is raised if *type* is not a heap type.

   3.4 新版功能.


创建堆分配类型
==============

下列函数和结构体可被用来创建 堆类型。

PyObject* PyType_FromSpecWithBases(PyType_Spec *spec, PyObject *bases)
    *Return value: New reference.*

   Creates and returns a heap type object from the *spec*
   ("Py_TPFLAGS_HEAPTYPE").

   If *bases* is a tuple, the created heap type contains all types
   contained in it as base types.

   If *bases* is "NULL", the *Py_tp_bases* slot is used instead. If
   that also is "NULL", the *Py_tp_base* slot is used instead. If that
   also is "NULL", the new type derives from "object".

   此函数会在新类型上调用 "PyType_Ready()"。

   3.3 新版功能.

PyObject* PyType_FromSpec(PyType_Spec *spec)
    *Return value: New reference.*

   等价于 "PyType_FromSpecWithBases(spec, NULL)"。

PyType_Spec

   定义一个类型的行为的结构体。

   const char* PyType_Spec.name

      类型的名称，用来设置 "PyTypeObject.tp_name"。

   int PyType_Spec.basicsize

   int PyType_Spec.itemsize

      以字节数表示的实例大小，用来设置 "PyTypeObject.tp_basicsize" 和
      "PyTypeObject.tp_itemsize"。

   int PyType_Spec.flags

      类型旗标，用来设置 "PyTypeObject.tp_flags"。

      如果未设置 "Py_TPFLAGS_HEAPTYPE" 旗标，则
      "PyType_FromSpecWithBases()" 会自动设置它。

   PyType_Slot *PyType_Spec.slots

      "PyType_Slot" 结构体的数组。 以特殊槽位值 "{0, NULL}" 来结束。

PyType_Slot

   定义一个类型的可选功能的结构体，包含一个槽位 ID 和一个值指针。

   int PyType_Slot.slot

      槽位 ID。

      槽位 ID 的类名像是结构体 "PyTypeObject", "PyNumberMethods",
      "PySequenceMethods", "PyMappingMethods" 和 "PyAsyncMethods" 的字
      段名附加一个 "Py_" 前缀。 举例来说，使用:

      * "Py_tp_dealloc" 设置 "PyTypeObject.tp_dealloc"

      * "Py_nb_add" 设置 "PyNumberMethods.nb_add"

      * "Py_sq_length" 设置 "PySequenceMethods.sq_length"

      The following fields cannot be set using "PyType_Spec" and
      "PyType_Slot":

      * "tp_dict"

      * "tp_mro"

      * "tp_cache"

      * "tp_subclasses"

      * "tp_weaklist"

      * "tp_print"

      * "tp_weaklistoffset"

      * "tp_dictoffset"

      * "bf_getbuffer"

      * "bf_releasebuffer"

      设置 "Py_tp_bases" 或 "Py_tp_base" 在某些平台上可能会有问题。 为
      了避免问题，请改用 "PyType_FromSpecWithBases()" 的 *bases* 参数
      。

   void *PyType_Slot.pfunc

      该槽位的预期值。 在大多数情况下，这将是一个指向函数的指针。

      May not be "NULL".
