blob: 44c731a9fa75ff678369d933d25a7e828cd2dcc3 [file] [log] [blame]
Georg Brandl54a3faa2008-01-20 09:30:57 +00001.. highlightlang:: c
2
3.. _typeobjects:
4
5Type Objects
6------------
7
8.. index:: object: type
9
10
Georg Brandl60203b42010-10-06 10:11:56 +000011.. c:type:: PyTypeObject
Georg Brandl54a3faa2008-01-20 09:30:57 +000012
13 The C structure of the objects used to describe built-in types.
14
15
Georg Brandl60203b42010-10-06 10:11:56 +000016.. c:var:: PyObject* PyType_Type
Georg Brandl54a3faa2008-01-20 09:30:57 +000017
Georg Brandl2aff3352010-10-17 10:59:41 +000018 This is the type object for type objects; it is the same object as
19 :class:`type` in the Python layer.
Georg Brandl54a3faa2008-01-20 09:30:57 +000020
21
Georg Brandl60203b42010-10-06 10:11:56 +000022.. c:function:: int PyType_Check(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +000023
24 Return true if the object *o* is a type object, including instances of types
25 derived from the standard type object. Return false in all other cases.
26
27
Georg Brandl60203b42010-10-06 10:11:56 +000028.. c:function:: int PyType_CheckExact(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +000029
30 Return true if the object *o* is a type object, but not a subtype of the
31 standard type object. Return false in all other cases.
32
33
Georg Brandl60203b42010-10-06 10:11:56 +000034.. c:function:: unsigned int PyType_ClearCache()
Christian Heimes26855632008-01-27 23:50:43 +000035
Georg Brandlf08a9dd2008-06-10 16:57:31 +000036 Clear the internal lookup cache. Return the current version tag.
37
Martin v. Löwis738236d2011-02-05 20:35:29 +000038.. c:function:: long PyType_GetFlags(PyTypeObject* type)
39
40 Return the :attr:`tp_flags` member of *type*. This function is primarily
41 meant for use with `Py_LIMITED_API`; the individual flag bits are
42 guaranteed to be stable across Python releases, but access to
43 :attr:`tp_flags` itself is not part of the limited API.
44
45 .. versionadded:: 3.2
Georg Brandlf08a9dd2008-06-10 16:57:31 +000046
Georg Brandl60203b42010-10-06 10:11:56 +000047.. c:function:: void PyType_Modified(PyTypeObject *type)
Georg Brandlf08a9dd2008-06-10 16:57:31 +000048
49 Invalidate the internal lookup cache for the type and all of its
50 subtypes. This function must be called after any manual
51 modification of the attributes or base classes of the type.
Christian Heimes26855632008-01-27 23:50:43 +000052
Christian Heimes26855632008-01-27 23:50:43 +000053
Eli Bendersky08131682012-06-03 08:07:47 +030054.. c:function:: int PyType_HasFeature(PyTypeObject *o, int feature)
Georg Brandl54a3faa2008-01-20 09:30:57 +000055
56 Return true if the type object *o* sets the feature *feature*. Type features
57 are denoted by single bit flags.
58
59
Eli Bendersky08131682012-06-03 08:07:47 +030060.. c:function:: int PyType_IS_GC(PyTypeObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +000061
62 Return true if the type object includes support for the cycle detector; this
63 tests the type flag :const:`Py_TPFLAGS_HAVE_GC`.
64
65
Georg Brandl60203b42010-10-06 10:11:56 +000066.. c:function:: int PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b)
Georg Brandl54a3faa2008-01-20 09:30:57 +000067
68 Return true if *a* is a subtype of *b*.
69
70
Georg Brandl60203b42010-10-06 10:11:56 +000071.. c:function:: PyObject* PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
Georg Brandl54a3faa2008-01-20 09:30:57 +000072
Eli Bendersky11cfea92012-06-03 06:47:53 +030073 Generic handler for the :attr:`tp_alloc` slot of a type object. Use
74 Python's default memory allocation mechanism to allocate a new instance and
75 initialize all its contents to *NULL*.
Georg Brandl54a3faa2008-01-20 09:30:57 +000076
Georg Brandl60203b42010-10-06 10:11:56 +000077.. c:function:: PyObject* PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
Georg Brandl54a3faa2008-01-20 09:30:57 +000078
Eli Bendersky11cfea92012-06-03 06:47:53 +030079 Generic handler for the :attr:`tp_new` slot of a type object. Create a
80 new instance using the type's :attr:`tp_alloc` slot.
Georg Brandl54a3faa2008-01-20 09:30:57 +000081
Georg Brandl60203b42010-10-06 10:11:56 +000082.. c:function:: int PyType_Ready(PyTypeObject *type)
Georg Brandl54a3faa2008-01-20 09:30:57 +000083
84 Finalize a type object. This should be called on all type objects to finish
85 their initialization. This function is responsible for adding inherited slots
86 from a type's base class. Return ``0`` on success, or return ``-1`` and sets an
87 exception on error.
Martin v. Löwis788306a2012-06-23 23:21:48 +020088
Martin v. Löwis9c564092012-06-23 23:20:45 +020089.. c:function:: PyObject* PyType_FromSpec(PyType_Spec *spec)
90
91 Creates and returns a heap type object from the *spec* passed to the function.
92
93.. c:function:: PyObject* PyType_FromSpecWithBases(PyType_Spec *spec, PyObject *bases)
94
95 Creates and returns a heap type object from the *spec*. In addition to that,
96 the created heap type contains all types contained by the *bases* tuple as base
97 types. This allows the caller to reference other heap types as base types.
98
99 .. versionadded:: 3.3