型オブジェクト
**************

新スタイルの型を定義する構造体: "PyTypeObject" 構造体は、おそらく
Python オブジェクトシステムの中で最も重要な構造体の1つでしょう。型オブ
ジェクトは "PyObject_*()" 系や "PyType_*()" 系の関数で扱えますが、ほと
んどの Python アプリケーションにとって、さして面白みのある機能を提供し
ません。型オブジェクトはオブジェクトがどのように振舞うかを決める基盤で
すから、インタプリタ自体や新たな型を定義する拡張モジュールでは非常に重
要な存在です。

型オブジェクトは標準の型 (standard type) に比べるとかなり大きな構造体
です。各型オブジェクトは多くの値を保持しており、そのほとんどは C 関数
へのポインタで、それぞれの関数はその型の機能の小さい部分を実装していま
す。この節では、型オブジェクトの各フィールドについて詳細を説明します。
各フィールドは、構造体内で出現する順番に説明されています。

Typedefs: unaryfunc, binaryfunc, ternaryfunc, inquiry, coercion,
intargfunc, intintargfunc, intobjargproc, intintobjargproc,
objobjargproc, destructor, freefunc, printfunc, getattrfunc,
getattrofunc, setattrfunc, setattrofunc, cmpfunc, reprfunc, hashfunc

"PyTypeObject" の構造体定義は "Include/object.h" で見つけられるはずで
す。参照の手間を省くために、ここでは定義を繰り返します:

   typedef struct _typeobject {
       PyObject_VAR_HEAD
       char *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;
       getattrfunc tp_getattr;
       setattrfunc tp_setattr;
       cmpfunc tp_compare;
       reprfunc tp_repr;

       /* Method suites for standard classes */

       PyNumberMethods *tp_as_number;
       PySequenceMethods *tp_as_sequence;
       PyMappingMethods *tp_as_mapping;

       /* More standard operations (here for binary compatibility) */

       hashfunc tp_hash;
       ternaryfunc tp_call;
       reprfunc tp_str;
       getattrofunc tp_getattro;
       setattrofunc tp_setattro;

       /* Functions to access object as input/output buffer */
       PyBufferProcs *tp_as_buffer;

       /* Flags to define presence of optional/expanded features */
       long tp_flags;

       char *tp_doc; /* Documentation string */

       /* Assigned meaning in release 2.0 */
       /* call function for all accessible objects */
       traverseproc tp_traverse;

       /* delete references to contained objects */
       inquiry tp_clear;

       /* Assigned meaning in release 2.1 */
       /* rich comparisons */
       richcmpfunc tp_richcompare;

       /* weak reference enabler */
       long tp_weaklistoffset;

       /* Added in release 2.2 */
       /* Iterators */
       getiterfunc tp_iter;
       iternextfunc tp_iternext;

       /* Attribute descriptor and subclassing stuff */
       struct PyMethodDef *tp_methods;
       struct PyMemberDef *tp_members;
       struct PyGetSetDef *tp_getset;
       struct _typeobject *tp_base;
       PyObject *tp_dict;
       descrgetfunc tp_descr_get;
       descrsetfunc tp_descr_set;
       long tp_dictoffset;
       initproc tp_init;
       allocfunc tp_alloc;
       newfunc tp_new;
       freefunc tp_free; /* Low-level free-memory routine */
       inquiry tp_is_gc; /* For PyObject_IS_GC */
       PyObject *tp_bases;
       PyObject *tp_mro; /* method resolution order */
       PyObject *tp_cache;
       PyObject *tp_subclasses;
       PyObject *tp_weaklist;

   } PyTypeObject;

型オブジェクト構造体は "PyVarObject" 構造体を拡張したものです。
"ob_size" フィールドは、(通常 class 文が呼び出す "type_new()" で生成さ
れる) 動的な型に使います。 "PyType_Type" (メタタイプ) は "tp_itemsize"
を初期化するので注意してください。すなわち、 インスタンス (つまり型オ
ブジェクト) には "ob_size" フィールドが存在しなければ *なりません* 。

PyObject* PyObject._ob_next
PyObject* PyObject._ob_prev

   これらのフィールドはマクロ "Py_TRACE_REFS" が定義されている場合のみ
   存在します。 "PyObject_HEAD_INIT" マクロを使うと、フィールドを
   *NULL* に初期化します。静的にメモリ確保されているオブジェクトでは、
   これらのフィールドは常に *NULL* のままです。動的にメモリ確保される
   オブジェクトの場合、これら二つのフィールドは、ヒープ上の *全ての*
   存続中のオブジェクトからなる二重リンクリストでオブジェクトをリンク
   する際に使われます。このことは様々なデバッグ目的に利用できます; 現
   状では、環境変数 "PYTHONDUMPREFS" が設定されているときに、プログラ
   ムの実行終了時点で存続しているオブジェクトを出力するのが唯一の用例
   です。

   サブタイプはこのフィールドを継承しません。

Py_ssize_t PyObject.ob_refcnt

   型オブジェクトの参照カウントで、 "PyObject_HEAD_INIT" はこの値を
   "1" に初期化します。静的にメモリ確保された型オブジェクトでは、型の
   インスタンス ("ob_type" が該当する型を指しているオブジェクト) は参
   照をカウントする対象には *なりません* 。動的にメモリ確保される型オ
   ブジェクトの場合、インスタンスは参照カウントの対象に *なります* 。

   サブタイプはこのフィールドを継承しません。

   バージョン 2.5 で変更: This field used to be an "int" type. This
   might require changes in your code for properly supporting 64-bit
   systems.

PyTypeObject* PyObject.ob_type

   型自体の型、別の言い方をするとメタタイプです。 "PyObject_HEAD_INIT"
   マクロで初期化され、通常は "&PyType_Type" になります。しかし、(少な
   くとも) Windows で利用できる動的ロード可能な拡張モジュールでは、コ
   ンパイラは有効な初期化ではないと文句をつけます。そこで、ならわしと
   して、 "PyObject_HEAD_INIT" には *NULL* を渡して初期化しておき、他
   の操作を行う前にモジュールの初期化関数で明示的にこのフィールドを初
   期化することになっています。この操作は以下のように行います:

      Foo_Type.ob_type = &PyType_Type;

   This should be done before any instances of the type are created.
   "PyType_Ready()" checks if "ob_type" is *NULL*, and if so,
   initializes it: in Python 2.2, it is set to "&PyType_Type"; in
   Python 2.2.1 and later it is initialized to the "ob_type" field of
   the base class. "PyType_Ready()" will not change this field if it
   is non-zero.

   In Python 2.2, this field is not inherited by subtypes.  In 2.2.1,
   and in 2.3 and beyond, it is inherited by subtypes.

Py_ssize_t PyVarObject.ob_size

   静的にメモリ確保されている型オブジェクトの場合、このフィールドはゼ
   ロに初期化されます。動的にメモリ確保されている型オブジェクトの場合
   、このフィールドは内部使用される特殊な意味を持ちます。

   サブタイプはこのフィールドを継承しません。

char* PyTypeObject.tp_name

   型の名前が入っている NUL 終端された文字列へのポインタです。モジュー
   ルのグローバル変数としてアクセスできる型の場合、この文字列は完全な
   モジュール名、ドット、そして型の名前と続く文字列になります; 組み込
   み型の場合、ただの型の名前です。モジュールがあるパッケージのサブモ
   ジュールの場合、完全なパッケージ名が完全なモジュール名の一部になっ
   ています。例えば、パッケージ "P" 内のサブモジュール "Q" に入ってい
   るモジュール "M" 内で定義されている "T" は、 "tp_name" を
   ""P.Q.M.T"" に初期化します。

   動的にメモリ確保される型オブジェクトの場合、このフィールドは単に型
   の名前になり、モジュール名は型の辞書内でキー "'__module__'" に対す
   る値として明示的に保存されます。

   静的にメモリ確保される型オブジェクトの場合、 tp_name フィールドには
   ドットが含まれているはずです。 最後のドットよりも前にある部分文字列
   全体は "__module__" 属性として、またドットよりも後ろにある部分は
   "__name__" 属性としてアクセスできます。

   ドットが入っていない場合、 "tp_name" フィールドの内容全てが
   "__name__" 属性になり、 "__module__" 属性は (前述のように型の辞書内
   で明示的にセットしないかぎり) 未定義になります。 このため、その型は
   pickle 化できないことになります。 さらに、 pydoc が作成するモジュー
   ルドキュメントのリストにも載らなくなります。

   サブタイプはこのフィールドを継承しません。

Py_ssize_t PyTypeObject.tp_basicsize
Py_ssize_t PyTypeObject.tp_itemsize

   これらのフィールドは、型インスタンスのバイトサイズを計算できるよう
   にします。

   型には二つの種類があります: 固定長インスタンスの型は、
   "tp_itemsize" フィールドがゼロで、可変長インスタンスの方は
   "tp_itemsize" フィールドが非ゼロの値になります。固定長インスタンス
   の型の場合、全てのインスタンスは等しく "tp_basicsize" で与えられた
   サイズになります。

   For a type with variable-length instances, the instances must have
   an "ob_size" field, and the instance size is "tp_basicsize" plus N
   times "tp_itemsize", where N is the 「length」 of the object.  The
   value of N is typically stored in the instance’s "ob_size" field.
   There are exceptions:  for example, long ints use a negative
   "ob_size" to indicate a negative number, and N is "abs(ob_size)"
   there.  Also, the presence of an "ob_size" field in the instance
   layout doesn’t mean that the instance structure is variable-length
   (for example, the structure for the list type has fixed-length
   instances, yet those instances have a meaningful "ob_size" field).

   The basic size includes the fields in the instance declared by the
   macro "PyObject_HEAD" or "PyObject_VAR_HEAD" (whichever is used to
   declare the instance struct) and this in turn includes the
   "_ob_prev" and "_ob_next" fields if they are present.  This means
   that the only correct way to get an initializer for the
   "tp_basicsize" is to use the "sizeof" operator on the struct used
   to declare the instance layout. The basic size does not include the
   GC header size (this is new in Python 2.2; in 2.1 and 2.0, the GC
   header size was included in "tp_basicsize").

   これらのフィールドはサブタイプに別々に継承されます。基底タイプが 0
   でない "tp_itemsize" を持っていた場合、基底タイプの実装に依存します
   が、一般的にはサブタイプで別の 0 で無い値を "tp_itemsize" に設定す
   るのは安全ではありません。

   アラインメントに関する注釈: 変数の各要素を配置する際に特定のアライ
   ンメントが必要となる場合、 "tp_basicsize" の値に気をつけなければな
   りません。例: ある型が "double" の配列を実装しているとします。
   "tp_itemsize" は "sizeof(double)" です。 "tp_basicsize" が
   "sizeof(double)" (ここではこれを "double" のアラインメントが要求す
   るサイズと仮定する) の個数分のサイズになるようにするのはプログラマ
   の責任です。

destructor PyTypeObject.tp_dealloc

   インスタンスのデストラクタ関数へのポインタです。この関数は (単量子
   "None" や "Ellipsis" の場合のように) インスタンスが決してメモリ解放
   されない型でない限り必ず定義しなければなりません。

   デストラクタ関数は、参照カウントが新たにゼロになった際に
   "Py_DECREF()" や "Py_XDECREF()" マクロから呼び出されます。呼び出さ
   れた時点では、インスタンスはまだ存在しますが、インスタンスに対する
   参照は全くない状態です。デストラクタ関数はインスタンスが保持してい
   る全ての参照を解放し、インスタンスが確保している全てのメモリバッフ
   ァを (バッファの確保時に使った関数に対応するメモリ解放関数を使って)
   解放し、最後に (デストラクタ関数の最後の操作として) その型の
   "tp_free" 関数を呼び出します。ある型がサブタイプを作成できない
   ("Py_TPFLAGS_BASETYPE" フラグがセットされていない) 場合、 "tp_free"
   の代わりにオブジェクトのメモリ解放関数 (deallocator) を直接呼び出し
   てもかまいません。オブジェクトのメモリ解放関数は、インスタンスのメ
   モリ確保を行う際に使った関数に対応したものでなければなりません; イ
   ンスタンスを "PyObject_New()" や "PyObject_VarNew()" でメモリ 確保
   した場合には、通常 "PyObject_Del()" を使い、 "PyObject_GC_New()" や
   "PyObject_GC_NewVar()" で確保した場合には "PyObject_GC_Del()" を使
   います。

   サブタイプはこのフィールドを継承します。

printfunc PyTypeObject.tp_print

   An optional pointer to the instance print function.

   The print function is only called when the instance is printed to a
   *real* file; when it is printed to a pseudo-file (like a "StringIO"
   instance), the instance’s "tp_repr" or "tp_str" function is called
   to convert it to a string.  These are also called when the type’s
   "tp_print" field is *NULL*.  A type should never implement
   "tp_print" in a way that produces different output than "tp_repr"
   or "tp_str" would.

   The print function is called with the same signature as
   "PyObject_Print()": "int tp_print(PyObject *self, FILE *file, int
   flags)".  The *self* argument is the instance to be printed.  The
   *file* argument is the stdio file to which it is to be printed.
   The *flags* argument is composed of flag bits. The only flag bit
   currently defined is "Py_PRINT_RAW". When the "Py_PRINT_RAW" flag
   bit is set, the instance should be printed the same way as "tp_str"
   would format it; when the "Py_PRINT_RAW" flag bit is clear, the
   instance should be printed the same was as "tp_repr" would format
   it. It should return "-1" and set an exception condition when an
   error occurred during the comparison.

   It is possible that the "tp_print" field will be deprecated. In any
   case, it is recommended not to define "tp_print", but instead to
   rely on "tp_repr" and "tp_str" for printing.

   サブタイプはこのフィールドを継承します。

getattrfunc PyTypeObject.tp_getattr

   オプションのポインタで、get-attribute-string を行う関数を指します。

   このフィールドは非推奨です。 このフィールドを定義するときは、
   "tp_getattro" 関数と同じように動作し、属性名は Python 文字列 オブジ
   ェクトではなく C 文字列で指定するような関数を指すようにしなければな
   りません。 シグネチャは次の通りです:

      PyObject * tp_getattr(PyObject *o, char *attr_name);

   このフィールドは "tp_getattro" と共にサブタイプに継承されます: すな
   わち、サブタイプの "tp_getattr" および "tp_getattro" が共に *NULL*
   の場合、サブタイプは基底タイプから "tp_getattr" と "tp_getattro" を
   両方とも継承します。

setattrfunc PyTypeObject.tp_setattr

   オプションのポインタで、属性の設定と削除を行う関数を指します。

   このフィールドは非推奨です。 このフィールドを定義するときは、
   "tp_setattro" 関数と同じように動作し、属性名は Python 文字列 オブジ
   ェクトではなく C 文字列で指定するような関数を指すようにしなければな
   りません。 シグネチャは次の通りです:

      PyObject * tp_setattr(PyObject *o, char *attr_name, PyObject *v);

   引数 *v* に *NULL* を設定すると属性を削除します。 このフィールドは
   "tp_setattro" と共にサブタイプに継承されます: すなわち、サブタイプ
   の "tp_setattr" および "tp_setattro" が両方とも *NULL* のとき、サブ
   タイプは基底タイプから "tp_setattr" と "tp_setattro" を両方とも継承
   します。

cmpfunc PyTypeObject.tp_compare

   An optional pointer to the three-way comparison function.

   The signature is the same as for "PyObject_Compare()". The function
   should return "1" if *self* greater than *other*, "0" if *self* is
   equal to *other*, and "-1" if *self* less than *other*.  It should
   return "-1" and set an exception condition when an error occurred
   during the comparison.

   This field is inherited by subtypes together with "tp_richcompare"
   and "tp_hash": a subtypes inherits all three of "tp_compare",
   "tp_richcompare", and "tp_hash" when the subtype’s "tp_compare",
   "tp_richcompare", and "tp_hash" are all *NULL*.

reprfunc PyTypeObject.tp_repr

   オプションのポインタで、組み込み関数 "repr()" を実装している関数を
   指します。

   シグネチャは "PyObject_Repr()" と同じです。この関数は文字列オブジェ
   クトか Unicode オブジェクトを返さなければなりません。理想的には、こ
   の関数が返す文字列は、適切な環境で "eval()" に渡した場合、同じ値を
   持つオブジェクトになるような文字列でなければなりません。不可能な場
   合には、オブジェクトの型と値から導出した内容の入った "'<'" から始ま
   って "'>'" で終わる文字列を返さなければなりません。

   このフィールドが設定されていない場合、 "<%s object at %p>" の形式を
   とる文字列が返されます。 "%s" は型の名前に、 "%p" はオブジェクトの
   メモリアドレスに置き換えられます。

   サブタイプはこのフィールドを継承します。

PyNumberMethods* tp_as_number

   数値プロトコルを実装した追加の構造体を指すポインタです。これらのフ
   ィールドについては 数値オブジェクト構造体 で説明されています。

   "tp_as_number" フィールドは継承されませんが、そこの含まれるフィール
   ドが個別に継承されます。

PySequenceMethods* tp_as_sequence

   シーケンスプロトコルを実装した追加の構造体を指すポインタです。これ
   らのフィールドについては シーケンスオブジェクト構造体 で説明されて
   います。

   "tp_as_sequence" フィールドは継承されませんが、これに含まれるフィー
   ルドが個別に継承されます。

PyMappingMethods* tp_as_mapping

   マッピングプロトコルを実装した追加の構造体を指すポインタです。これ
   らのフィールドについては マップオブジェクト構造体 で説明されていま
   す。

   "tp_as_mapping" フィールドは継承されませんが、これに含まれるフィー
   ルドが個別に継承されます。

hashfunc PyTypeObject.tp_hash

   オプションのポインタで、組み込み関数 "hash()" を実装している関数を
   指します。

   The signature is the same as for "PyObject_Hash()"; it must return
   a C long.  The value "-1" should not be returned as a normal return
   value; when an error occurs during the computation of the hash
   value, the function should set an exception and return "-1".

   このフィールドは明示的に "PyObject_HashNotImplemented()" に設定する
   ことで、親 type からのハッシュメソッドの継承をブロックすることがで
   きます。これは Python レベルでの "__hash__ = None" と同等に解釈され
   、 "isinstance(o, collections.Hashable)" が正しく "False" を返すよ
   うになります。逆もまた可能であることに注意してください - Python レ
   ベルで "__hash__ = None" を設定することで "tp_hash" スロットは
   "PyObject_HashNotImplemented()" に設定されます。

   When this field is not set, two possibilities exist: if the
   "tp_compare" and "tp_richcompare" fields are both *NULL*, a default
   hash value based on the object’s address is returned; otherwise, a
   "TypeError" is raised.

   This field is inherited by subtypes together with "tp_richcompare"
   and "tp_compare": a subtypes inherits all three of "tp_compare",
   "tp_richcompare", and "tp_hash", when the subtype’s "tp_compare",
   "tp_richcompare" and "tp_hash" are all *NULL*.

ternaryfunc PyTypeObject.tp_call

   オプションのポインタで、オブジェクトの呼び出しを実装している関数を
   指します。オブジェクトが呼び出し可能でない場合には *NULL* にしなけ
   ればなりません。シグネチャは "PyObject_Call()" と同じです。

   サブタイプはこのフィールドを継承します。

reprfunc PyTypeObject.tp_str

   オプションのポインタで、組み込みの演算 "str()" を実装している関数を
   指します。("str" が型の一つになったため、 "str()" は "str" のコンス
   トラクタを呼び出すことに注意してください。このコンストラクタは実際
   の処理を行う上で "PyObject_Str()" を呼び出し、さらに
   "PyObject_Str()" がこのハンドラを呼び出すことになります。)

   The signature is the same as for "PyObject_Str()"; it must return a
   string or a Unicode object.  This function should return a 「
   friendly」 string representation of the object, as this is the
   representation that will be used by the print statement.

   このフィールドが設定されていない場合、文字列表現を返すためには
   "PyObject_Repr()" が呼び出されます。

   サブタイプはこのフィールドを継承します。

getattrofunc PyTypeObject.tp_getattro

   オプションのポインタで、get-attribute を実装している関数を指します
   。

   シグネチャは "PyObject_GetAttr()" と同じです。 通常の属性検索を実装
   している "PyObject_GenericGetAttr()"  をこのフィールドに設定してお
   くとたいていの場合は便利です。

   このフィールドは "tp_getattr" と共にサブタイプに継承されます: すな
   わち、サブタイプの "tp_getattr" および "tp_getattro" が共に *NULL*
   の場合、サブタイプは基底タイプから "tp_getattr" と "tp_getattro" を
   両方とも継承します。

setattrofunc PyTypeObject.tp_setattro

   オプションのポインタで、属性の設定と削除を行う関数を指します。

   シグネチャは "PyObject_SetAttr()" と同じですが、 *v* に *NULL* を指
   定して属性を削除できるようにしなければなりません。 通常の属性設定を
   実装している "PyObject_GenericSetAttr()"  をこのフィールドに設定し
   ておくとたいていの場合は便利です。

   このフィールドは "tp_setattr" と共にサブタイプに継承されます: すな
   わち、サブタイプの "tp_setattr" および "tp_setattro" が共に *NULL*
   の場合、サブタイプは基底タイプから "tp_setattr" と "tp_setattro" を
   両方とも継承します。

PyBufferProcs* PyTypeObject.tp_as_buffer

   バッファインタフェースを実装しているオブジェクトにのみ関連する、一
   連のフィールド群が入った別の構造体を指すポインタです。構造体内の各
   フィールドは バッファオブジェクト構造体 (buffer object structure)
   で説明します。

   "tp_as_buffer" フィールド自体は継承されませんが、これに含まれるフィ
   ールドは個別に継承されます。

long PyTypeObject.tp_flags

   このフィールドは様々なフラグからなるビットマスクです。いくつかのフ
   ラグは、特定の状況において変則的なセマンティクスが適用されることを
   示します; その他のフラグは、型オブジェクト (あるいは "tp_as_number"
   、 "tp_as_sequence" 、 "tp_as_mapping" 、 および "tp_as_buffer" が
   参照している拡張機能構造体) の特定のフィールドのうち、過去から現在
   までずっと存在していたわけではないものが有効になっていることを示す
   ために使われます; フラグビットがクリアされていれば、フラグが保護し
   ているフィールドにはアクセスしない代わりに、その値はゼロか *NULL*
   になっているとみなさなければなりません。

   Inheritance of this field is complicated.  Most flag bits are
   inherited individually, i.e. if the base type has a flag bit set,
   the subtype inherits this flag bit.  The flag bits that pertain to
   extension structures are strictly inherited if the extension
   structure is inherited, i.e. the base type’s value of the flag bit
   is copied into the subtype together with a pointer to the extension
   structure.  The "Py_TPFLAGS_HAVE_GC" flag bit is inherited together
   with the "tp_traverse" and "tp_clear" fields, i.e. if the
   "Py_TPFLAGS_HAVE_GC" flag bit is clear in the subtype and the
   "tp_traverse" and "tp_clear" fields in the subtype exist (as
   indicated by the "Py_TPFLAGS_HAVE_RICHCOMPARE" flag bit) and have
   *NULL* values.

   以下に挙げるビットマスクは現在定義されているものです; フラグは "|"
   演算子で論理和を取って "tp_flags" フィールドの値を作成できます。
   "PyType_HasFeature()" マクロは型とフラグ値、 *tp* および *f* をとり
   、 "tp->tp_flags & f" が非ゼロかどうか調べます。

   Py_TPFLAGS_HAVE_GETCHARBUFFER

      If this bit is set, the "PyBufferProcs" struct referenced by
      "tp_as_buffer" has the "bf_getcharbuffer" field.

   Py_TPFLAGS_HAVE_SEQUENCE_IN

      If this bit is set, the "PySequenceMethods" struct referenced by
      "tp_as_sequence" has the "sq_contains" field.

   Py_TPFLAGS_GC

      This bit is obsolete.  The bit it used to name is no longer in
      use.  The symbol is now defined as zero.

   Py_TPFLAGS_HAVE_INPLACEOPS

      If this bit is set, the "PySequenceMethods" struct referenced by
      "tp_as_sequence" and the "PyNumberMethods" structure referenced
      by "tp_as_number" contain the fields for in-place operators. In
      particular, this means that the "PyNumberMethods" structure has
      the fields "nb_inplace_add", "nb_inplace_subtract",
      "nb_inplace_multiply", "nb_inplace_divide",
      "nb_inplace_remainder", "nb_inplace_power", "nb_inplace_lshift",
      "nb_inplace_rshift", "nb_inplace_and", "nb_inplace_xor", and
      "nb_inplace_or"; and the "PySequenceMethods" struct has the
      fields "sq_inplace_concat" and "sq_inplace_repeat".

   Py_TPFLAGS_CHECKTYPES

      If this bit is set, the binary and ternary operations in the
      "PyNumberMethods" structure referenced by "tp_as_number" accept
      arguments of arbitrary object types, and do their own type
      conversions if needed.  If this bit is clear, those operations
      require that all arguments have the current type as their type,
      and the caller is supposed to perform a coercion operation
      first.  This applies to "nb_add", "nb_subtract", "nb_multiply",
      "nb_divide", "nb_remainder", "nb_divmod", "nb_power",
      "nb_lshift", "nb_rshift", "nb_and", "nb_xor", and "nb_or".

   Py_TPFLAGS_HAVE_RICHCOMPARE

      If this bit is set, the type object has the "tp_richcompare"
      field, as well as the "tp_traverse" and the "tp_clear" fields.

   Py_TPFLAGS_HAVE_WEAKREFS

      If this bit is set, the "tp_weaklistoffset" field is defined.
      Instances of a type are weakly referenceable if the type’s
      "tp_weaklistoffset" field has a value greater than zero.

   Py_TPFLAGS_HAVE_ITER

      If this bit is set, the type object has the "tp_iter" and
      "tp_iternext" fields.

   Py_TPFLAGS_HAVE_CLASS

      If this bit is set, the type object has several new fields
      defined starting in Python 2.2: "tp_methods", "tp_members",
      "tp_getset", "tp_base", "tp_dict", "tp_descr_get",
      "tp_descr_set", "tp_dictoffset", "tp_init", "tp_alloc",
      "tp_new", "tp_free", "tp_is_gc", "tp_bases", "tp_mro",
      "tp_cache", "tp_subclasses", and "tp_weaklist".

   Py_TPFLAGS_HEAPTYPE

      型オブジェクト自体がヒープにメモリ確保される場合にセットされるビ
      ットです。型オブジェクト自体がヒープにメモリ確保される場合、イン
      スタンスの "ob_type" フィールドは型オブジェクトへの参照とみなさ
      れます。この場合、新たなインスタンスを生成する度に型オブジェクト
      を INCREF し、インスタンスを解放するたびに DECREF します (サブタ
      イプのインスタンスには適用されません; インスタンスが "ob_type"
      で参照している型だけが INCREF および DECREF されます)。

   Py_TPFLAGS_BASETYPE

      型を別の型の基底タイプとして使える場合にセットされるビットです。
      このビットがクリアならば、この型のサブタイプは生成できません
      (Java における 「final」 クラスに似たクラスになります)。

   Py_TPFLAGS_READY

      型オブジェクトが "PyType_Ready()" で完全に初期化されるとセットさ
      れるビットです。

   Py_TPFLAGS_READYING

      "PyType_Ready()" による型オブジェクトの初期化処理中にセットされ
      るビットです。

   Py_TPFLAGS_HAVE_GC

      This bit is set when the object supports garbage collection.  If
      this bit is set, instances must be created using
      "PyObject_GC_New()" and destroyed using "PyObject_GC_Del()".
      More information in section 循環参照ガベージコレクションをサポー
      トする.  This bit also implies that the GC-related fields
      "tp_traverse" and "tp_clear" are present in the type object; but
      those fields also exist when "Py_TPFLAGS_HAVE_GC" is clear but
      "Py_TPFLAGS_HAVE_RICHCOMPARE" is set.

   Py_TPFLAGS_DEFAULT

      This is a bitmask of all the bits that pertain to the existence
      of certain fields in the type object and its extension
      structures. Currently, it includes the following bits:
      "Py_TPFLAGS_HAVE_GETCHARBUFFER", "Py_TPFLAGS_HAVE_SEQUENCE_IN",
      "Py_TPFLAGS_HAVE_INPLACEOPS", "Py_TPFLAGS_HAVE_RICHCOMPARE",
      "Py_TPFLAGS_HAVE_WEAKREFS", "Py_TPFLAGS_HAVE_ITER", and
      "Py_TPFLAGS_HAVE_CLASS".

char* PyTypeObject.tp_doc

   オプションのポインタで、この型オブジェクトの docstring を与える NUL
   終端された C の文字列を指します。この値は型オブジェクトと型のインス
   タンスにおける "__doc__" 属性として公開されます。

   サブタイプはこのフィールドを継承 *しません* 。

The following three fields only exist if the
"Py_TPFLAGS_HAVE_RICHCOMPARE" flag bit is set.

traverseproc PyTypeObject.tp_traverse

   オプションのポインタで、ガベージコレクタのためのトラバーサル関数
   (traversal function) を指します。 "Py_TPFLAGS_HAVE_GC" がセットされ
   ている場合にのみ使われます。Pythonのガベージコレクションの枠組みに
   関する詳細は 循環参照ガベージコレクションをサポートする にあります
   。

   The "tp_traverse" pointer is used by the garbage collector to
   detect reference cycles. A typical implementation of a
   "tp_traverse" function simply calls "Py_VISIT()" on each of the
   instance’s members that are Python objects.  For example, this is
   function "local_traverse()" from the "thread" extension module:

      static int
      local_traverse(localobject *self, visitproc visit, void *arg)
      {
          Py_VISIT(self->args);
          Py_VISIT(self->kw);
          Py_VISIT(self->dict);
          return 0;
      }

   "Py_VISIT()" が循環参照になる恐れのあるメンバにだけ呼び出されている
   ことに注目してください。 "self->key" メンバもありますが、それは
   *NULL* か Python文字列なので、循環参照の一部になることはありません
   。

   一方、メンバが循環参照の一部になり得ないと判っていても、デバッグ目
   的で巡回したい場合があるかもしれないので、 "gc" モジュールの
   "get_referents()" 関数は循環参照になり得ないメンバも返します。

   "Py_VISIT()" は "local_traverse()" が *visit* と *arg* という決まっ
   た名前の引数を持つことを要求します。

   This field is inherited by subtypes together with "tp_clear" and
   the "Py_TPFLAGS_HAVE_GC" flag bit: the flag bit, "tp_traverse", and
   "tp_clear" are all inherited from the base type if they are all
   zero in the subtype *and* the subtype has the
   "Py_TPFLAGS_HAVE_RICHCOMPARE" flag bit set.

inquiry PyTypeObject.tp_clear

   オプションのポインタで、ガベージコレクタにおける消去関数 (clear
   function) を指します。 "Py_TPFLAGS_HAVE_GC" がセットされている場合
   にのみ使われます。

   "tp_clear" メンバ関数は GC が検出した循環しているゴミの循環参照を壊
   すために用いられます。総合的な視点で考えると、システム内の全ての
   "tp_clear" 関数が連携して、全ての循環参照を破壊しなければなりません
   。 (訳注: ある型が "tp_clear" を実装しなくても全ての循環参照が破壊
   できるのであれば実装しなくても良い) これはとても繊細で、もし少しで
   も不確かな部分があるのであれば、 "tp_clear" 関数を提供するべきです
   。例えば、タプルは "tp_clear" を実装しません。なぜなら、タプルだけ
   で構成された循環参照がみつかることは無いからです。従って、タプル以
   外の型の "tp_clear" 関数だけで、タプルを含むどんな循環参照も必ず破
   壊できることになります。これは簡単に判ることではなく、 "tp_clear"
   の実装を避ける良い理由はめったにありません。

   次の例にあるように、 "tp_clear" の実装は、インスタンスから Python
   オブジェクトだと思われるメンバへの参照を外し、それらのメンバへのポ
   インタに *NULL* をセットすべきです:

      static int
      local_clear(localobject *self)
      {
          Py_CLEAR(self->key);
          Py_CLEAR(self->args);
          Py_CLEAR(self->kw);
          Py_CLEAR(self->dict);
          return 0;
      }

   参照のクリアはデリケートなので、 "Py_CLEAR()" マクロを使うべきです:
   ポインタを *NULL* にセットするまで、そのオブジェクトの参照カウント
   をデクリメントしてはいけません。参照カウントのデクリメントすると、
   そのオブジェクトが破棄されるかもしれず、 (そのオブジェクトに関連付
   けられたファイナライザ、弱参照のコールバックにより) 任意のPythonコ
   ードの実行を含む後片付け処理が実行されるかもしれないからです。もし
   そういったコードが再び *self* を参照することがあれば、すでに持って
   いたオブジェクトへのポインタは *NULL* になっているので、 *self* は
   所有していたオブジェクトをもう利用できないことを認識できます。
   "Py_CLEAR()" マクロはその手続きを安全な順番で実行します。

   "tp_clear" 関数の目的は参照カウントを破壊することなので、 Python 文
   字列や Python 整数のような、循環参照に含むことのできないオブジェク
   トをクリアする必要はありません。一方、所有する全ての Python オブジ
   ェクトをクリアするようにし、その型の "tp_dealloc" 関数が "tp_clear"
   関数を実行するようにすると実装が楽になるでしょう。

   Pythonのガベージコレクションの仕組みについての詳細は、 循環参照ガベ
   ージコレクションをサポートする にあります。

   This field is inherited by subtypes together with "tp_traverse" and
   the "Py_TPFLAGS_HAVE_GC" flag bit: the flag bit, "tp_traverse", and
   "tp_clear" are all inherited from the base type if they are all
   zero in the subtype *and* the subtype has the
   "Py_TPFLAGS_HAVE_RICHCOMPARE" flag bit set.

richcmpfunc PyTypeObject.tp_richcompare

   An optional pointer to the rich comparison function, whose
   signature is "PyObject *tp_richcompare(PyObject *a, PyObject *b,
   int op)".

   この関数は、比較結果を返すべきです。(普通は "Py_True" か "Py_False"
   です。) 比較が未定義の場合は、"Py_NotImplemented" を、それ以外のエ
   ラーが発生した場合には例外状態をセットして "NULL" を返さなければな
   りません。

   注釈: 限られた種類の比較だけが可能 (例えば、 "==" と "!=" が可能
     で "<" などが不可能) な型を実装したい場合、拡張比較関数で直接
     "TypeError" を返します。

   This field is inherited by subtypes together with "tp_compare" and
   "tp_hash": a subtype inherits all three of "tp_compare",
   "tp_richcompare", and "tp_hash", when the subtype’s "tp_compare",
   "tp_richcompare", and "tp_hash" are all *NULL*.

   "tp_richcompare" および "PyObject_RichCompare()" 関数の第三引数に使
   うための定数としては以下が定義されています:

   +------------------+--------------+
   | 定数             | 比較         |
   +==================+==============+
   | "Py_LT"          | "<"          |
   +------------------+--------------+
   | "Py_LE"          | "<="         |
   +------------------+--------------+
   | "Py_EQ"          | "=="         |
   +------------------+--------------+
   | "Py_NE"          | "!="         |
   +------------------+--------------+
   | "Py_GT"          | ">"          |
   +------------------+--------------+
   | "Py_GE"          | ">="         |
   +------------------+--------------+

The next field only exists if the "Py_TPFLAGS_HAVE_WEAKREFS" flag bit
is set.

long PyTypeObject.tp_weaklistoffset

   型のインスタンスが弱参照可能な場合、このフィールドはゼロよりも大き
   な数になり、インスタンス構造体における弱参照リストの先頭を示すオフ
   セットが入ります (GC ヘッダがある場合には無視します); このオフセッ
   ト値は "PyObject_ClearWeakRefs()" および "PyWeakref_*()" 関数が利用
   します。インスタンス構造体には、 *NULL* に初期化された "PyObject*"
   型のフィールドが入っていなければなりません。

   このフィールドを "tp_weaklist" と混同しないようにしてください; これ
   は型オブジェクト自身への弱参照からなるリストの先頭です。

   このフィールドはサブタイプに継承されますが、以下の規則を読んでくだ
   さい。サブタイプはこのオフセット値をオーバライドすることがあります;
   従って、サブタイプでは弱参照リストの先頭が基底タイプとは異なる場合
   があります。リストの先頭は常に "tp_weaklistoffset" で分かるはずなの
   で、このことは問題にはならないはずです。

   "class" 文で定義された型に "__slots__" 宣言が全くなく、かつ基底タイ
   プが弱参照可能でない場合、その型を弱参照可能にするには弱参照リスト
   の先頭を表すスロットをインスタンスデータレイアウト構造体に追加し、
   スロットのオフセットを "tp_weaklistoffset" に設定します。

   型の "__slots__" の宣言に "__weakref__" という名前のスロットが含ま
   れているとき、スロットはその型のインスタンスにおける弱参照リストの
   先頭を表すスロットになり、スロットのオフセットが型の
   "tp_weaklistoffset" に入ります。

   型の "__slots__" 宣言が "__weakref__" という名前のスロットを含んで
   いないとき、その型は基底タイプから "tp_weaklistoffset" を継承します
   。

The next two fields only exist if the "Py_TPFLAGS_HAVE_ITER" flag bit
is set.

getiterfunc PyTypeObject.tp_iter

   An optional pointer to a function that returns an iterator for the
   object.  Its presence normally signals that the instances of this
   type are iterable (although sequences may be iterable without this
   function, and classic instances always have this function, even if
   they don’t define an "__iter__()" method).

   この関数は "PyObject_GetIter()" と同じシグネチャを持っています。

   サブタイプはこのフィールドを継承します。

iternextfunc PyTypeObject.tp_iternext

   An optional pointer to a function that returns the next item in an
   iterator. When the iterator is exhausted, it must return *NULL*; a
   "StopIteration" exception may or may not be set.  When another
   error occurs, it must return *NULL* too.  Its presence normally
   signals that the instances of this type are iterators (although
   classic instances always have this function, even if they don’t
   define a "next()" method).

   イテレータ型では、 "tp_iter" 関数も定義されていなければならず、その
   関数は (新たなイテレータインスタンスではなく) イテレータインスタン
   ス自体を返さねばなりません。

   この関数のシグネチャは "PyIter_Next()" と同じです。

   サブタイプはこのフィールドを継承します。

The next fields, up to and including "tp_weaklist", only exist if the
"Py_TPFLAGS_HAVE_CLASS" flag bit is set.

struct PyMethodDef* PyTypeObject.tp_methods

   オプションのポインタで、この型の正規 (regular) のメソッドを宣言して
   いる "PyMethodDef" 構造体からなる、 *NULL* で終端された静的な配列を
   指します。

   配列の各要素ごとに、メソッドデスクリプタの入った、要素が型の辞書 (
   下記の "tp_dict" 参照) に追加されます。

   サブタイプはこのフィールドを継承しません (メソッドは別個のメカニズ
   ムで継承されています)。

struct PyMemberDef* PyTypeObject.tp_members

   オプションのポインタで、型の正規 (regular) のデータメンバ (フィール
   ドおよびスロット) を宣言している "PyMemberDef" 構造体からなる、
   *NULL* で終端された静的な配列を指します。

   配列の各要素ごとに、メンバデスクリプタの入った要素が型の辞書 (下記
   の "tp_dict" 参照) に追加されます。

   サブタイプはこのフィールドを継承しません (メンバは別個のメカニズム
   で継承されています)。

struct PyGetSetDef* PyTypeObject.tp_getset

   オプションのポインタで、インスタンスの算出属性 (computed attribute)
   を宣言している "PyGetSetDef" 構造体からなる、 *NULL* で終端された静
   的な配列を指します。

   配列の各要素ごとに、 getter/setter デスクリプタの入った、要素が型の
   辞書 (下記の "tp_dict" 参照) に追加されます。

   サブタイプはこのフィールドを継承しません (算出属性は別個のメカニズ
   ムで継承されています)。

PyTypeObject* PyTypeObject.tp_base

   オプションのポインタで、型に関するプロパティを継承する基底タイプを
   指します。このフィールドのレベルでは、単継承 (single inheritance)
   だけがサポートされています; 多重継承はメタタイプの呼び出しによる動
   的な型オブジェクトの生成を必要とします。

   (当たり前ですが) サブタイプはこのフィールドを継承しません。しかし、
   このフィールドのデフォルト値は (Python プログラマは "object" 型とし
   て知っている) "&PyBaseObject_Type" になります。

PyObject* PyTypeObject.tp_dict

   型の辞書は "PyType_Ready()" によってこのフィールドに収められます。

   このフィールドは通常、 "PyType_Ready()" を呼び出す前に *NULL* に初
   期化しておかなければなりません; あるいは、型の初期属性の入った辞書
   で初期化しておいてもかまいません。 "PyType_Ready()" が型をひとたび
   初期化すると、型の新たな属性をこの辞書に追加できるのは、属性が
   ("__add__()" のような) オーバロード用演算でないときだけです。

   サブタイプはこのフィールドを継承しません (が、この辞書内で定義され
   ている属性は異なるメカニズムで継承されます)。

descrgetfunc PyTypeObject.tp_descr_get

   オプションのポインタで、デスクリプタの get 関数を指します。

   関数のシグネチャは次のとおりです

      PyObject * tp_descr_get(PyObject *self, PyObject *obj, PyObject *type);

   サブタイプはこのフィールドを継承します。

descrsetfunc PyTypeObject.tp_descr_set

   オプションのポインタで、デスクリプタの値の設定と削除を行う関数を指
   します。

   関数のシグネチャは次のとおりです

      int tp_descr_set(PyObject *self, PyObject *obj, PyObject *value);

   *value* 引数に *NULL* を設定して値を消します。 このフィールドはサブ
   タイプに継承されます。

long PyTypeObject.tp_dictoffset

   型のインスタンスにインスタンス変数の入った辞書がある場合、このフィ
   ールドは非ゼロの値になり、型のインスタンスデータ構造体におけるイン
   スタンス変数辞書へのオフセットが入ります; このオフセット値は
   "PyObject_GenericGetAttr()" が使います。

   このフィールドを "tp_dict" と混同しないようにしてください; これは型
   オブジェクト自身の属性の辞書です。

   このフィールドの値がゼロより大きければ、値はインスタンス構造体の先
   頭からの オフセットを表します。値がゼロより小さければ、インスタンス
   構造体の *末尾* からのオフセットを表します。負のオフセットを使うコ
   ストは比較的高くつくので、 インスタンス構造体に可変長部分があるとき
   のみ使うべきです。例えば、 "str" や "tuple" のサブタイプにインスタ
   ンス変数の辞書を追加する場合には、負のオフセットを使います。この場
   合、たとえ辞書が基本のオブジェクトレイアウトに含まれていなくても、
   "tp_basicsize" フィールドは追加された辞書を考慮にいれなければならな
   いことに注意してください。ポインタサイズが 4 バイトのシステムでは、
   構造体の最後尾に辞書が宣言されていることを示す場合、
   "tp_dictoffset" を "-4" にしなければなりません。

   負の "tp_dictoffset" から、インスタンスでの実際のオフセットを計算す
   るには以下のようにします:

      dictoffset = tp_basicsize + abs(ob_size)*tp_itemsize + tp_dictoffset
      if dictoffset is not aligned on sizeof(void*):
          round up to sizeof(void*)

   where "tp_basicsize", "tp_itemsize" and "tp_dictoffset" are taken
   from the type object, and "ob_size" is taken from the instance.
   The absolute value is taken because long ints use the sign of
   "ob_size" to store the sign of the number.  (There’s never a need
   to do this calculation yourself; it is done for you by
   "_PyObject_GetDictPtr()".)

   このフィールドはサブタイプに継承されますが、以下の規則を読んでくだ
   さい。サブタイプはこのオフセット値をオーバライドすることがあります;
   従って、サブタイプでは辞書のオフセットが基底タイプとは異なる場合が
   あります。辞書のオフセットは常に "tp_dictoffset" で分かるはずなので
   、このことは問題にはならないはずです。

   "class" 文で定義された型に "__slots__" 宣言がなく、かつ基底タイプの
   全てにインスタンス変数辞書がない場合、辞書のスロットをインスタンス
   データレイアウト構造体に追加し、スロットのオフセットを
   "tp_dictoffset" に設定します。

   "class" 文で定義された型に "__slots__" 宣言がある場合、この型は基底
   タイプから "tp_dictoffset" を継承します。

   ("__dict__" という名前のスロットを "__slots__" 宣言に追加しても、期
   待どおりの効果は得られず、単に混乱を招くだけになります。とはいえ、
   これは将来 "__weakref__" のように追加されるはずです。)

initproc PyTypeObject.tp_init

   オプションのポインタで、インスタンス初期化関数を指します。

   この関数はクラスにおける "__init__()" メソッドに対応します。
   "__init__()" と同様、 "__init__()" を呼び出さずにインスタンスを作成
   できます。また、 "__init__()" を再度呼び出してインスタンスの再初期
   化もできます。

   関数のシグネチャは次のとおりです

      int tp_init(PyObject *self, PyObject *args, PyObject *kwds)

   *self* 引数は初期化するインスタンスです; *args* および *kwds* 引数
   は、 "__init__()" を呼び出す際の固定引数およびキーワード引数です。

   The "tp_init" function, if not *NULL*, is called when an instance
   is created normally by calling its type, after the type’s "tp_new"
   function has returned an instance of the type.  If the "tp_new"
   function returns an instance of some other type that is not a
   subtype of the original type, no "tp_init" function is called; if
   "tp_new" returns an instance of a subtype of the original type, the
   subtype’s "tp_init" is called.  (VERSION NOTE: described here is
   what is implemented in Python 2.2.1 and later.  In Python 2.2, the
   "tp_init" of the type of the object returned by "tp_new" was always
   called, if not *NULL*.)

   サブタイプはこのフィールドを継承します。

allocfunc PyTypeObject.tp_alloc

   オプションのポインタで、インスタンスのメモリ確保関数を指します。

   関数のシグネチャは次のとおりです

      PyObject *tp_alloc(PyTypeObject *self, Py_ssize_t nitems)

   この関数の目的は、メモリ確保をメモリ初期化から分離することにありま
   す。この関数は、インスタンス用の的確なサイズ、適切なアラインメント
   、ゼロによる初期化がなされ、 "ob_refcnt" を "1" に、 "ob_type" を型
   引数 (type argument) にセットしたメモリブロックへのポインタを返さね
   ばなりません。型の "tp_itemsize" がゼロでない場合、オブジェクトの
   "ob_size" フィールドは *nitems* に初期化され、確保されるメモリブロ
   ックの長さは "tp_basicsize + nitems*tp_itemsize" を "sizeof(void*)"
   の倍数に切り上げた値になるはずです; それ以外の場合、 *nitems* の値
   は使われず、メモリブロックの長さは "tp_basicsize" になるはずです。

   この関数をインスタンス初期化の他のどの処理にも、追加でメモリ確保を
   する場合でさえ使ってはなりません; そうした処理は "tp_new" で行わね
   ばなりません。

   静的なサブタイプはこのフィールドを継承しますが、動的なサブタイプ
   ("class" 文で生成するサブタイプ) の場合は継承しません; 後者の場合、
   このフィールドは常に "PyType_GenericAlloc()" にセットされ、標準のヒ
   ープ上メモリ確保戦略が強制されます。静的に定義する型の場合でも、
   "PyType_GenericAlloc()" を推奨します。

newfunc PyTypeObject.tp_new

   オプションのポインタで、インスタンス生成関数を指します。

   このフィールドが *NULL* を指している型では、型を呼び出して新たなイ
   ンスタンスを生成できません; こうした型では、おそらくファクトリ関数
   のように、インスタンスを生成する他の方法があるはずです。

   関数のシグネチャは次のとおりです

      PyObject *tp_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds)

   *subtype* 引数は生成するオブジェクトの型です; *args* および *kwds*
   引数は、型を呼び出すときの位置引数およびキーワード引数です。サブタ
   イプは "tp_new" 関数を呼び出すときに使う型と同じである必要はないこ
   とに注意してください; その型の (無関係ではない) サブタイプのことも
   あります。

   "tp_new" 関数は "subtype->tp_alloc(subtype, nitems)" を呼び出してオ
   ブジェクトのメモリ領域を確保し、初期化で絶対に必要とされる処理だけ
   を行います。省略したり繰り返したりしても問題のない初期化処理は
   "tp_init" ハンドラ内に配置しなければなりません。だいたいの目安とし
   ては、変更不能な型では初期化は全て "tp_new" で行い、一方、変更可能
   な型ではほとんどの初期化を "tp_init" に回すべきです。

   This field is inherited by subtypes, except it is not inherited by
   static types whose "tp_base" is *NULL* or "&PyBaseObject_Type".
   The latter exception is a precaution so that old extension types
   don’t become callable simply by being linked with Python 2.2.

destructor PyTypeObject.tp_free

   An optional pointer to an instance deallocation function.

   The signature of this function has changed slightly: in Python 2.2
   and 2.2.1, its signature is "destructor":

      void tp_free(PyObject *)

   In Python 2.3 and beyond, its signature is "freefunc":

      void tp_free(void *)

   The only initializer that is compatible with both versions is
   "_PyObject_Del", whose definition has suitably adapted in Python
   2.3.

   静的なサブタイプはこのフィールドを継承しますが、動的なサブタイプ
   ("class" 文で生成するサブタイプ) の場合は継承しません; 後者の場合、
   このフィールドには "PyType_GenericAlloc()" と "Py_TPFLAGS_HAVE_GC"
   フラグビットの値に対応させるのにふさわしいメモリ解放関数がセットさ
   れます。

inquiry PyTypeObject.tp_is_gc

   オプションのポインタで、ガベージコレクタから呼び出される関数を指し
   ます。

   ガベージコレクタは、オブジェクトを回収して良いかどうかを知る必要が
   あります。通常は、オブジェクトの型の "tp_flags" フィールドを見て、
   "Py_TPFLAGS_HAVE_GC" フラグビットを調べるだけで十分です。しかし、あ
   る型では静的にメモリ確保されたインスタンスと動的にメモリ確保された
   インスタンスが混じっていて、静的にメモリ確保されたインスタンスは回
   収できません。こうした型では、関数を定義しなければなりません; 関数
   はインスタンスが回収可能の場合には "1" を、回収不能の場合には "0"
   を返さねばなりません。シグネチャは

      int tp_is_gc(PyObject *self)

   (上記のような型の例は、型オブジェクト自体です。メタタイプ
   "PyType_Type" は、型のメモリ確保が静的か動的かを区別するためにこの
   関数を定義しています。)

   This field is inherited by subtypes.  (VERSION NOTE: in Python 2.2,
   it was not inherited.  It is inherited in 2.2.1 and later
   versions.)

PyObject* PyTypeObject.tp_bases

   基底型からなるタプルです。

   "class" 文で生成されたクラスの場合このフィールドがセットされます。
   静的に定義されている型の場合には、このフィールドは *NULL* になりま
   す。

   このフィールドは継承されません。

PyObject* PyTypeObject.tp_mro

   基底タイプ群を展開した集合が入っているタプルです。集合は該当する型
   自体からはじまり、 "object" で終わります。メソッド解決順序 (Method
   Resolution Order) に従って並んでいます。

   このフィールドは継承されません; フィールドの値は "PyType_Ready()"
   で毎回計算されます。

PyObject* PyTypeObject.tp_cache

   使用されていません。継承されません。内部で使用するためだけのもので
   す。

PyObject* PyTypeObject.tp_subclasses

   サブクラスへの弱参照からなるリストです。継承されません。内部で使用
   するためだけのものです。

PyObject* PyTypeObject.tp_weaklist

   この型オブジェクトに対する弱参照からなるリストの先頭です。

The remaining fields are only defined if the feature test macro
"COUNT_ALLOCS" is defined, and are for internal use only. They are
documented here for completeness.  None of these fields are inherited
by subtypes. See the "PYTHONSHOWALLOCCOUNT" environment variable.

Py_ssize_t PyTypeObject.tp_allocs

   メモリ確保の回数です。

Py_ssize_t PyTypeObject.tp_frees

   メモリ解放の回数です。

Py_ssize_t PyTypeObject.tp_maxalloc

   同時にメモリ確保できる最大オブジェクト数です。

PyTypeObject* PyTypeObject.tp_next

   次のゼロでない "tp_allocs" フィールドを持つ型オブジェクトへのポイン
   タです。

また、 Python のガベージコレクションでは、 *tp_dealloc* を呼び出すのは
オブジェクトを生成したスレッドだけではなく、任意の Python スレッドかも
しれないという点にも注意して下さい。 (オブジェクトが循環参照の一部の場
合、任意のスレッドのガベージコレクションによって解放されてしまうかもし
れません)。Python API 側からみれば、 *tp_dealloc* を呼び出すスレッドは
グローバルインタプリタロック (GIL: Global Interpreter Lock) を獲得する
ので、これは問題ではありません。しかしながら、削除されようとしているオ
ブジェクトが何らかの C や C++ ライブラリ由来のオブジェクトを削除する場
合、 *tp_dealloc* を呼び出すスレッドのオブジェクトを削除することで、ラ
イブラリの仮定している何らかの規約に違反しないように気を付ける必要があ
ります。


数値オブジェクト構造体
**********************

PyNumberMethods

   This structure holds pointers to the functions which an object uses
   to implement the number protocol.  Almost every function below is
   used by the function of similar name documented in the 数値型プロト
   コル (number protocol) section.

   以下は構造体の定義です:

      typedef struct {
           binaryfunc nb_add;
           binaryfunc nb_subtract;
           binaryfunc nb_multiply;
           binaryfunc nb_divide;
           binaryfunc nb_remainder;
           binaryfunc nb_divmod;
           ternaryfunc nb_power;
           unaryfunc nb_negative;
           unaryfunc nb_positive;
           unaryfunc nb_absolute;
           inquiry nb_nonzero;       /* Used by PyObject_IsTrue */
           unaryfunc nb_invert;
           binaryfunc nb_lshift;
           binaryfunc nb_rshift;
           binaryfunc nb_and;
           binaryfunc nb_xor;
           binaryfunc nb_or;
           coercion nb_coerce;       /* Used by the coerce() function */
           unaryfunc nb_int;
           unaryfunc nb_long;
           unaryfunc nb_float;
           unaryfunc nb_oct;
           unaryfunc nb_hex;

           /* Added in release 2.0 */
           binaryfunc nb_inplace_add;
           binaryfunc nb_inplace_subtract;
           binaryfunc nb_inplace_multiply;
           binaryfunc nb_inplace_divide;
           binaryfunc nb_inplace_remainder;
           ternaryfunc nb_inplace_power;
           binaryfunc nb_inplace_lshift;
           binaryfunc nb_inplace_rshift;
           binaryfunc nb_inplace_and;
           binaryfunc nb_inplace_xor;
           binaryfunc nb_inplace_or;

           /* Added in release 2.2 */
           binaryfunc nb_floor_divide;
           binaryfunc nb_true_divide;
           binaryfunc nb_inplace_floor_divide;
           binaryfunc nb_inplace_true_divide;

           /* Added in release 2.5 */
           unaryfunc nb_index;
      } PyNumberMethods;

Binary and ternary functions may receive different kinds of arguments,
depending on the flag bit "Py_TPFLAGS_CHECKTYPES":

* If "Py_TPFLAGS_CHECKTYPES" is not set, the function arguments are
  guaranteed to be of the object’s type; the caller is responsible for
  calling the coercion method specified by the "nb_coerce" member to
  convert the arguments:

  coercion PyNumberMethods.nb_coerce

     This function is used by "PyNumber_CoerceEx()" and has the same
     signature.  The first argument is always a pointer to an object
     of the defined type.  If the conversion to a common 「larger」
     type is possible, the function replaces the pointers with new
     references to the converted objects and returns "0".  If the
     conversion is not possible, the function returns "1".  If an
     error condition is set, it will return "-1".

* If the "Py_TPFLAGS_CHECKTYPES" flag is set, binary and ternary
  functions must check the type of all their operands, and implement
  the necessary conversions (at least one of the operands is an
  instance of the defined type).  This is the recommended way; with
  Python 3 coercion will disappear completely.

If the operation is not defined for the given operands, binary and
ternary functions must return "Py_NotImplemented", if another error
occurred they must return "NULL" and set an exception.


マップオブジェクト構造体
************************

PyMappingMethods

   この構造体はマップ型プロトコルを実装するために使われる関数群へのポ
   インタを保持しています。 以下の3つのメンバを持っています:

lenfunc PyMappingMethods.mp_length

   この関数は "PyMapping_Length()" や "PyObject_Size()" から利用され、
   それらと同じシグネチャを持っています。オブジェクトが定義された長さ
   を持たない場合は、このスロットは *NULL* に設定されることがあります
   。

binaryfunc PyMappingMethods.mp_subscript

   この関数は "PyObject_GetItem()" から利用され、同じシグネチャを持っ
   ています。このスロットは "PyMapping_Check()" が "1" を返すためには
   必要で、そうでなければ *NULL* の場合があります。

objobjargproc PyMappingMethods.mp_ass_subscript

   この関数は "PyObject_SetItem()" および "PyObject_DelItem()" から利
   用されます。 "PyObject_SetItem()" と同じシグネチャを持ちますが、
   *v* に *NULL* を設定して要素の削除もできます。このスロットが *NULL*
   の場合は、このオブジェクトはアイテムの代入と削除をサポートしません
   。


シーケンスオブジェクト構造体
****************************

PySequenceMethods

   この構造体はシーケンス型プロトコルを実装するために使われる関数群へ
   のポインタを保持しています。

lenfunc PySequenceMethods.sq_length

   この関数は "PySequence_Size()" や "PyObject_Size()" から利用され、
   それらと同じシグネチャを持っています。

binaryfunc PySequenceMethods.sq_concat

   この関数は "PySequence_Concat()" で利用され、同じシグネチャを持って
   います。また、 "+" 演算子でも、 "nb_add" スロットによる数値加算を試
   した後に利用されます。

ssizeargfunc PySequenceMethods.sq_repeat

   この関数は "PySequence_Repeat()" で利用され、同じシグネチャを持って
   います。また、 "*" 演算でも、 "nb_multiply" スロットによる数値乗算
   を試したあとに利用されます。

ssizeargfunc PySequenceMethods.sq_item

   この関数は "PySequence_GetItem()" から利用され、同じシグネチャを持
   っています。このスロットは "PySequence_Check()" が "1" を返すために
   は埋めなければならず、それ以外の場合は *NULL* の可能性があります。

   負のインデックスは次のように処理されます: "sq_length" スロットが埋
   められていれば、それを呼び出してシーケンスの長さから正のインデック
   スを計算し、 "sq_item" に渡します。 "sq_length" が *NULL* の場合は
   、インデックスはそのままこの関数に渡されます。

ssizeobjargproc PySequenceMethods.sq_ass_item

   この関数は "PySequence_SetItem()" から利用され、同じシグネチャを持
   っています。 オブジェクトが要素の代入と削除をサポートしていない場合
   は、このスロットは *NULL* かもしれません。

objobjproc PySequenceMethods.sq_contains

   この関数は "PySequence_Contains()" から利用され、同じシグネチャを持
   っています。このスロットは *NULL* の場合があり、その時
   "PySequence_Contains()" はシンプルにマッチするオブジェクトを見つけ
   るまでシーケンスを巡回します。

binaryfunc PySequenceMethods.sq_inplace_concat

   この関数は "PySequence_InPlaceConcat()" から利用され、同じシグネチ
   ャを持っています。この関数は最初のオペランドを修正してそれを返すべ
   きです。

ssizeargfunc PySequenceMethods.sq_inplace_repeat

   この関数は "PySequence_InPlaceRepeat()" から利用され、同じシグネチ
   ャを持っています。この関数は最初のオペランドを修正してそれを返すべ
   きです。


バッファオブジェクト構造体 (buffer object structure)
****************************************************

The buffer interface exports a model where an object can expose its
internal data as a set of chunks of data, where each chunk is
specified as a pointer/length pair.  These chunks are called
*segments* and are presumed to be non-contiguous in memory.

If an object does not export the buffer interface, then its
"tp_as_buffer" member in the "PyTypeObject" structure should be
*NULL*.  Otherwise, the "tp_as_buffer" will point to a "PyBufferProcs"
structure.

注釈: It is very important that your "PyTypeObject" structure uses
  "Py_TPFLAGS_DEFAULT" for the value of the "tp_flags" member rather
  than "0".  This tells the Python runtime that your "PyBufferProcs"
  structure contains the "bf_getcharbuffer" slot. Older versions of
  Python did not have this member, so a new Python interpreter using
  an old extension needs to be able to test for its presence before
  using it.

PyBufferProcs

   Structure used to hold the function pointers which define an
   implementation of the buffer protocol.

   The first slot is "bf_getreadbuffer", of type "readbufferproc". If
   this slot is *NULL*, then the object does not support reading from
   the internal data.  This is non-sensical, so implementors should
   fill this in, but callers should test that the slot contains a
   non-*NULL* value.

   The next slot is "bf_getwritebuffer" having type "writebufferproc".
   This slot may be *NULL* if the object does not allow writing into
   its returned buffers.

   The third slot is "bf_getsegcount", with type "segcountproc". This
   slot must not be *NULL* and is used to inform the caller how many
   segments the object contains.  Simple objects such as
   "PyString_Type" and "PyBuffer_Type" objects contain a single
   segment.

   The last slot is "bf_getcharbuffer", of type "charbufferproc". This
   slot will only be present if the "Py_TPFLAGS_HAVE_GETCHARBUFFER"
   flag is present in the "tp_flags" field of the object’s
   "PyTypeObject". Before using this slot, the caller should test
   whether it is present by using the "PyType_HasFeature()" function.
   If the flag is present, "bf_getcharbuffer" may be *NULL*,
   indicating that the object’s contents cannot be used as *8-bit
   characters*. The slot function may also raise an error if the
   object’s contents cannot be interpreted as 8-bit characters. For
   example, if the object is an array which is configured to hold
   floating point values, an exception may be raised if a caller
   attempts to use "bf_getcharbuffer" to fetch a sequence of 8-bit
   characters. This notion of exporting the internal buffers as 「text
   」 is used to distinguish between objects that are binary in
   nature, and those which have character-based content.

   注釈: The current policy seems to state that these characters may
     be multi-byte characters. This implies that a buffer size of *N*
     does not mean there are *N* characters present.

Py_TPFLAGS_HAVE_GETCHARBUFFER

   Flag bit set in the type structure to indicate that the
   "bf_getcharbuffer" slot is known.  This being set does not indicate
   that the object supports the buffer interface or that the
   "bf_getcharbuffer" slot is non-*NULL*.

Py_ssize_t (*readbufferproc)(PyObject *self, Py_ssize_t segment, void **ptrptr)

   Return a pointer to a readable segment of the buffer in "*ptrptr".
   This function is allowed to raise an exception, in which case it
   must return "-1". The *segment* which is specified must be zero or
   positive, and strictly less than the number of segments returned by
   the "bf_getsegcount" slot function.  On success, it returns the
   length of the segment, and sets "*ptrptr" to a pointer to that
   memory.

Py_ssize_t (*writebufferproc)(PyObject *self, Py_ssize_t segment, void **ptrptr)

   Return a pointer to a writable memory buffer in "*ptrptr", and the
   length of that segment as the function return value.  The memory
   buffer must correspond to buffer segment *segment*.  Must return
   "-1" and set an exception on error. "TypeError" should be raised if
   the object only supports read-only buffers, and "SystemError"
   should be raised when *segment* specifies a segment that doesn’t
   exist.

Py_ssize_t (*segcountproc)(PyObject *self, Py_ssize_t *lenp)

   Return the number of memory segments which comprise the buffer.  If
   *lenp* is not *NULL*, the implementation must report the sum of the
   sizes (in bytes) of all segments in "*lenp". The function cannot
   fail.

Py_ssize_t (*charbufferproc)(PyObject *self, Py_ssize_t segment, char **ptrptr)

   Return the size of the segment *segment* that *ptrptr*  is set to.
   "*ptrptr" is set to the memory buffer. Returns "-1" on error.
