Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 1 | .. highlightlang:: c |
| 2 | |
| 3 | .. _typeobjects: |
| 4 | |
| 5 | Type Objects |
| 6 | ------------ |
| 7 | |
| 8 | .. index:: object: type |
| 9 | |
| 10 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 11 | .. c:type:: PyTypeObject |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 12 | |
| 13 | The C structure of the objects used to describe built-in types. |
| 14 | |
| 15 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 16 | .. c:var:: PyObject* PyType_Type |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 17 | |
Georg Brandl | 2aff335 | 2010-10-17 10:59:41 +0000 | [diff] [blame] | 18 | This is the type object for type objects; it is the same object as |
| 19 | :class:`type` in the Python layer. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 20 | |
| 21 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 22 | .. c:function:: int PyType_Check(PyObject *o) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 23 | |
| 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 Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 28 | .. c:function:: int PyType_CheckExact(PyObject *o) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 29 | |
| 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 Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 34 | .. c:function:: unsigned int PyType_ClearCache() |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 35 | |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 36 | Clear the internal lookup cache. Return the current version tag. |
| 37 | |
Martin v. Löwis | 738236d | 2011-02-05 20:35:29 +0000 | [diff] [blame] | 38 | .. 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 Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 46 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 47 | .. c:function:: void PyType_Modified(PyTypeObject *type) |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 48 | |
| 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 Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 52 | |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 53 | |
Eli Bendersky | 0813168 | 2012-06-03 08:07:47 +0300 | [diff] [blame] | 54 | .. c:function:: int PyType_HasFeature(PyTypeObject *o, int feature) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 55 | |
| 56 | Return true if the type object *o* sets the feature *feature*. Type features |
| 57 | are denoted by single bit flags. |
| 58 | |
| 59 | |
Eli Bendersky | 0813168 | 2012-06-03 08:07:47 +0300 | [diff] [blame] | 60 | .. c:function:: int PyType_IS_GC(PyTypeObject *o) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 61 | |
| 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 Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 66 | .. c:function:: int PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 67 | |
| 68 | Return true if *a* is a subtype of *b*. |
| 69 | |
| 70 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 71 | .. c:function:: PyObject* PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 72 | |
Eli Bendersky | 11cfea9 | 2012-06-03 06:47:53 +0300 | [diff] [blame] | 73 | 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 Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 76 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 77 | .. c:function:: PyObject* PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 78 | |
Eli Bendersky | 11cfea9 | 2012-06-03 06:47:53 +0300 | [diff] [blame] | 79 | 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 Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 81 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 82 | .. c:function:: int PyType_Ready(PyTypeObject *type) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 83 | |
| 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öwis | 788306a | 2012-06-23 23:21:48 +0200 | [diff] [blame] | 88 | |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 89 | .. 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 |