|
|
|
@ -35,7 +35,7 @@ definition found there: |
|
|
|
The type object structure extends the :c:type:`PyVarObject` structure. The |
|
|
|
:attr:`ob_size` field is used for dynamic types (created by :func:`type_new`, |
|
|
|
usually called from a class statement). Note that :c:data:`PyType_Type` (the |
|
|
|
metatype) initializes :attr:`tp_itemsize`, which means that its instances (i.e. |
|
|
|
metatype) initializes :c:member:`~PyTypeObject.tp_itemsize`, which means that its instances (i.e. |
|
|
|
type objects) *must* have the :attr:`ob_size` field. |
|
|
|
|
|
|
|
|
|
|
|
@ -102,7 +102,7 @@ type objects) *must* have the :attr:`ob_size` field. |
|
|
|
should be just the type name. If the module is a submodule of a package, the |
|
|
|
full package name is part of the full module name. For example, a type named |
|
|
|
:class:`T` defined in module :mod:`M` in subpackage :mod:`Q` in package :mod:`P` |
|
|
|
should have the :attr:`tp_name` initializer ``"P.Q.M.T"``. |
|
|
|
should have the :c:member:`~PyTypeObject.tp_name` initializer ``"P.Q.M.T"``. |
|
|
|
|
|
|
|
For dynamically allocated type objects, this should just be the type name, and |
|
|
|
the module name explicitly stored in the type dict as the value for key |
|
|
|
@ -113,7 +113,7 @@ type objects) *must* have the :attr:`ob_size` field. |
|
|
|
attribute, and everything after the last dot is made accessible as the |
|
|
|
:attr:`__name__` attribute. |
|
|
|
|
|
|
|
If no dot is present, the entire :attr:`tp_name` field is made accessible as the |
|
|
|
If no dot is present, the entire :c:member:`~PyTypeObject.tp_name` field is made accessible as the |
|
|
|
:attr:`__name__` attribute, and the :attr:`__module__` attribute is undefined |
|
|
|
(unless explicitly set in the dictionary, as explained above). This means your |
|
|
|
type will be impossible to pickle. |
|
|
|
@ -127,13 +127,13 @@ type objects) *must* have the :attr:`ob_size` field. |
|
|
|
These fields allow calculating the size in bytes of instances of the type. |
|
|
|
|
|
|
|
There are two kinds of types: types with fixed-length instances have a zero |
|
|
|
:attr:`tp_itemsize` field, types with variable-length instances have a non-zero |
|
|
|
:attr:`tp_itemsize` field. For a type with fixed-length instances, all |
|
|
|
instances have the same size, given in :attr:`tp_basicsize`. |
|
|
|
:c:member:`~PyTypeObject.tp_itemsize` field, types with variable-length instances have a non-zero |
|
|
|
:c:member:`~PyTypeObject.tp_itemsize` field. For a type with fixed-length instances, all |
|
|
|
instances have the same size, given in :c:member:`~PyTypeObject.tp_basicsize`. |
|
|
|
|
|
|
|
For a type with variable-length instances, the instances must have an |
|
|
|
:attr:`ob_size` field, and the instance size is :attr:`tp_basicsize` plus N |
|
|
|
times :attr:`tp_itemsize`, where N is the "length" of the object. The value of |
|
|
|
:attr:`ob_size` field, and the instance size is :c:member:`~PyTypeObject.tp_basicsize` plus N |
|
|
|
times :c:member:`~PyTypeObject.tp_itemsize`, where N is the "length" of the object. The value of |
|
|
|
N is typically stored in the instance's :attr:`ob_size` field. There are |
|
|
|
exceptions: for example, ints use a negative :attr:`ob_size` to indicate a |
|
|
|
negative number, and N is ``abs(ob_size)`` there. Also, the presence of an |
|
|
|
@ -146,20 +146,20 @@ type objects) *must* have the :attr:`ob_size` field. |
|
|
|
:c:macro:`PyObject_HEAD` or :c:macro:`PyObject_VAR_HEAD` (whichever is used to |
|
|
|
declare the instance struct) and this in turn includes the :attr:`_ob_prev` and |
|
|
|
:attr:`_ob_next` fields if they are present. This means that the only correct |
|
|
|
way to get an initializer for the :attr:`tp_basicsize` is to use the |
|
|
|
way to get an initializer for the :c:member:`~PyTypeObject.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. |
|
|
|
|
|
|
|
These fields are inherited separately by subtypes. If the base type has a |
|
|
|
non-zero :attr:`tp_itemsize`, it is generally not safe to set |
|
|
|
:attr:`tp_itemsize` to a different non-zero value in a subtype (though this |
|
|
|
non-zero :c:member:`~PyTypeObject.tp_itemsize`, it is generally not safe to set |
|
|
|
:c:member:`~PyTypeObject.tp_itemsize` to a different non-zero value in a subtype (though this |
|
|
|
depends on the implementation of the base type). |
|
|
|
|
|
|
|
A note about alignment: if the variable items require a particular alignment, |
|
|
|
this should be taken care of by the value of :attr:`tp_basicsize`. Example: |
|
|
|
suppose a type implements an array of ``double``. :attr:`tp_itemsize` is |
|
|
|
this should be taken care of by the value of :c:member:`~PyTypeObject.tp_basicsize`. Example: |
|
|
|
suppose a type implements an array of ``double``. :c:member:`~PyTypeObject.tp_itemsize` is |
|
|
|
``sizeof(double)``. It is the programmer's responsibility that |
|
|
|
:attr:`tp_basicsize` is a multiple of ``sizeof(double)`` (assuming this is the |
|
|
|
:c:member:`~PyTypeObject.tp_basicsize` is a multiple of ``sizeof(double)`` (assuming this is the |
|
|
|
alignment requirement for ``double``). |
|
|
|
|
|
|
|
|
|
|
|
@ -175,10 +175,10 @@ type objects) *must* have the :attr:`ob_size` field. |
|
|
|
destructor function should free all references which the instance owns, free all |
|
|
|
memory buffers owned by the instance (using the freeing function corresponding |
|
|
|
to the allocation function used to allocate the buffer), and finally (as its |
|
|
|
last action) call the type's :attr:`tp_free` function. If the type is not |
|
|
|
last action) call the type's :c:member:`~PyTypeObject.tp_free` function. If the type is not |
|
|
|
subtypable (doesn't have the :const:`Py_TPFLAGS_BASETYPE` flag bit set), it is |
|
|
|
permissible to call the object deallocator directly instead of via |
|
|
|
:attr:`tp_free`. The object deallocator should be the one used to allocate the |
|
|
|
:c:member:`~PyTypeObject.tp_free`. The object deallocator should be the one used to allocate the |
|
|
|
instance; this is normally :c:func:`PyObject_Del` if the instance was allocated |
|
|
|
using :c:func:`PyObject_New` or :c:func:`PyObject_VarNew`, or |
|
|
|
:c:func:`PyObject_GC_Del` if the instance was allocated using |
|
|
|
@ -193,25 +193,25 @@ type objects) *must* have the :attr:`ob_size` field. |
|
|
|
|
|
|
|
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 :class:`StringIO` instance), the |
|
|
|
instance's :attr:`tp_repr` or :attr:`tp_str` function is called to convert it to |
|
|
|
a string. These are also called when the type's :attr:`tp_print` field is |
|
|
|
*NULL*. A type should never implement :attr:`tp_print` in a way that produces |
|
|
|
different output than :attr:`tp_repr` or :attr:`tp_str` would. |
|
|
|
instance's :c:member:`~PyTypeObject.tp_repr` or :c:member:`~PyTypeObject.tp_str` function is called to convert it to |
|
|
|
a string. These are also called when the type's :c:member:`~PyTypeObject.tp_print` field is |
|
|
|
*NULL*. A type should never implement :c:member:`~PyTypeObject.tp_print` in a way that produces |
|
|
|
different output than :c:member:`~PyTypeObject.tp_repr` or :c:member:`~PyTypeObject.tp_str` would. |
|
|
|
|
|
|
|
The print function is called with the same signature as :c:func:`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 :const:`Py_PRINT_RAW`. When the :const:`Py_PRINT_RAW` |
|
|
|
flag bit is set, the instance should be printed the same way as :attr:`tp_str` |
|
|
|
flag bit is set, the instance should be printed the same way as :c:member:`~PyTypeObject.tp_str` |
|
|
|
would format it; when the :const:`Py_PRINT_RAW` flag bit is clear, the instance |
|
|
|
should be printed the same was as :attr:`tp_repr` would format it. It should |
|
|
|
should be printed the same was as :c:member:`~PyTypeObject.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 :attr:`tp_print` field will be deprecated. In any case, |
|
|
|
it is recommended not to define :attr:`tp_print`, but instead to rely on |
|
|
|
:attr:`tp_repr` and :attr:`tp_str` for printing. |
|
|
|
It is possible that the :c:member:`~PyTypeObject.tp_print` field will be deprecated. In any case, |
|
|
|
it is recommended not to define :c:member:`~PyTypeObject.tp_print`, but instead to rely on |
|
|
|
:c:member:`~PyTypeObject.tp_repr` and :c:member:`~PyTypeObject.tp_str` for printing. |
|
|
|
|
|
|
|
This field is inherited by subtypes. |
|
|
|
|
|
|
|
@ -221,13 +221,13 @@ type objects) *must* have the :attr:`ob_size` field. |
|
|
|
An optional pointer to the get-attribute-string function. |
|
|
|
|
|
|
|
This field is deprecated. When it is defined, it should point to a function |
|
|
|
that acts the same as the :attr:`tp_getattro` function, but taking a C string |
|
|
|
that acts the same as the :c:member:`~PyTypeObject.tp_getattro` function, but taking a C string |
|
|
|
instead of a Python string object to give the attribute name. The signature is |
|
|
|
the same as for :c:func:`PyObject_GetAttrString`. |
|
|
|
|
|
|
|
This field is inherited by subtypes together with :attr:`tp_getattro`: a subtype |
|
|
|
inherits both :attr:`tp_getattr` and :attr:`tp_getattro` from its base type when |
|
|
|
the subtype's :attr:`tp_getattr` and :attr:`tp_getattro` are both *NULL*. |
|
|
|
This field is inherited by subtypes together with :c:member:`~PyTypeObject.tp_getattro`: a subtype |
|
|
|
inherits both :c:member:`~PyTypeObject.tp_getattr` and :c:member:`~PyTypeObject.tp_getattro` from its base type when |
|
|
|
the subtype's :c:member:`~PyTypeObject.tp_getattr` and :c:member:`~PyTypeObject.tp_getattro` are both *NULL*. |
|
|
|
|
|
|
|
|
|
|
|
.. c:member:: setattrfunc PyTypeObject.tp_setattr |
|
|
|
@ -235,13 +235,13 @@ type objects) *must* have the :attr:`ob_size` field. |
|
|
|
An optional pointer to the set-attribute-string function. |
|
|
|
|
|
|
|
This field is deprecated. When it is defined, it should point to a function |
|
|
|
that acts the same as the :attr:`tp_setattro` function, but taking a C string |
|
|
|
that acts the same as the :c:member:`~PyTypeObject.tp_setattro` function, but taking a C string |
|
|
|
instead of a Python string object to give the attribute name. The signature is |
|
|
|
the same as for :c:func:`PyObject_SetAttrString`. |
|
|
|
|
|
|
|
This field is inherited by subtypes together with :attr:`tp_setattro`: a subtype |
|
|
|
inherits both :attr:`tp_setattr` and :attr:`tp_setattro` from its base type when |
|
|
|
the subtype's :attr:`tp_setattr` and :attr:`tp_setattro` are both *NULL*. |
|
|
|
This field is inherited by subtypes together with :c:member:`~PyTypeObject.tp_setattro`: a subtype |
|
|
|
inherits both :c:member:`~PyTypeObject.tp_setattr` and :c:member:`~PyTypeObject.tp_setattro` from its base type when |
|
|
|
the subtype's :c:member:`~PyTypeObject.tp_setattr` and :c:member:`~PyTypeObject.tp_setattro` are both *NULL*. |
|
|
|
|
|
|
|
|
|
|
|
.. c:member:: void* PyTypeObject.tp_reserved |
|
|
|
@ -275,7 +275,7 @@ type objects) *must* have the :attr:`ob_size` field. |
|
|
|
objects which implement the number protocol. These fields are documented in |
|
|
|
:ref:`number-structs`. |
|
|
|
|
|
|
|
The :attr:`tp_as_number` field is not inherited, but the contained fields are |
|
|
|
The :c:member:`~PyTypeObject.tp_as_number` field is not inherited, but the contained fields are |
|
|
|
inherited individually. |
|
|
|
|
|
|
|
|
|
|
|
@ -285,7 +285,7 @@ type objects) *must* have the :attr:`ob_size` field. |
|
|
|
objects which implement the sequence protocol. These fields are documented |
|
|
|
in :ref:`sequence-structs`. |
|
|
|
|
|
|
|
The :attr:`tp_as_sequence` field is not inherited, but the contained fields |
|
|
|
The :c:member:`~PyTypeObject.tp_as_sequence` field is not inherited, but the contained fields |
|
|
|
are inherited individually. |
|
|
|
|
|
|
|
|
|
|
|
@ -295,7 +295,7 @@ type objects) *must* have the :attr:`ob_size` field. |
|
|
|
objects which implement the mapping protocol. These fields are documented in |
|
|
|
:ref:`mapping-structs`. |
|
|
|
|
|
|
|
The :attr:`tp_as_mapping` field is not inherited, but the contained fields |
|
|
|
The :c:member:`~PyTypeObject.tp_as_mapping` field is not inherited, but the contained fields |
|
|
|
are inherited individually. |
|
|
|
|
|
|
|
|
|
|
|
@ -323,9 +323,9 @@ type objects) *must* have the :attr:`ob_size` field. |
|
|
|
object raises :exc:`TypeError`. |
|
|
|
|
|
|
|
This field is inherited by subtypes together with |
|
|
|
:attr:`tp_richcompare`: a subtype inherits both of |
|
|
|
:attr:`tp_richcompare` and :attr:`tp_hash`, when the subtype's |
|
|
|
:attr:`tp_richcompare` and :attr:`tp_hash` are both *NULL*. |
|
|
|
:c:member:`~PyTypeObject.tp_richcompare`: a subtype inherits both of |
|
|
|
:c:member:`~PyTypeObject.tp_richcompare` and :c:member:`~PyTypeObject.tp_hash`, when the subtype's |
|
|
|
:c:member:`~PyTypeObject.tp_richcompare` and :c:member:`~PyTypeObject.tp_hash` are both *NULL*. |
|
|
|
|
|
|
|
|
|
|
|
.. c:member:: ternaryfunc PyTypeObject.tp_call |
|
|
|
@ -363,9 +363,9 @@ type objects) *must* have the :attr:`ob_size` field. |
|
|
|
convenient to set this field to :c:func:`PyObject_GenericGetAttr`, which |
|
|
|
implements the normal way of looking for object attributes. |
|
|
|
|
|
|
|
This field is inherited by subtypes together with :attr:`tp_getattr`: a subtype |
|
|
|
inherits both :attr:`tp_getattr` and :attr:`tp_getattro` from its base type when |
|
|
|
the subtype's :attr:`tp_getattr` and :attr:`tp_getattro` are both *NULL*. |
|
|
|
This field is inherited by subtypes together with :c:member:`~PyTypeObject.tp_getattr`: a subtype |
|
|
|
inherits both :c:member:`~PyTypeObject.tp_getattr` and :c:member:`~PyTypeObject.tp_getattro` from its base type when |
|
|
|
the subtype's :c:member:`~PyTypeObject.tp_getattr` and :c:member:`~PyTypeObject.tp_getattro` are both *NULL*. |
|
|
|
|
|
|
|
|
|
|
|
.. c:member:: setattrofunc PyTypeObject.tp_setattro |
|
|
|
@ -376,9 +376,9 @@ type objects) *must* have the :attr:`ob_size` field. |
|
|
|
convenient to set this field to :c:func:`PyObject_GenericSetAttr`, which |
|
|
|
implements the normal way of setting object attributes. |
|
|
|
|
|
|
|
This field is inherited by subtypes together with :attr:`tp_setattr`: a subtype |
|
|
|
inherits both :attr:`tp_setattr` and :attr:`tp_setattro` from its base type when |
|
|
|
the subtype's :attr:`tp_setattr` and :attr:`tp_setattro` are both *NULL*. |
|
|
|
This field is inherited by subtypes together with :c:member:`~PyTypeObject.tp_setattr`: a subtype |
|
|
|
inherits both :c:member:`~PyTypeObject.tp_setattr` and :c:member:`~PyTypeObject.tp_setattro` from its base type when |
|
|
|
the subtype's :c:member:`~PyTypeObject.tp_setattr` and :c:member:`~PyTypeObject.tp_setattro` are both *NULL*. |
|
|
|
|
|
|
|
|
|
|
|
.. c:member:: PyBufferProcs* PyTypeObject.tp_as_buffer |
|
|
|
@ -387,7 +387,7 @@ type objects) *must* have the :attr:`ob_size` field. |
|
|
|
which implement the buffer interface. These fields are documented in |
|
|
|
:ref:`buffer-structs`. |
|
|
|
|
|
|
|
The :attr:`tp_as_buffer` field is not inherited, but the contained fields are |
|
|
|
The :c:member:`~PyTypeObject.tp_as_buffer` field is not inherited, but the contained fields are |
|
|
|
inherited individually. |
|
|
|
|
|
|
|
|
|
|
|
@ -396,8 +396,8 @@ type objects) *must* have the :attr:`ob_size` field. |
|
|
|
This field is a bit mask of various flags. Some flags indicate variant |
|
|
|
semantics for certain situations; others are used to indicate that certain |
|
|
|
fields in the type object (or in the extension structures referenced via |
|
|
|
:attr:`tp_as_number`, :attr:`tp_as_sequence`, :attr:`tp_as_mapping`, and |
|
|
|
:attr:`tp_as_buffer`) that were historically not always present are valid; if |
|
|
|
:c:member:`~PyTypeObject.tp_as_number`, :c:member:`~PyTypeObject.tp_as_sequence`, :c:member:`~PyTypeObject.tp_as_mapping`, and |
|
|
|
:c:member:`~PyTypeObject.tp_as_buffer`) that were historically not always present are valid; if |
|
|
|
such a flag bit is clear, the type fields it guards must not be accessed and |
|
|
|
must be considered to have a zero or *NULL* value instead. |
|
|
|
|
|
|
|
@ -407,13 +407,13 @@ type objects) *must* have the :attr:`ob_size` field. |
|
|
|
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 :const:`Py_TPFLAGS_HAVE_GC` flag bit is inherited together with |
|
|
|
the :attr:`tp_traverse` and :attr:`tp_clear` fields, i.e. if the |
|
|
|
the :c:member:`~PyTypeObject.tp_traverse` and :c:member:`~PyTypeObject.tp_clear` fields, i.e. if the |
|
|
|
:const:`Py_TPFLAGS_HAVE_GC` flag bit is clear in the subtype and the |
|
|
|
:attr:`tp_traverse` and :attr:`tp_clear` fields in the subtype exist and have |
|
|
|
:c:member:`~PyTypeObject.tp_traverse` and :c:member:`~PyTypeObject.tp_clear` fields in the subtype exist and have |
|
|
|
*NULL* values. |
|
|
|
|
|
|
|
The following bit masks are currently defined; these can be ORed together using |
|
|
|
the ``|`` operator to form the value of the :attr:`tp_flags` field. The macro |
|
|
|
the ``|`` operator to form the value of the :c:member:`~PyTypeObject.tp_flags` field. The macro |
|
|
|
:c:func:`PyType_HasFeature` takes a type and a flags value, *tp* and *f*, and |
|
|
|
checks whether ``tp->tp_flags & f`` is non-zero. |
|
|
|
|
|
|
|
@ -453,7 +453,7 @@ type objects) *must* have the :attr:`ob_size` field. |
|
|
|
is set, instances must be created using :c:func:`PyObject_GC_New` and |
|
|
|
destroyed using :c:func:`PyObject_GC_Del`. More information in section |
|
|
|
:ref:`supporting-cycle-detection`. This bit also implies that the |
|
|
|
GC-related fields :attr:`tp_traverse` and :attr:`tp_clear` are present in |
|
|
|
GC-related fields :c:member:`~PyTypeObject.tp_traverse` and :c:member:`~PyTypeObject.tp_clear` are present in |
|
|
|
the type object. |
|
|
|
|
|
|
|
|
|
|
|
@ -481,8 +481,8 @@ type objects) *must* have the :attr:`ob_size` field. |
|
|
|
about Python's garbage collection scheme can be found in section |
|
|
|
:ref:`supporting-cycle-detection`. |
|
|
|
|
|
|
|
The :attr:`tp_traverse` pointer is used by the garbage collector to detect |
|
|
|
reference cycles. A typical implementation of a :attr:`tp_traverse` function |
|
|
|
The :c:member:`~PyTypeObject.tp_traverse` pointer is used by the garbage collector to detect |
|
|
|
reference cycles. A typical implementation of a :c:member:`~PyTypeObject.tp_traverse` function |
|
|
|
simply calls :c:func:`Py_VISIT` on each of the instance's members that are Python |
|
|
|
objects. For example, this is function :c:func:`local_traverse` from the |
|
|
|
:mod:`_thread` extension module:: |
|
|
|
@ -508,9 +508,9 @@ type objects) *must* have the :attr:`ob_size` field. |
|
|
|
:c:func:`local_traverse` to have these specific names; don't name them just |
|
|
|
anything. |
|
|
|
|
|
|
|
This field is inherited by subtypes together with :attr:`tp_clear` and the |
|
|
|
:const:`Py_TPFLAGS_HAVE_GC` flag bit: the flag bit, :attr:`tp_traverse`, and |
|
|
|
:attr:`tp_clear` are all inherited from the base type if they are all zero in |
|
|
|
This field is inherited by subtypes together with :c:member:`~PyTypeObject.tp_clear` and the |
|
|
|
:const:`Py_TPFLAGS_HAVE_GC` flag bit: the flag bit, :c:member:`~PyTypeObject.tp_traverse`, and |
|
|
|
:c:member:`~PyTypeObject.tp_clear` are all inherited from the base type if they are all zero in |
|
|
|
the subtype. |
|
|
|
|
|
|
|
|
|
|
|
@ -519,17 +519,17 @@ type objects) *must* have the :attr:`ob_size` field. |
|
|
|
An optional pointer to a clear function for the garbage collector. This is only |
|
|
|
used if the :const:`Py_TPFLAGS_HAVE_GC` flag bit is set. |
|
|
|
|
|
|
|
The :attr:`tp_clear` member function is used to break reference cycles in cyclic |
|
|
|
garbage detected by the garbage collector. Taken together, all :attr:`tp_clear` |
|
|
|
The :c:member:`~PyTypeObject.tp_clear` member function is used to break reference cycles in cyclic |
|
|
|
garbage detected by the garbage collector. Taken together, all :c:member:`~PyTypeObject.tp_clear` |
|
|
|
functions in the system must combine to break all reference cycles. This is |
|
|
|
subtle, and if in any doubt supply a :attr:`tp_clear` function. For example, |
|
|
|
the tuple type does not implement a :attr:`tp_clear` function, because it's |
|
|
|
subtle, and if in any doubt supply a :c:member:`~PyTypeObject.tp_clear` function. For example, |
|
|
|
the tuple type does not implement a :c:member:`~PyTypeObject.tp_clear` function, because it's |
|
|
|
possible to prove that no reference cycle can be composed entirely of tuples. |
|
|
|
Therefore the :attr:`tp_clear` functions of other types must be sufficient to |
|
|
|
Therefore the :c:member:`~PyTypeObject.tp_clear` functions of other types must be sufficient to |
|
|
|
break any cycle containing a tuple. This isn't immediately obvious, and there's |
|
|
|
rarely a good reason to avoid implementing :attr:`tp_clear`. |
|
|
|
rarely a good reason to avoid implementing :c:member:`~PyTypeObject.tp_clear`. |
|
|
|
|
|
|
|
Implementations of :attr:`tp_clear` should drop the instance's references to |
|
|
|
Implementations of :c:member:`~PyTypeObject.tp_clear` should drop the instance's references to |
|
|
|
those of its members that may be Python objects, and set its pointers to those |
|
|
|
members to *NULL*, as in the following example:: |
|
|
|
|
|
|
|
@ -554,18 +554,18 @@ type objects) *must* have the :attr:`ob_size` field. |
|
|
|
so that *self* knows the contained object can no longer be used. The |
|
|
|
:c:func:`Py_CLEAR` macro performs the operations in a safe order. |
|
|
|
|
|
|
|
Because the goal of :attr:`tp_clear` functions is to break reference cycles, |
|
|
|
Because the goal of :c:member:`~PyTypeObject.tp_clear` functions is to break reference cycles, |
|
|
|
it's not necessary to clear contained objects like Python strings or Python |
|
|
|
integers, which can't participate in reference cycles. On the other hand, it may |
|
|
|
be convenient to clear all contained Python objects, and write the type's |
|
|
|
:attr:`tp_dealloc` function to invoke :attr:`tp_clear`. |
|
|
|
:c:member:`~PyTypeObject.tp_dealloc` function to invoke :c:member:`~PyTypeObject.tp_clear`. |
|
|
|
|
|
|
|
More information about Python's garbage collection scheme can be found in |
|
|
|
section :ref:`supporting-cycle-detection`. |
|
|
|
|
|
|
|
This field is inherited by subtypes together with :attr:`tp_traverse` and the |
|
|
|
:const:`Py_TPFLAGS_HAVE_GC` flag bit: the flag bit, :attr:`tp_traverse`, and |
|
|
|
:attr:`tp_clear` are all inherited from the base type if they are all zero in |
|
|
|
This field is inherited by subtypes together with :c:member:`~PyTypeObject.tp_traverse` and the |
|
|
|
:const:`Py_TPFLAGS_HAVE_GC` flag bit: the flag bit, :c:member:`~PyTypeObject.tp_traverse`, and |
|
|
|
:c:member:`~PyTypeObject.tp_clear` are all inherited from the base type if they are all zero in |
|
|
|
the subtype. |
|
|
|
|
|
|
|
|
|
|
|
@ -585,13 +585,13 @@ type objects) *must* have the :attr:`ob_size` field. |
|
|
|
comparisons makes sense (e.g. ``==`` and ``!=``, but not ``<`` and |
|
|
|
friends), directly raise :exc:`TypeError` in the rich comparison function. |
|
|
|
|
|
|
|
This field is inherited by subtypes together with :attr:`tp_hash`: |
|
|
|
a subtype inherits :attr:`tp_richcompare` and :attr:`tp_hash` when |
|
|
|
the subtype's :attr:`tp_richcompare` and :attr:`tp_hash` are both |
|
|
|
This field is inherited by subtypes together with :c:member:`~PyTypeObject.tp_hash`: |
|
|
|
a subtype inherits :c:member:`~PyTypeObject.tp_richcompare` and :c:member:`~PyTypeObject.tp_hash` when |
|
|
|
the subtype's :c:member:`~PyTypeObject.tp_richcompare` and :c:member:`~PyTypeObject.tp_hash` are both |
|
|
|
*NULL*. |
|
|
|
|
|
|
|
The following constants are defined to be used as the third argument for |
|
|
|
:attr:`tp_richcompare` and for :c:func:`PyObject_RichCompare`: |
|
|
|
:c:member:`~PyTypeObject.tp_richcompare` and for :c:func:`PyObject_RichCompare`: |
|
|
|
|
|
|
|
+----------------+------------+ |
|
|
|
| Constant | Comparison | |
|
|
|
@ -619,26 +619,26 @@ type objects) *must* have the :attr:`ob_size` field. |
|
|
|
instance structure needs to include a field of type :c:type:`PyObject\*` which is |
|
|
|
initialized to *NULL*. |
|
|
|
|
|
|
|
Do not confuse this field with :attr:`tp_weaklist`; that is the list head for |
|
|
|
Do not confuse this field with :c:member:`~PyTypeObject.tp_weaklist`; that is the list head for |
|
|
|
weak references to the type object itself. |
|
|
|
|
|
|
|
This field is inherited by subtypes, but see the rules listed below. A subtype |
|
|
|
may override this offset; this means that the subtype uses a different weak |
|
|
|
reference list head than the base type. Since the list head is always found via |
|
|
|
:attr:`tp_weaklistoffset`, this should not be a problem. |
|
|
|
:c:member:`~PyTypeObject.tp_weaklistoffset`, this should not be a problem. |
|
|
|
|
|
|
|
When a type defined by a class statement has no :attr:`__slots__` declaration, |
|
|
|
and none of its base types are weakly referenceable, the type is made weakly |
|
|
|
referenceable by adding a weak reference list head slot to the instance layout |
|
|
|
and setting the :attr:`tp_weaklistoffset` of that slot's offset. |
|
|
|
and setting the :c:member:`~PyTypeObject.tp_weaklistoffset` of that slot's offset. |
|
|
|
|
|
|
|
When a type's :attr:`__slots__` declaration contains a slot named |
|
|
|
:attr:`__weakref__`, that slot becomes the weak reference list head for |
|
|
|
instances of the type, and the slot's offset is stored in the type's |
|
|
|
:attr:`tp_weaklistoffset`. |
|
|
|
:c:member:`~PyTypeObject.tp_weaklistoffset`. |
|
|
|
|
|
|
|
When a type's :attr:`__slots__` declaration does not contain a slot named |
|
|
|
:attr:`__weakref__`, the type inherits its :attr:`tp_weaklistoffset` from its |
|
|
|
:attr:`__weakref__`, the type inherits its :c:member:`~PyTypeObject.tp_weaklistoffset` from its |
|
|
|
base type. |
|
|
|
|
|
|
|
.. c:member:: getiterfunc PyTypeObject.tp_iter |
|
|
|
@ -660,7 +660,7 @@ type objects) *must* have the :attr:`ob_size` field. |
|
|
|
*NULL* too. Its presence signals that the instances of this type are |
|
|
|
iterators. |
|
|
|
|
|
|
|
Iterator types should also define the :attr:`tp_iter` function, and that |
|
|
|
Iterator types should also define the :c:member:`~PyTypeObject.tp_iter` function, and that |
|
|
|
function should return the iterator instance itself (not a new iterator |
|
|
|
instance). |
|
|
|
|
|
|
|
@ -675,7 +675,7 @@ type objects) *must* have the :attr:`ob_size` field. |
|
|
|
structures, declaring regular methods of this type. |
|
|
|
|
|
|
|
For each entry in the array, an entry is added to the type's dictionary (see |
|
|
|
:attr:`tp_dict` below) containing a method descriptor. |
|
|
|
:c:member:`~PyTypeObject.tp_dict` below) containing a method descriptor. |
|
|
|
|
|
|
|
This field is not inherited by subtypes (methods are inherited through a |
|
|
|
different mechanism). |
|
|
|
@ -688,7 +688,7 @@ type objects) *must* have the :attr:`ob_size` field. |
|
|
|
this type. |
|
|
|
|
|
|
|
For each entry in the array, an entry is added to the type's dictionary (see |
|
|
|
:attr:`tp_dict` below) containing a member descriptor. |
|
|
|
:c:member:`~PyTypeObject.tp_dict` below) containing a member descriptor. |
|
|
|
|
|
|
|
This field is not inherited by subtypes (members are inherited through a |
|
|
|
different mechanism). |
|
|
|
@ -700,7 +700,7 @@ type objects) *must* have the :attr:`ob_size` field. |
|
|
|
structures, declaring computed attributes of instances of this type. |
|
|
|
|
|
|
|
For each entry in the array, an entry is added to the type's dictionary (see |
|
|
|
:attr:`tp_dict` below) containing a getset descriptor. |
|
|
|
:c:member:`~PyTypeObject.tp_dict` below) containing a getset descriptor. |
|
|
|
|
|
|
|
This field is not inherited by subtypes (computed attributes are inherited |
|
|
|
through a different mechanism). |
|
|
|
@ -748,7 +748,7 @@ type objects) *must* have the :attr:`ob_size` field. |
|
|
|
.. warning:: |
|
|
|
|
|
|
|
It is not safe to use :c:func:`PyDict_SetItem` on or otherwise modify |
|
|
|
:attr:`tp_dict` with the dictionary C-API. |
|
|
|
:c:member:`~PyTypeObject.tp_dict` with the dictionary C-API. |
|
|
|
|
|
|
|
|
|
|
|
.. c:member:: descrgetfunc PyTypeObject.tp_descr_get |
|
|
|
@ -784,7 +784,7 @@ type objects) *must* have the :attr:`ob_size` field. |
|
|
|
the instance variable dictionary; this offset is used by |
|
|
|
:c:func:`PyObject_GenericGetAttr`. |
|
|
|
|
|
|
|
Do not confuse this field with :attr:`tp_dict`; that is the dictionary for |
|
|
|
Do not confuse this field with :c:member:`~PyTypeObject.tp_dict`; that is the dictionary for |
|
|
|
attributes of the type object itself. |
|
|
|
|
|
|
|
If the value of this field is greater than zero, it specifies the offset from |
|
|
|
@ -793,20 +793,20 @@ type objects) *must* have the :attr:`ob_size` field. |
|
|
|
offset is more expensive to use, and should only be used when the instance |
|
|
|
structure contains a variable-length part. This is used for example to add an |
|
|
|
instance variable dictionary to subtypes of :class:`str` or :class:`tuple`. Note |
|
|
|
that the :attr:`tp_basicsize` field should account for the dictionary added to |
|
|
|
that the :c:member:`~PyTypeObject.tp_basicsize` field should account for the dictionary added to |
|
|
|
the end in that case, even though the dictionary is not included in the basic |
|
|
|
object layout. On a system with a pointer size of 4 bytes, |
|
|
|
:attr:`tp_dictoffset` should be set to ``-4`` to indicate that the dictionary is |
|
|
|
:c:member:`~PyTypeObject.tp_dictoffset` should be set to ``-4`` to indicate that the dictionary is |
|
|
|
at the very end of the structure. |
|
|
|
|
|
|
|
The real dictionary offset in an instance can be computed from a negative |
|
|
|
:attr:`tp_dictoffset` as follows:: |
|
|
|
:c:member:`~PyTypeObject.tp_dictoffset` as follows:: |
|
|
|
|
|
|
|
dictoffset = tp_basicsize + abs(ob_size)*tp_itemsize + tp_dictoffset |
|
|
|
if dictoffset is not aligned on sizeof(void*): |
|
|
|
round up to sizeof(void*) |
|
|
|
|
|
|
|
where :attr:`tp_basicsize`, :attr:`tp_itemsize` and :attr:`tp_dictoffset` are |
|
|
|
where :c:member:`~PyTypeObject.tp_basicsize`, :c:member:`~PyTypeObject.tp_itemsize` and :c:member:`~PyTypeObject.tp_dictoffset` are |
|
|
|
taken from the type object, and :attr:`ob_size` is taken from the instance. The |
|
|
|
absolute value is taken because ints use the sign of :attr:`ob_size` to |
|
|
|
store the sign of the number. (There's never a need to do this calculation |
|
|
|
@ -815,15 +815,15 @@ type objects) *must* have the :attr:`ob_size` field. |
|
|
|
This field is inherited by subtypes, but see the rules listed below. A subtype |
|
|
|
may override this offset; this means that the subtype instances store the |
|
|
|
dictionary at a difference offset than the base type. Since the dictionary is |
|
|
|
always found via :attr:`tp_dictoffset`, this should not be a problem. |
|
|
|
always found via :c:member:`~PyTypeObject.tp_dictoffset`, this should not be a problem. |
|
|
|
|
|
|
|
When a type defined by a class statement has no :attr:`__slots__` declaration, |
|
|
|
and none of its base types has an instance variable dictionary, a dictionary |
|
|
|
slot is added to the instance layout and the :attr:`tp_dictoffset` is set to |
|
|
|
slot is added to the instance layout and the :c:member:`~PyTypeObject.tp_dictoffset` is set to |
|
|
|
that slot's offset. |
|
|
|
|
|
|
|
When a type defined by a class statement has a :attr:`__slots__` declaration, |
|
|
|
the type inherits its :attr:`tp_dictoffset` from its base type. |
|
|
|
the type inherits its :c:member:`~PyTypeObject.tp_dictoffset` from its base type. |
|
|
|
|
|
|
|
(Adding a slot named :attr:`__dict__` to the :attr:`__slots__` declaration does |
|
|
|
not have the expected effect, it just causes confusion. Maybe this should be |
|
|
|
@ -847,12 +847,12 @@ type objects) *must* have the :attr:`ob_size` field. |
|
|
|
arguments represent positional and keyword arguments of the call to |
|
|
|
:meth:`__init__`. |
|
|
|
|
|
|
|
The :attr:`tp_init` function, if not *NULL*, is called when an instance is |
|
|
|
created normally by calling its type, after the type's :attr:`tp_new` function |
|
|
|
has returned an instance of the type. If the :attr:`tp_new` function returns an |
|
|
|
The :c:member:`~PyTypeObject.tp_init` function, if not *NULL*, is called when an instance is |
|
|
|
created normally by calling its type, after the type's :c:member:`~PyTypeObject.tp_new` function |
|
|
|
has returned an instance of the type. If the :c:member:`~PyTypeObject.tp_new` function returns an |
|
|
|
instance of some other type that is not a subtype of the original type, no |
|
|
|
:attr:`tp_init` function is called; if :attr:`tp_new` returns an instance of a |
|
|
|
subtype of the original type, the subtype's :attr:`tp_init` is called. |
|
|
|
:c:member:`~PyTypeObject.tp_init` function is called; if :c:member:`~PyTypeObject.tp_new` returns an instance of a |
|
|
|
subtype of the original type, the subtype's :c:member:`~PyTypeObject.tp_init` is called. |
|
|
|
|
|
|
|
This field is inherited by subtypes. |
|
|
|
|
|
|
|
@ -869,14 +869,14 @@ type objects) *must* have the :attr:`ob_size` field. |
|
|
|
initialization. It should return a pointer to a block of memory of adequate |
|
|
|
length for the instance, suitably aligned, and initialized to zeros, but with |
|
|
|
:attr:`ob_refcnt` set to ``1`` and :attr:`ob_type` set to the type argument. If |
|
|
|
the type's :attr:`tp_itemsize` is non-zero, the object's :attr:`ob_size` field |
|
|
|
the type's :c:member:`~PyTypeObject.tp_itemsize` is non-zero, the object's :attr:`ob_size` field |
|
|
|
should be initialized to *nitems* and the length of the allocated memory block |
|
|
|
should be ``tp_basicsize + nitems*tp_itemsize``, rounded up to a multiple of |
|
|
|
``sizeof(void*)``; otherwise, *nitems* is not used and the length of the block |
|
|
|
should be :attr:`tp_basicsize`. |
|
|
|
should be :c:member:`~PyTypeObject.tp_basicsize`. |
|
|
|
|
|
|
|
Do not use this function to do any other instance initialization, not even to |
|
|
|
allocate additional memory; that should be done by :attr:`tp_new`. |
|
|
|
allocate additional memory; that should be done by :c:member:`~PyTypeObject.tp_new`. |
|
|
|
|
|
|
|
This field is inherited by static subtypes, but not by dynamic subtypes |
|
|
|
(subtypes created by a class statement); in the latter, this field is always set |
|
|
|
@ -898,20 +898,20 @@ type objects) *must* have the :attr:`ob_size` field. |
|
|
|
|
|
|
|
The subtype argument is the type of the object being created; the *args* and |
|
|
|
*kwds* arguments represent positional and keyword arguments of the call to the |
|
|
|
type. Note that subtype doesn't have to equal the type whose :attr:`tp_new` |
|
|
|
type. Note that subtype doesn't have to equal the type whose :c:member:`~PyTypeObject.tp_new` |
|
|
|
function is called; it may be a subtype of that type (but not an unrelated |
|
|
|
type). |
|
|
|
|
|
|
|
The :attr:`tp_new` function should call ``subtype->tp_alloc(subtype, nitems)`` |
|
|
|
The :c:member:`~PyTypeObject.tp_new` function should call ``subtype->tp_alloc(subtype, nitems)`` |
|
|
|
to allocate space for the object, and then do only as much further |
|
|
|
initialization as is absolutely necessary. Initialization that can safely be |
|
|
|
ignored or repeated should be placed in the :attr:`tp_init` handler. A good |
|
|
|
ignored or repeated should be placed in the :c:member:`~PyTypeObject.tp_init` handler. A good |
|
|
|
rule of thumb is that for immutable types, all initialization should take place |
|
|
|
in :attr:`tp_new`, while for mutable types, most initialization should be |
|
|
|
deferred to :attr:`tp_init`. |
|
|
|
in :c:member:`~PyTypeObject.tp_new`, while for mutable types, most initialization should be |
|
|
|
deferred to :c:member:`~PyTypeObject.tp_init`. |
|
|
|
|
|
|
|
This field is inherited by subtypes, except it is not inherited by static types |
|
|
|
whose :attr:`tp_base` is *NULL* or ``&PyBaseObject_Type``. |
|
|
|
whose :c:member:`~PyTypeObject.tp_base` is *NULL* or ``&PyBaseObject_Type``. |
|
|
|
|
|
|
|
|
|
|
|
.. c:member:: destructor PyTypeObject.tp_free |
|
|
|
@ -935,7 +935,7 @@ type objects) *must* have the :attr:`ob_size` field. |
|
|
|
|
|
|
|
The garbage collector needs to know whether a particular object is collectible |
|
|
|
or not. Normally, it is sufficient to look at the object's type's |
|
|
|
:attr:`tp_flags` field, and check the :const:`Py_TPFLAGS_HAVE_GC` flag bit. But |
|
|
|
:c:member:`~PyTypeObject.tp_flags` field, and check the :const:`Py_TPFLAGS_HAVE_GC` flag bit. But |
|
|
|
some types have a mixture of statically and dynamically allocated instances, and |
|
|
|
the statically allocated instances are not collectible. Such types should |
|
|
|
define this function; it should return ``1`` for a collectible instance, and |
|
|
|
@ -1006,7 +1006,7 @@ subtypes. |
|
|
|
|
|
|
|
.. c:member:: PyTypeObject* PyTypeObject.tp_next |
|
|
|
|
|
|
|
Pointer to the next type object with a non-zero :attr:`tp_allocs` field. |
|
|
|
Pointer to the next type object with a non-zero :c:member:`~PyTypeObject.tp_allocs` field. |
|
|
|
|
|
|
|
Also, note that, in a garbage collected Python, tp_dealloc may be called from |
|
|
|
any Python thread, not just the thread which created the object (if the object |
|
|
|
@ -1145,13 +1145,13 @@ Sequence Object Structures |
|
|
|
|
|
|
|
This function is used by :c:func:`PySequence_Concat` and has the same |
|
|
|
signature. It is also used by the ``+`` operator, after trying the numeric |
|
|
|
addition via the :attr:`tp_as_number.nb_add` slot. |
|
|
|
addition via the :c:member:`~PyTypeObject.tp_as_number.nb_add` slot. |
|
|
|
|
|
|
|
.. c:member:: ssizeargfunc PySequenceMethods.sq_repeat |
|
|
|
|
|
|
|
This function is used by :c:func:`PySequence_Repeat` and has the same |
|
|
|
signature. It is also used by the ``*`` operator, after trying numeric |
|
|
|
multiplication via the :attr:`tp_as_number.nb_mul` slot. |
|
|
|
multiplication via the :c:member:`~PyTypeObject.tp_as_number.nb_mul` slot. |
|
|
|
|
|
|
|
.. c:member:: ssizeargfunc PySequenceMethods.sq_item |
|
|
|
|
|
|
|
|