| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="api.css" type='text/css' /> |
| 5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> |
| 6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> |
| 7 | <link rel="first" href="api.html" title='Python/C API Reference Manual' /> |
| 8 | <link rel='contents' href='contents.html' title="Contents" /> |
| 9 | <link rel='index' href='genindex.html' title='Index' /> |
| 10 | <link rel='last' href='about.html' title='About this document...' /> |
| 11 | <link rel='help' href='about.html' title='About this document...' /> |
| 12 | <link rel="next" href="mapping-structs.html" /> |
| 13 | <link rel="prev" href="common-structs.html" /> |
| 14 | <link rel="parent" href="newTypes.html" /> |
| 15 | <link rel="next" href="mapping-structs.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>10.3 Type Objects </title> |
| 18 | </head> |
| 19 | <body> |
| 20 | <DIV CLASS="navigation"> |
| 21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 23 | <tr> |
| 24 | <td class='online-navigation'><a rel="prev" title="10.2 Common Object Structures" |
| 25 | href="common-structs.html"><img src='../icons/previous.png' |
| 26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 27 | <td class='online-navigation'><a rel="parent" title="10. Object Implementation Support" |
| 28 | href="newTypes.html"><img src='../icons/up.png' |
| 29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 30 | <td class='online-navigation'><a rel="next" title="10.4 Mapping Object Structures" |
| 31 | href="mapping-structs.html"><img src='../icons/next.png' |
| 32 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 33 | <td align="center" width="100%">Python/C API Reference Manual</td> |
| 34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 35 | href="contents.html"><img src='../icons/contents.png' |
| 36 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 37 | <td class='online-navigation'><img src='../icons/blank.png' |
| 38 | border='0' height='32' alt='' width='32' /></td> |
| 39 | <td class='online-navigation'><a rel="index" title="Index" |
| 40 | href="genindex.html"><img src='../icons/index.png' |
| 41 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 42 | </tr></table> |
| 43 | <div class='online-navigation'> |
| 44 | <b class="navlabel">Previous:</b> |
| 45 | <a class="sectref" rel="prev" href="common-structs.html">10.2 Common Object Structures</A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="newTypes.html">10. Object Implementation Support</A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="mapping-structs.html">10.4 Mapping Object Structures</A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H1><A NAME="SECTION0012300000000000000000"></A><A NAME="type-structs"></A> |
| 56 | <BR> |
| 57 | 10.3 Type Objects |
| 58 | </H1> |
| 59 | |
| 60 | <P> |
| 61 | Perhaps one of the most important structures of the Python object |
| 62 | system is the structure that defines a new type: the |
| 63 | <tt class="ctype">PyTypeObject</tt> structure. Type objects can be handled using any |
| 64 | of the <tt class="cfunction">PyObject_*()</tt> or <tt class="cfunction">PyType_*()</tt> functions, |
| 65 | but do not offer much that's interesting to most Python applications. |
| 66 | These objects are fundamental to how objects behave, so they are very |
| 67 | important to the interpreter itself and to any extension module that |
| 68 | implements new types. |
| 69 | |
| 70 | <P> |
| 71 | Type objects are fairly large compared to most of the standard types. |
| 72 | The reason for the size is that each type object stores a large number |
| 73 | of values, mostly C function pointers, each of which implements a |
| 74 | small part of the type's functionality. The fields of the type object |
| 75 | are examined in detail in this section. The fields will be described |
| 76 | in the order in which they occur in the structure. |
| 77 | |
| 78 | <P> |
| 79 | Typedefs: |
| 80 | unaryfunc, binaryfunc, ternaryfunc, inquiry, coercion, intargfunc, |
| 81 | intintargfunc, intobjargproc, intintobjargproc, objobjargproc, |
| 82 | destructor, freefunc, printfunc, getattrfunc, getattrofunc, setattrfunc, |
| 83 | setattrofunc, cmpfunc, reprfunc, hashfunc |
| 84 | |
| 85 | <P> |
| 86 | The structure definition for <tt class="ctype">PyTypeObject</tt> can be found in |
| 87 | <span class="file">Include/object.h</span>. For convenience of reference, this repeats |
| 88 | the definition found there: |
| 89 | |
| 90 | <P> |
| 91 | <div class="verbatim"> |
| 92 | <pre>typedef struct _typeobject { |
| 93 | PyObject_VAR_HEAD |
| 94 | char *tp_name; /* For printing, in format "<module>.<name>" */ |
| 95 | int tp_basicsize, tp_itemsize; /* For allocation */ |
| 96 | |
| 97 | /* Methods to implement standard operations */ |
| 98 | |
| 99 | destructor tp_dealloc; |
| 100 | printfunc tp_print; |
| 101 | getattrfunc tp_getattr; |
| 102 | setattrfunc tp_setattr; |
| 103 | cmpfunc tp_compare; |
| 104 | reprfunc tp_repr; |
| 105 | |
| 106 | /* Method suites for standard classes */ |
| 107 | |
| 108 | PyNumberMethods *tp_as_number; |
| 109 | PySequenceMethods *tp_as_sequence; |
| 110 | PyMappingMethods *tp_as_mapping; |
| 111 | |
| 112 | /* More standard operations (here for binary compatibility) */ |
| 113 | |
| 114 | hashfunc tp_hash; |
| 115 | ternaryfunc tp_call; |
| 116 | reprfunc tp_str; |
| 117 | getattrofunc tp_getattro; |
| 118 | setattrofunc tp_setattro; |
| 119 | |
| 120 | /* Functions to access object as input/output buffer */ |
| 121 | PyBufferProcs *tp_as_buffer; |
| 122 | |
| 123 | /* Flags to define presence of optional/expanded features */ |
| 124 | long tp_flags; |
| 125 | |
| 126 | char *tp_doc; /* Documentation string */ |
| 127 | |
| 128 | /* Assigned meaning in release 2.0 */ |
| 129 | /* call function for all accessible objects */ |
| 130 | traverseproc tp_traverse; |
| 131 | |
| 132 | /* delete references to contained objects */ |
| 133 | inquiry tp_clear; |
| 134 | |
| 135 | /* Assigned meaning in release 2.1 */ |
| 136 | /* rich comparisons */ |
| 137 | richcmpfunc tp_richcompare; |
| 138 | |
| 139 | /* weak reference enabler */ |
| 140 | long tp_weaklistoffset; |
| 141 | |
| 142 | /* Added in release 2.2 */ |
| 143 | /* Iterators */ |
| 144 | getiterfunc tp_iter; |
| 145 | iternextfunc tp_iternext; |
| 146 | |
| 147 | /* Attribute descriptor and subclassing stuff */ |
| 148 | struct PyMethodDef *tp_methods; |
| 149 | struct PyMemberDef *tp_members; |
| 150 | struct PyGetSetDef *tp_getset; |
| 151 | struct _typeobject *tp_base; |
| 152 | PyObject *tp_dict; |
| 153 | descrgetfunc tp_descr_get; |
| 154 | descrsetfunc tp_descr_set; |
| 155 | long tp_dictoffset; |
| 156 | initproc tp_init; |
| 157 | allocfunc tp_alloc; |
| 158 | newfunc tp_new; |
| 159 | freefunc tp_free; /* Low-level free-memory routine */ |
| 160 | inquiry tp_is_gc; /* For PyObject_IS_GC */ |
| 161 | PyObject *tp_bases; |
| 162 | PyObject *tp_mro; /* method resolution order */ |
| 163 | PyObject *tp_cache; |
| 164 | PyObject *tp_subclasses; |
| 165 | PyObject *tp_weaklist; |
| 166 | |
| 167 | } PyTypeObject; |
| 168 | </pre> |
| 169 | <div class="footer"> |
| 170 | <a href="typestruct.txt" type="text/plain">Download as text (original file name: <span class="file">typestruct.h</span>).</a> |
| 171 | </div></div> |
| 172 | |
| 173 | <P> |
| 174 | The type object structure extends the <tt class="ctype">PyVarObject</tt> structure. |
| 175 | The <tt class="member">ob_size</tt> field is used for dynamic types (created |
| 176 | by <tt class="function">type_new()</tt>, usually called from a class statement). |
| 177 | Note that <tt class="cdata">PyType_Type</tt> (the metatype) initializes |
| 178 | <tt class="member">tp_itemsize</tt>, which means that its instances (i.e. type |
| 179 | objects) <em>must</em> have the <tt class="member">ob_size</tt> field. |
| 180 | |
| 181 | <P> |
| 182 | <dl><dt>PyObject* <b><tt id='l2h-937' xml:id='l2h-937' class="cmember">_ob_next</tt></b></dt> |
| 183 | <dd> |
| 184 | <dt>PyObject* <b><tt id='l2h-1002' xml:id='l2h-1002' class="cmember">_ob_prev</tt></b></dt> |
| 185 | <dd> |
| 186 | These fields are only present when the macro <code>Py_TRACE_REFS</code> is |
| 187 | defined. Their initialization to <tt class="constant">NULL</tt> is taken care of by the |
| 188 | <code>PyObject_HEAD_INIT</code> macro. For statically allocated objects, |
| 189 | these fields always remain <tt class="constant">NULL</tt>. For dynamically allocated |
| 190 | objects, these two fields are used to link the object into a |
| 191 | doubly-linked list of <em>all</em> live objects on the heap. This |
| 192 | could be used for various debugging purposes; currently the only use |
| 193 | is to print the objects that are still alive at the end of a run |
| 194 | when the environment variable <a class="envvar" id='l2h-1003' xml:id='l2h-1003'>PYTHONDUMPREFS</a> is set. |
| 195 | |
| 196 | <P> |
| 197 | These fields are not inherited by subtypes. |
| 198 | </dl> |
| 199 | |
| 200 | <P> |
| 201 | <dl><dt>int <b><tt id='l2h-938' xml:id='l2h-938' class="cmember">ob_refcnt</tt></b></dt> |
| 202 | <dd> |
| 203 | This is the type object's reference count, initialized to <code>1</code> |
| 204 | by the <code>PyObject_HEAD_INIT</code> macro. Note that for statically |
| 205 | allocated type objects, the type's instances (objects whose |
| 206 | <tt class="member">ob_type</tt> points back to the type) do <em>not</em> count as |
| 207 | references. But for dynamically allocated type objects, the |
| 208 | instances <em>do</em> count as references. |
| 209 | |
| 210 | <P> |
| 211 | This field is not inherited by subtypes. |
| 212 | </dl> |
| 213 | |
| 214 | <P> |
| 215 | <dl><dt>PyTypeObject* <b><tt id='l2h-939' xml:id='l2h-939' class="cmember">ob_type</tt></b></dt> |
| 216 | <dd> |
| 217 | This is the type's type, in other words its metatype. It is |
| 218 | initialized by the argument to the <code>PyObject_HEAD_INIT</code> macro, |
| 219 | and its value should normally be <code>&PyType_Type</code>. However, for |
| 220 | dynamically loadable extension modules that must be usable on |
| 221 | Windows (at least), the compiler complains that this is not a valid |
| 222 | initializer. Therefore, the convention is to pass <tt class="constant">NULL</tt> to the |
| 223 | <code>PyObject_HEAD_INIT</code> macro and to initialize this field |
| 224 | explicitly at the start of the module's initialization function, |
| 225 | before doing anything else. This is typically done like this: |
| 226 | |
| 227 | <P> |
| 228 | <div class="verbatim"><pre> |
| 229 | Foo_Type.ob_type = &PyType_Type; |
| 230 | </pre></div> |
| 231 | |
| 232 | <P> |
| 233 | This should be done before any instances of the type are created. |
| 234 | <tt class="cfunction">PyType_Ready()</tt> checks if <tt class="member">ob_type</tt> is <tt class="constant">NULL</tt>, and |
| 235 | if so, initializes it: in Python 2.2, it is set to |
| 236 | <code>&PyType_Type</code>; in Python 2.2.1 and later it is |
| 237 | initialized to the <tt class="member">ob_type</tt> field of the base class. |
| 238 | <tt class="cfunction">PyType_Ready()</tt> will not change this field if it is |
| 239 | non-zero. |
| 240 | |
| 241 | <P> |
| 242 | In Python 2.2, this field is not inherited by subtypes. In 2.2.1, |
| 243 | and in 2.3 and beyond, it is inherited by subtypes. |
| 244 | </dl> |
| 245 | |
| 246 | <P> |
| 247 | <dl><dt>int <b><tt id='l2h-940' xml:id='l2h-940' class="cmember">ob_size</tt></b></dt> |
| 248 | <dd> |
| 249 | For statically allocated type objects, this should be initialized |
| 250 | to zero. For dynamically allocated type objects, this field has a |
| 251 | special internal meaning. |
| 252 | |
| 253 | <P> |
| 254 | This field is not inherited by subtypes. |
| 255 | </dl> |
| 256 | |
| 257 | <P> |
| 258 | <dl><dt>char* <b><tt id='l2h-941' xml:id='l2h-941' class="cmember">tp_name</tt></b></dt> |
| 259 | <dd> |
| 260 | Pointer to a NUL-terminated string containing the name of the type. |
| 261 | For types that are accessible as module globals, the string should |
| 262 | be the full module name, followed by a dot, followed by the type |
| 263 | name; for built-in types, it should be just the type name. If the |
| 264 | module is a submodule of a package, the full package name is part of |
| 265 | the full module name. For example, a type named <tt class="class">T</tt> defined |
| 266 | in module <tt class="module">M</tt> in subpackage <tt class="module">Q</tt> in package <tt class="module">P</tt> |
| 267 | should have the <tt class="member">tp_name</tt> initializer <code>"P.Q.M.T"</code>. |
| 268 | |
| 269 | <P> |
| 270 | For dynamically allocated type objects, this should just be the type |
| 271 | name, and the module name explicitly stored in the type dict as the |
| 272 | value for key <code>'__module__'</code>. |
| 273 | |
| 274 | <P> |
| 275 | For statically allocated type objects, the tp_name field should |
| 276 | contain a dot. Everything before the last dot is made accessible as |
| 277 | the <tt class="member">__module__</tt> attribute, and everything after the last dot |
| 278 | is made accessible as the <tt class="member">__name__</tt> attribute. |
| 279 | |
| 280 | <P> |
| 281 | If no dot is present, the entire <tt class="member">tp_name</tt> field is made |
| 282 | accessible as the <tt class="member">__name__</tt> attribute, and the |
| 283 | <tt class="member">__module__</tt> attribute is undefined (unless explicitly set in |
| 284 | the dictionary, as explained above). This means your type will be |
| 285 | impossible to pickle. |
| 286 | |
| 287 | <P> |
| 288 | This field is not inherited by subtypes. |
| 289 | </dl> |
| 290 | |
| 291 | <P> |
| 292 | <dl><dt>int <b><tt id='l2h-942' xml:id='l2h-942' class="cmember">tp_basicsize</tt></b></dt> |
| 293 | <dd> |
| 294 | <dt>int <b><tt id='l2h-1004' xml:id='l2h-1004' class="cmember">tp_itemsize</tt></b></dt> |
| 295 | <dd> |
| 296 | These fields allow calculating the size in bytes of instances of |
| 297 | the type. |
| 298 | |
| 299 | <P> |
| 300 | There are two kinds of types: types with fixed-length instances have |
| 301 | a zero <tt class="member">tp_itemsize</tt> field, types with variable-length |
| 302 | instances have a non-zero <tt class="member">tp_itemsize</tt> field. For a type |
| 303 | with fixed-length instances, all instances have the same size, |
| 304 | given in <tt class="member">tp_basicsize</tt>. |
| 305 | |
| 306 | <P> |
| 307 | For a type with variable-length instances, the instances must have |
| 308 | an <tt class="member">ob_size</tt> field, and the instance size is |
| 309 | <tt class="member">tp_basicsize</tt> plus N times <tt class="member">tp_itemsize</tt>, where N is |
| 310 | the ``length'' of the object. The value of N is typically stored in |
| 311 | the instance's <tt class="member">ob_size</tt> field. There are exceptions: for |
| 312 | example, long ints use a negative <tt class="member">ob_size</tt> to indicate a |
| 313 | negative number, and N is <code>abs(<tt class="member">ob_size</tt>)</code> there. Also, |
| 314 | the presence of an <tt class="member">ob_size</tt> field in the instance layout |
| 315 | doesn't mean that the instance structure is variable-length (for |
| 316 | example, the structure for the list type has fixed-length instances, |
| 317 | yet those instances have a meaningful <tt class="member">ob_size</tt> field). |
| 318 | |
| 319 | <P> |
| 320 | The basic size includes the fields in the instance declared by the |
| 321 | macro PyObject_HEAD or |
| 322 | PyObject_VAR_HEAD (whichever is used to declare the |
| 323 | instance struct) and this in turn includes the <tt class="member">_ob_prev</tt> and |
| 324 | <tt class="member">_ob_next</tt> fields if they are present. This means that the |
| 325 | only correct way to get an initializer for the <tt class="member">tp_basicsize</tt> |
| 326 | is to use the <tt class="keyword">sizeof</tt> operator on the struct used to |
| 327 | declare the instance layout. The basic size does not include the GC |
| 328 | header size (this is new in Python 2.2; in 2.1 and 2.0, the GC |
| 329 | header size was included in <tt class="member">tp_basicsize</tt>). |
| 330 | |
| 331 | <P> |
| 332 | These fields are inherited separately by subtypes. If the base type |
| 333 | has a non-zero <tt class="member">tp_itemsize</tt>, it is generally not safe to set |
| 334 | <tt class="member">tp_itemsize</tt> to a different non-zero value in a subtype |
| 335 | (though this depends on the implementation of the base type). |
| 336 | |
| 337 | <P> |
| 338 | A note about alignment: if the variable items require a particular |
| 339 | alignment, this should be taken care of by the value of |
| 340 | <tt class="member">tp_basicsize</tt>. Example: suppose a type implements an array |
| 341 | of <code>double</code>. <tt class="member">tp_itemsize</tt> is <code>sizeof(double)</code>. |
| 342 | It is the programmer's responsibility that <tt class="member">tp_basicsize</tt> is |
| 343 | a multiple of <code>sizeof(double)</code> (assuming this is the alignment |
| 344 | requirement for <code>double</code>). |
| 345 | </dl> |
| 346 | |
| 347 | <P> |
| 348 | <dl><dt>destructor <b><tt id='l2h-943' xml:id='l2h-943' class="cmember">tp_dealloc</tt></b></dt> |
| 349 | <dd> |
| 350 | A pointer to the instance destructor function. This function must |
| 351 | be defined unless the type guarantees that its instances will never |
| 352 | be deallocated (as is the case for the singletons <code>None</code> and |
| 353 | <code>Ellipsis</code>). |
| 354 | |
| 355 | <P> |
| 356 | The destructor function is called by the <tt class="cfunction">Py_DECREF()</tt> and |
| 357 | <tt class="cfunction">Py_XDECREF()</tt> macros when the new reference count is |
| 358 | zero. At this point, the instance is still in existence, but there |
| 359 | are no references to it. The destructor function should free all |
| 360 | references which the instance owns, free all memory buffers owned by |
| 361 | the instance (using the freeing function corresponding to the |
| 362 | allocation function used to allocate the buffer), and finally (as |
| 363 | its last action) call the type's <tt class="member">tp_free</tt> function. If the |
| 364 | type is not subtypable (doesn't have the |
| 365 | <tt class="constant">Py_TPFLAGS_BASETYPE</tt> flag bit set), it is permissible to |
| 366 | call the object deallocator directly instead of via |
| 367 | <tt class="member">tp_free</tt>. The object deallocator should be the one used to |
| 368 | allocate the instance; this is normally <tt class="cfunction">PyObject_Del()</tt> |
| 369 | if the instance was allocated using <tt class="cfunction">PyObject_New()</tt> or |
| 370 | <tt class="cfunction">PyOject_VarNew()</tt>, or <tt class="cfunction">PyObject_GC_Del()</tt> if |
| 371 | the instance was allocated using <tt class="cfunction">PyObject_GC_New()</tt> or |
| 372 | <tt class="cfunction">PyObject_GC_VarNew()</tt>. |
| 373 | |
| 374 | <P> |
| 375 | This field is inherited by subtypes. |
| 376 | </dl> |
| 377 | |
| 378 | <P> |
| 379 | <dl><dt>printfunc <b><tt id='l2h-944' xml:id='l2h-944' class="cmember">tp_print</tt></b></dt> |
| 380 | <dd> |
| 381 | An optional pointer to the instance print function. |
| 382 | |
| 383 | <P> |
| 384 | The print function is only called when the instance is printed to a |
| 385 | <em>real</em> file; when it is printed to a pseudo-file (like a |
| 386 | <tt class="class">StringIO</tt> instance), the instance's <tt class="member">tp_repr</tt> or |
| 387 | <tt class="member">tp_str</tt> function is called to convert it to a string. These |
| 388 | are also called when the type's <tt class="member">tp_print</tt> field is <tt class="constant">NULL</tt>. A |
| 389 | type should never implement <tt class="member">tp_print</tt> in a way that produces |
| 390 | different output than <tt class="member">tp_repr</tt> or <tt class="member">tp_str</tt> would. |
| 391 | |
| 392 | <P> |
| 393 | The print function is called with the same signature as |
| 394 | <tt class="cfunction">PyObject_Print()</tt>: <code>int tp_print(PyObject *self, FILE |
| 395 | *file, int flags)</code>. The <var>self</var> argument is the instance to be |
| 396 | printed. The <var>file</var> argument is the stdio file to which it is |
| 397 | to be printed. The <var>flags</var> argument is composed of flag bits. |
| 398 | The only flag bit currently defined is <tt class="constant">Py_PRINT_RAW</tt>. |
| 399 | When the <tt class="constant">Py_PRINT_RAW</tt> flag bit is set, the instance |
| 400 | should be printed the same way as <tt class="member">tp_str</tt> would format it; |
| 401 | when the <tt class="constant">Py_PRINT_RAW</tt> flag bit is clear, the instance |
| 402 | should be printed the same was as <tt class="member">tp_repr</tt> would format it. |
| 403 | It should return <code>-1</code> and set an exception condition when an |
| 404 | error occurred during the comparison. |
| 405 | |
| 406 | <P> |
| 407 | It is possible that the <tt class="member">tp_print</tt> field will be deprecated. |
| 408 | In any case, it is recommended not to define <tt class="member">tp_print</tt>, but |
| 409 | instead to rely on <tt class="member">tp_repr</tt> and <tt class="member">tp_str</tt> for |
| 410 | printing. |
| 411 | |
| 412 | <P> |
| 413 | This field is inherited by subtypes. |
| 414 | </dl> |
| 415 | |
| 416 | <P> |
| 417 | <dl><dt>getattrfunc <b><tt id='l2h-945' xml:id='l2h-945' class="cmember">tp_getattr</tt></b></dt> |
| 418 | <dd> |
| 419 | An optional pointer to the get-attribute-string function. |
| 420 | |
| 421 | <P> |
| 422 | This field is deprecated. When it is defined, it should point to a |
| 423 | function that acts the same as the <tt class="member">tp_getattro</tt> function, |
| 424 | but taking a C string instead of a Python string object to give the |
| 425 | attribute name. The signature is the same as for |
| 426 | <tt class="cfunction">PyObject_GetAttrString()</tt>. |
| 427 | |
| 428 | <P> |
| 429 | This field is inherited by subtypes together with |
| 430 | <tt class="member">tp_getattro</tt>: a subtype inherits both <tt class="member">tp_getattr</tt> |
| 431 | and <tt class="member">tp_getattro</tt> from its base type when the subtype's |
| 432 | <tt class="member">tp_getattr</tt> and <tt class="member">tp_getattro</tt> are both <tt class="constant">NULL</tt>. |
| 433 | </dl> |
| 434 | |
| 435 | <P> |
| 436 | <dl><dt>setattrfunc <b><tt id='l2h-946' xml:id='l2h-946' class="cmember">tp_setattr</tt></b></dt> |
| 437 | <dd> |
| 438 | An optional pointer to the set-attribute-string function. |
| 439 | |
| 440 | <P> |
| 441 | This field is deprecated. When it is defined, it should point to a |
| 442 | function that acts the same as the <tt class="member">tp_setattro</tt> function, |
| 443 | but taking a C string instead of a Python string object to give the |
| 444 | attribute name. The signature is the same as for |
| 445 | <tt class="cfunction">PyObject_SetAttrString()</tt>. |
| 446 | |
| 447 | <P> |
| 448 | This field is inherited by subtypes together with |
| 449 | <tt class="member">tp_setattro</tt>: a subtype inherits both <tt class="member">tp_setattr</tt> |
| 450 | and <tt class="member">tp_setattro</tt> from its base type when the subtype's |
| 451 | <tt class="member">tp_setattr</tt> and <tt class="member">tp_setattro</tt> are both <tt class="constant">NULL</tt>. |
| 452 | </dl> |
| 453 | |
| 454 | <P> |
| 455 | <dl><dt>cmpfunc <b><tt id='l2h-947' xml:id='l2h-947' class="cmember">tp_compare</tt></b></dt> |
| 456 | <dd> |
| 457 | An optional pointer to the three-way comparison function. |
| 458 | |
| 459 | <P> |
| 460 | The signature is the same as for <tt class="cfunction">PyObject_Compare()</tt>. |
| 461 | The function should return <code>1</code> if <var>self</var> greater than |
| 462 | <var>other</var>, <code>0</code> if <var>self</var> is equal to <var>other</var>, and |
| 463 | <code>-1</code> if <var>self</var> less than <var>other</var>. It should return |
| 464 | <code>-1</code> and set an exception condition when an error occurred |
| 465 | during the comparison. |
| 466 | |
| 467 | <P> |
| 468 | This field is inherited by subtypes together with |
| 469 | <tt class="member">tp_richcompare</tt> and <tt class="member">tp_hash</tt>: a subtypes inherits |
| 470 | all three of <tt class="member">tp_compare</tt>, <tt class="member">tp_richcompare</tt>, and |
| 471 | <tt class="member">tp_hash</tt> when the subtype's <tt class="member">tp_compare</tt>, |
| 472 | <tt class="member">tp_richcompare</tt>, and <tt class="member">tp_hash</tt> are all <tt class="constant">NULL</tt>. |
| 473 | </dl> |
| 474 | |
| 475 | <P> |
| 476 | <dl><dt>reprfunc <b><tt id='l2h-948' xml:id='l2h-948' class="cmember">tp_repr</tt></b></dt> |
| 477 | <dd> |
| 478 | An optional pointer to a function that implements the built-in |
| 479 | function <tt class="function">repr()</tt>.<a id='l2h-949' xml:id='l2h-949'></a> |
| 480 | <P> |
| 481 | The signature is the same as for <tt class="cfunction">PyObject_Repr()</tt>; it |
| 482 | must return a string or a Unicode object. Ideally, this function |
| 483 | should return a string that, when passed to <tt class="function">eval()</tt>, given |
| 484 | a suitable environment, returns an object with the same value. If |
| 485 | this is not feasible, it should return a string starting with |
| 486 | "<tt class="character"><</tt>" and ending with "<tt class="character">></tt>" from |
| 487 | which both the type and the value of the object can be deduced. |
| 488 | |
| 489 | <P> |
| 490 | When this field is not set, a string of the form "<tt class="samp"><%s object |
| 491 | at %p></tt>" is returned, where <code>%s</code> is replaced by the type name, |
| 492 | and <code>%p</code> by the object's memory address. |
| 493 | |
| 494 | <P> |
| 495 | This field is inherited by subtypes. |
| 496 | </dl> |
| 497 | |
| 498 | <P> |
| 499 | PyNumberMethods *tp_as_number; |
| 500 | |
| 501 | <P> |
| 502 | XXX |
| 503 | |
| 504 | <P> |
| 505 | PySequenceMethods *tp_as_sequence; |
| 506 | |
| 507 | <P> |
| 508 | XXX |
| 509 | |
| 510 | <P> |
| 511 | PyMappingMethods *tp_as_mapping; |
| 512 | |
| 513 | <P> |
| 514 | XXX |
| 515 | |
| 516 | <P> |
| 517 | <dl><dt>hashfunc <b><tt id='l2h-950' xml:id='l2h-950' class="cmember">tp_hash</tt></b></dt> |
| 518 | <dd> |
| 519 | An optional pointer to a function that implements the built-in |
| 520 | function <tt class="function">hash()</tt>.<a id='l2h-951' xml:id='l2h-951'></a> |
| 521 | <P> |
| 522 | The signature is the same as for <tt class="cfunction">PyObject_Hash()</tt>; it |
| 523 | must return a C long. The value <code>-1</code> should not be returned as |
| 524 | a normal return value; when an error occurs during the computation |
| 525 | of the hash value, the function should set an exception and return |
| 526 | <code>-1</code>. |
| 527 | |
| 528 | <P> |
| 529 | When this field is not set, two possibilities exist: if the |
| 530 | <tt class="member">tp_compare</tt> and <tt class="member">tp_richcompare</tt> fields are both |
| 531 | <tt class="constant">NULL</tt>, a default hash value based on the object's address is |
| 532 | returned; otherwise, a <tt class="exception">TypeError</tt> is raised. |
| 533 | |
| 534 | <P> |
| 535 | This field is inherited by subtypes together with |
| 536 | <tt class="member">tp_richcompare</tt> and <tt class="member">tp_compare</tt>: a subtypes inherits |
| 537 | all three of <tt class="member">tp_compare</tt>, <tt class="member">tp_richcompare</tt>, and |
| 538 | <tt class="member">tp_hash</tt>, when the subtype's <tt class="member">tp_compare</tt>, |
| 539 | <tt class="member">tp_richcompare</tt> and <tt class="member">tp_hash</tt> are all <tt class="constant">NULL</tt>. |
| 540 | </dl> |
| 541 | |
| 542 | <P> |
| 543 | <dl><dt>ternaryfunc <b><tt id='l2h-952' xml:id='l2h-952' class="cmember">tp_call</tt></b></dt> |
| 544 | <dd> |
| 545 | An optional pointer to a function that implements calling the |
| 546 | object. This should be <tt class="constant">NULL</tt> if the object is not callable. The |
| 547 | signature is the same as for <tt class="cfunction">PyObject_Call()</tt>. |
| 548 | |
| 549 | <P> |
| 550 | This field is inherited by subtypes. |
| 551 | </dl> |
| 552 | |
| 553 | <P> |
| 554 | <dl><dt>reprfunc <b><tt id='l2h-953' xml:id='l2h-953' class="cmember">tp_str</tt></b></dt> |
| 555 | <dd> |
| 556 | An optional pointer to a function that implements the built-in |
| 557 | operation <tt class="function">str()</tt>. (Note that <tt class="class">str</tt> is a type now, |
| 558 | and <tt class="function">str()</tt> calls the constructor for that type. This |
| 559 | constructor calls <tt class="cfunction">PyObject_Str()</tt> to do the actual work, |
| 560 | and <tt class="cfunction">PyObject_Str()</tt> will call this handler.) |
| 561 | |
| 562 | <P> |
| 563 | The signature is the same as for <tt class="cfunction">PyObject_Str()</tt>; it must |
| 564 | return a string or a Unicode object. This function should return a |
| 565 | ``friendly'' string representation of the object, as this is the |
| 566 | representation that will be used by the print statement. |
| 567 | |
| 568 | <P> |
| 569 | When this field is not set, <tt class="cfunction">PyObject_Repr()</tt> is called to |
| 570 | return a string representation. |
| 571 | |
| 572 | <P> |
| 573 | This field is inherited by subtypes. |
| 574 | </dl> |
| 575 | |
| 576 | <P> |
| 577 | <dl><dt>getattrofunc <b><tt id='l2h-954' xml:id='l2h-954' class="cmember">tp_getattro</tt></b></dt> |
| 578 | <dd> |
| 579 | An optional pointer to the get-attribute function. |
| 580 | |
| 581 | <P> |
| 582 | The signature is the same as for <tt class="cfunction">PyObject_GetAttr()</tt>. It |
| 583 | is usually convenient to set this field to |
| 584 | <tt class="cfunction">PyObject_GenericGetAttr()</tt>, which implements the normal |
| 585 | way of looking for object attributes. |
| 586 | |
| 587 | <P> |
| 588 | This field is inherited by subtypes together with |
| 589 | <tt class="member">tp_getattr</tt>: a subtype inherits both <tt class="member">tp_getattr</tt> and |
| 590 | <tt class="member">tp_getattro</tt> from its base type when the subtype's |
| 591 | <tt class="member">tp_getattr</tt> and <tt class="member">tp_getattro</tt> are both <tt class="constant">NULL</tt>. |
| 592 | </dl> |
| 593 | |
| 594 | <P> |
| 595 | <dl><dt>setattrofunc <b><tt id='l2h-955' xml:id='l2h-955' class="cmember">tp_setattro</tt></b></dt> |
| 596 | <dd> |
| 597 | An optional pointer to the set-attribute function. |
| 598 | |
| 599 | <P> |
| 600 | The signature is the same as for <tt class="cfunction">PyObject_SetAttr()</tt>. It |
| 601 | is usually convenient to set this field to |
| 602 | <tt class="cfunction">PyObject_GenericSetAttr()</tt>, which implements the normal |
| 603 | way of setting object attributes. |
| 604 | |
| 605 | <P> |
| 606 | This field is inherited by subtypes together with |
| 607 | <tt class="member">tp_setattr</tt>: a subtype inherits both <tt class="member">tp_setattr</tt> and |
| 608 | <tt class="member">tp_setattro</tt> from its base type when the subtype's |
| 609 | <tt class="member">tp_setattr</tt> and <tt class="member">tp_setattro</tt> are both <tt class="constant">NULL</tt>. |
| 610 | </dl> |
| 611 | |
| 612 | <P> |
| 613 | <dl><dt>PyBufferProcs* <b><tt id='l2h-956' xml:id='l2h-956' class="cmember">tp_as_buffer</tt></b></dt> |
| 614 | <dd> |
| 615 | Pointer to an additional structure that contains fields relevant only to |
| 616 | objects which implement the buffer interface. These fields are |
| 617 | documented in ``Buffer Object Structures'' (section |
| 618 | <A href="buffer-structs.html#buffer-structs">10.7</A>). |
| 619 | |
| 620 | <P> |
| 621 | The <tt class="member">tp_as_buffer</tt> field is not inherited, but the contained |
| 622 | fields are inherited individually. |
| 623 | </dl> |
| 624 | |
| 625 | <P> |
| 626 | <dl><dt>long <b><tt id='l2h-957' xml:id='l2h-957' class="cmember">tp_flags</tt></b></dt> |
| 627 | <dd> |
| 628 | This field is a bit mask of various flags. Some flags indicate |
| 629 | variant semantics for certain situations; others are used to |
| 630 | indicate that certain fields in the type object (or in the extension |
| 631 | structures referenced via <tt class="member">tp_as_number</tt>, |
| 632 | <tt class="member">tp_as_sequence</tt>, <tt class="member">tp_as_mapping</tt>, and |
| 633 | <tt class="member">tp_as_buffer</tt>) that were historically not always present are |
| 634 | valid; if such a flag bit is clear, the type fields it guards must |
| 635 | not be accessed and must be considered to have a zero or <tt class="constant">NULL</tt> |
| 636 | value instead. |
| 637 | |
| 638 | <P> |
| 639 | Inheritance of this field is complicated. Most flag bits are |
| 640 | inherited individually, i.e. if the base type has a flag bit set, |
| 641 | the subtype inherits this flag bit. The flag bits that pertain to |
| 642 | extension structures are strictly inherited if the extension |
| 643 | structure is inherited, i.e. the base type's value of the flag bit |
| 644 | is copied into the subtype together with a pointer to the extension |
| 645 | structure. The <tt class="constant">Py_TPFLAGS_HAVE_GC</tt> flag bit is inherited |
| 646 | together with the <tt class="member">tp_traverse</tt> and <tt class="member">tp_clear</tt> fields, |
| 647 | i.e. if the <tt class="constant">Py_TPFLAGS_HAVE_GC</tt> flag bit is clear in the |
| 648 | subtype and the <tt class="member">tp_traverse</tt> and <tt class="member">tp_clear</tt> fields in |
| 649 | the subtype exist (as indicated by the |
| 650 | <tt class="constant">Py_TPFLAGS_HAVE_RICHCOMPARE</tt> flag bit) and have <tt class="constant">NULL</tt> |
| 651 | values. |
| 652 | |
| 653 | <P> |
| 654 | The following bit masks are currently defined; these can be or-ed |
| 655 | together using the <code>|</code> operator to form the value of the |
| 656 | <tt class="member">tp_flags</tt> field. The macro <tt class="cfunction">PyType_HasFeature()</tt> |
| 657 | takes a type and a flags value, <var>tp</var> and <var>f</var>, and checks |
| 658 | whether <code><var>tp</var>->tp_flags & <var>f</var></code> is non-zero. |
| 659 | |
| 660 | <P> |
| 661 | <dl><dt><b><tt id='l2h-958' xml:id='l2h-958'>Py_TPFLAGS_HAVE_GETCHARBUFFER</tt></b></dt> |
| 662 | <dd> |
| 663 | If this bit is set, the <tt class="ctype">PyBufferProcs</tt> struct referenced by |
| 664 | <tt class="member">tp_as_buffer</tt> has the <tt class="member">bf_getcharbuffer</tt> field. |
| 665 | </dd></dl> |
| 666 | |
| 667 | <P> |
| 668 | <dl><dt><b><tt id='l2h-959' xml:id='l2h-959'>Py_TPFLAGS_HAVE_SEQUENCE_IN</tt></b></dt> |
| 669 | <dd> |
| 670 | If this bit is set, the <tt class="ctype">PySequenceMethods</tt> struct |
| 671 | referenced by <tt class="member">tp_as_sequence</tt> has the <tt class="member">sq_contains</tt> |
| 672 | field. |
| 673 | </dd></dl> |
| 674 | |
| 675 | <P> |
| 676 | <dl><dt><b><tt id='l2h-960' xml:id='l2h-960'>Py_TPFLAGS_GC</tt></b></dt> |
| 677 | <dd> |
| 678 | This bit is obsolete. The bit it used to name is no longer in |
| 679 | use. The symbol is now defined as zero. |
| 680 | </dd></dl> |
| 681 | |
| 682 | <P> |
| 683 | <dl><dt><b><tt id='l2h-961' xml:id='l2h-961'>Py_TPFLAGS_HAVE_INPLACEOPS</tt></b></dt> |
| 684 | <dd> |
| 685 | If this bit is set, the <tt class="ctype">PySequenceMethods</tt> struct |
| 686 | referenced by <tt class="member">tp_as_sequence</tt> and the |
| 687 | <tt class="ctype">PyNumberMethods</tt> structure referenced by |
| 688 | <tt class="member">tp_as_number</tt> contain the fields for in-place operators. |
| 689 | In particular, this means that the <tt class="ctype">PyNumberMethods</tt> |
| 690 | structure has the fields <tt class="member">nb_inplace_add</tt>, |
| 691 | <tt class="member">nb_inplace_subtract</tt>, <tt class="member">nb_inplace_multiply</tt>, |
| 692 | <tt class="member">nb_inplace_divide</tt>, <tt class="member">nb_inplace_remainder</tt>, |
| 693 | <tt class="member">nb_inplace_power</tt>, <tt class="member">nb_inplace_lshift</tt>, |
| 694 | <tt class="member">nb_inplace_rshift</tt>, <tt class="member">nb_inplace_and</tt>, |
| 695 | <tt class="member">nb_inplace_xor</tt>, and <tt class="member">nb_inplace_or</tt>; and the |
| 696 | <tt class="ctype">PySequenceMethods</tt> struct has the fields |
| 697 | <tt class="member">sq_inplace_concat</tt> and <tt class="member">sq_inplace_repeat</tt>. |
| 698 | </dd></dl> |
| 699 | |
| 700 | <P> |
| 701 | <dl><dt><b><tt id='l2h-962' xml:id='l2h-962'>Py_TPFLAGS_CHECKTYPES</tt></b></dt> |
| 702 | <dd> |
| 703 | If this bit is set, the binary and ternary operations in the |
| 704 | <tt class="ctype">PyNumberMethods</tt> structure referenced by |
| 705 | <tt class="member">tp_as_number</tt> accept arguments of arbitrary object types, |
| 706 | and do their own type conversions if needed. If this bit is |
| 707 | clear, those operations require that all arguments have the |
| 708 | current type as their type, and the caller is supposed to perform |
| 709 | a coercion operation first. This applies to <tt class="member">nb_add</tt>, |
| 710 | <tt class="member">nb_subtract</tt>, <tt class="member">nb_multiply</tt>, <tt class="member">nb_divide</tt>, |
| 711 | <tt class="member">nb_remainder</tt>, <tt class="member">nb_divmod</tt>, <tt class="member">nb_power</tt>, |
| 712 | <tt class="member">nb_lshift</tt>, <tt class="member">nb_rshift</tt>, <tt class="member">nb_and</tt>, |
| 713 | <tt class="member">nb_xor</tt>, and <tt class="member">nb_or</tt>. |
| 714 | </dd></dl> |
| 715 | |
| 716 | <P> |
| 717 | <dl><dt><b><tt id='l2h-963' xml:id='l2h-963'>Py_TPFLAGS_HAVE_RICHCOMPARE</tt></b></dt> |
| 718 | <dd> |
| 719 | If this bit is set, the type object has the |
| 720 | <tt class="member">tp_richcompare</tt> field, as well as the <tt class="member">tp_traverse</tt> |
| 721 | and the <tt class="member">tp_clear</tt> fields. |
| 722 | </dd></dl> |
| 723 | |
| 724 | <P> |
| 725 | <dl><dt><b><tt id='l2h-964' xml:id='l2h-964'>Py_TPFLAGS_HAVE_WEAKREFS</tt></b></dt> |
| 726 | <dd> |
| 727 | If this bit is set, the <tt class="member">tp_weaklistoffset</tt> field is |
| 728 | defined. Instances of a type are weakly referenceable if the |
| 729 | type's <tt class="member">tp_weaklistoffset</tt> field has a value greater than |
| 730 | zero. |
| 731 | </dd></dl> |
| 732 | |
| 733 | <P> |
| 734 | <dl><dt><b><tt id='l2h-965' xml:id='l2h-965'>Py_TPFLAGS_HAVE_ITER</tt></b></dt> |
| 735 | <dd> |
| 736 | If this bit is set, the type object has the <tt class="member">tp_iter</tt> and |
| 737 | <tt class="member">tp_iternext</tt> fields. |
| 738 | </dd></dl> |
| 739 | |
| 740 | <P> |
| 741 | <dl><dt><b><tt id='l2h-966' xml:id='l2h-966'>Py_TPFLAGS_HAVE_CLASS</tt></b></dt> |
| 742 | <dd> |
| 743 | If this bit is set, the type object has several new fields defined |
| 744 | starting in Python 2.2: <tt class="member">tp_methods</tt>, <tt class="member">tp_members</tt>, |
| 745 | <tt class="member">tp_getset</tt>, <tt class="member">tp_base</tt>, <tt class="member">tp_dict</tt>, |
| 746 | <tt class="member">tp_descr_get</tt>, <tt class="member">tp_descr_set</tt>, |
| 747 | <tt class="member">tp_dictoffset</tt>, <tt class="member">tp_init</tt>, <tt class="member">tp_alloc</tt>, |
| 748 | <tt class="member">tp_new</tt>, <tt class="member">tp_free</tt>, <tt class="member">tp_is_gc</tt>, |
| 749 | <tt class="member">tp_bases</tt>, <tt class="member">tp_mro</tt>, <tt class="member">tp_cache</tt>, |
| 750 | <tt class="member">tp_subclasses</tt>, and <tt class="member">tp_weaklist</tt>. |
| 751 | </dd></dl> |
| 752 | |
| 753 | <P> |
| 754 | <dl><dt><b><tt id='l2h-967' xml:id='l2h-967'>Py_TPFLAGS_HEAPTYPE</tt></b></dt> |
| 755 | <dd> |
| 756 | This bit is set when the type object itself is allocated on the |
| 757 | heap. In this case, the <tt class="member">ob_type</tt> field of its instances |
| 758 | is considered a reference to the type, and the type object is |
| 759 | INCREF'ed when a new instance is created, and DECREF'ed when an |
| 760 | instance is destroyed (this does not apply to instances of |
| 761 | subtypes; only the type referenced by the instance's ob_type gets |
| 762 | INCREF'ed or DECREF'ed). |
| 763 | </dd></dl> |
| 764 | |
| 765 | <P> |
| 766 | <dl><dt><b><tt id='l2h-968' xml:id='l2h-968'>Py_TPFLAGS_BASETYPE</tt></b></dt> |
| 767 | <dd> |
| 768 | This bit is set when the type can be used as the base type of |
| 769 | another type. If this bit is clear, the type cannot be subtyped |
| 770 | (similar to a "final" class in Java). |
| 771 | </dd></dl> |
| 772 | |
| 773 | <P> |
| 774 | <dl><dt><b><tt id='l2h-969' xml:id='l2h-969'>Py_TPFLAGS_READY</tt></b></dt> |
| 775 | <dd> |
| 776 | This bit is set when the type object has been fully initialized by |
| 777 | <tt class="cfunction">PyType_Ready()</tt>. |
| 778 | </dd></dl> |
| 779 | |
| 780 | <P> |
| 781 | <dl><dt><b><tt id='l2h-970' xml:id='l2h-970'>Py_TPFLAGS_READYING</tt></b></dt> |
| 782 | <dd> |
| 783 | This bit is set while <tt class="cfunction">PyType_Ready()</tt> is in the process |
| 784 | of initializing the type object. |
| 785 | </dd></dl> |
| 786 | |
| 787 | <P> |
| 788 | <dl><dt><b><tt id='l2h-971' xml:id='l2h-971'>Py_TPFLAGS_HAVE_GC</tt></b></dt> |
| 789 | <dd> |
| 790 | This bit is set when the object supports garbage collection. If |
| 791 | this bit is set, instances must be created using |
| 792 | <tt class="cfunction">PyObject_GC_New()</tt> and destroyed using |
| 793 | <tt class="cfunction">PyObject_GC_Del()</tt>. More information in section XXX |
| 794 | about garbage collection. This bit also implies that the |
| 795 | GC-related fields <tt class="member">tp_traverse</tt> and <tt class="member">tp_clear</tt> are |
| 796 | present in the type object; but those fields also exist when |
| 797 | <tt class="constant">Py_TPFLAGS_HAVE_GC</tt> is clear but |
| 798 | <tt class="constant">Py_TPFLAGS_HAVE_RICHCOMPARE</tt> is set. |
| 799 | </dd></dl> |
| 800 | |
| 801 | <P> |
| 802 | <dl><dt><b><tt id='l2h-972' xml:id='l2h-972'>Py_TPFLAGS_DEFAULT</tt></b></dt> |
| 803 | <dd> |
| 804 | This is a bitmask of all the bits that pertain to the existence of |
| 805 | certain fields in the type object and its extension structures. |
| 806 | Currently, it includes the following bits: |
| 807 | <tt class="constant">Py_TPFLAGS_HAVE_GETCHARBUFFER</tt>, |
| 808 | <tt class="constant">Py_TPFLAGS_HAVE_SEQUENCE_IN</tt>, |
| 809 | <tt class="constant">Py_TPFLAGS_HAVE_INPLACEOPS</tt>, |
| 810 | <tt class="constant">Py_TPFLAGS_HAVE_RICHCOMPARE</tt>, |
| 811 | <tt class="constant">Py_TPFLAGS_HAVE_WEAKREFS</tt>, |
| 812 | <tt class="constant">Py_TPFLAGS_HAVE_ITER</tt>, and |
| 813 | <tt class="constant">Py_TPFLAGS_HAVE_CLASS</tt>. |
| 814 | </dd></dl> |
| 815 | </dl> |
| 816 | |
| 817 | <P> |
| 818 | <dl><dt>char* <b><tt id='l2h-973' xml:id='l2h-973' class="cmember">tp_doc</tt></b></dt> |
| 819 | <dd> |
| 820 | An optional pointer to a NUL-terminated C string giving the |
| 821 | docstring for this type object. This is exposed as the |
| 822 | <tt class="member">__doc__</tt> attribute on the type and instances of the type. |
| 823 | |
| 824 | <P> |
| 825 | This field is <em>not</em> inherited by subtypes. |
| 826 | </dl> |
| 827 | |
| 828 | <P> |
| 829 | The following three fields only exist if the |
| 830 | <tt class="constant">Py_TPFLAGS_HAVE_RICHCOMPARE</tt> flag bit is set. |
| 831 | |
| 832 | <P> |
| 833 | <dl><dt>traverseproc <b><tt id='l2h-974' xml:id='l2h-974' class="cmember">tp_traverse</tt></b></dt> |
| 834 | <dd> |
| 835 | An optional pointer to a traversal function for the garbage |
| 836 | collector. This is only used if the <tt class="constant">Py_TPFLAGS_HAVE_GC</tt> |
| 837 | flag bit is set. More information in section |
| 838 | <A href="supporting-cycle-detection.html#supporting-cycle-detection">10.9</A> about garbage collection. |
| 839 | |
| 840 | <P> |
| 841 | This field is inherited by subtypes together with <tt class="member">tp_clear</tt> |
| 842 | and the <tt class="constant">Py_TPFLAGS_HAVE_GC</tt> flag bit: the flag bit, |
| 843 | <tt class="member">tp_traverse</tt>, and <tt class="member">tp_clear</tt> are all inherited from |
| 844 | the base type if they are all zero in the subtype <em>and</em> the |
| 845 | subtype has the <tt class="constant">Py_TPFLAGS_HAVE_RICHCOMPARE</tt> flag bit set. |
| 846 | </dl> |
| 847 | |
| 848 | <P> |
| 849 | <dl><dt>inquiry <b><tt id='l2h-975' xml:id='l2h-975' class="cmember">tp_clear</tt></b></dt> |
| 850 | <dd> |
| 851 | An optional pointer to a clear function for the garbage collector. |
| 852 | This is only used if the <tt class="constant">Py_TPFLAGS_HAVE_GC</tt> flag bit is |
| 853 | set. More information in section |
| 854 | <A href="supporting-cycle-detection.html#supporting-cycle-detection">10.9</A> about garbage collection. |
| 855 | |
| 856 | <P> |
| 857 | This field is inherited by subtypes together with <tt class="member">tp_clear</tt> |
| 858 | and the <tt class="constant">Py_TPFLAGS_HAVE_GC</tt> flag bit: the flag bit, |
| 859 | <tt class="member">tp_traverse</tt>, and <tt class="member">tp_clear</tt> are all inherited from |
| 860 | the base type if they are all zero in the subtype <em>and</em> the |
| 861 | subtype has the <tt class="constant">Py_TPFLAGS_HAVE_RICHCOMPARE</tt> flag bit set. |
| 862 | </dl> |
| 863 | |
| 864 | <P> |
| 865 | <dl><dt>richcmpfunc <b><tt id='l2h-976' xml:id='l2h-976' class="cmember">tp_richcompare</tt></b></dt> |
| 866 | <dd> |
| 867 | An optional pointer to the rich comparison function. |
| 868 | |
| 869 | <P> |
| 870 | The signature is the same as for <tt class="cfunction">PyObject_RichCompare()</tt>. |
| 871 | The function should return <code>1</code> if the requested comparison |
| 872 | returns true, <code>0</code> if it returns false. It should return |
| 873 | <code>-1</code> and set an exception condition when an error occurred |
| 874 | during the comparison. |
| 875 | |
| 876 | <P> |
| 877 | This field is inherited by subtypes together with |
| 878 | <tt class="member">tp_compare</tt> and <tt class="member">tp_hash</tt>: a subtype inherits all |
| 879 | three of <tt class="member">tp_compare</tt>, <tt class="member">tp_richcompare</tt>, and |
| 880 | <tt class="member">tp_hash</tt>, when the subtype's <tt class="member">tp_compare</tt>, |
| 881 | <tt class="member">tp_richcompare</tt>, and <tt class="member">tp_hash</tt> are all <tt class="constant">NULL</tt>. |
| 882 | |
| 883 | <P> |
| 884 | The following constants are defined to be used as the third argument |
| 885 | for <tt class="member">tp_richcompare</tt> and for <tt class="cfunction">PyObject_RichCompare()</tt>: |
| 886 | |
| 887 | <P> |
| 888 | <div class="center"><table class="realtable"> |
| 889 | <thead> |
| 890 | <tr> |
| 891 | <th class="left" >Constant</th> |
| 892 | <th class="center">Comparison</th> |
| 893 | </tr> |
| 894 | </thead> |
| 895 | <tbody> |
| 896 | <tr><td class="left" valign="baseline"><tt class="constant">Py_LT</tt></td> |
| 897 | <td class="center"><code><</code></td></tr> |
| 898 | <tr><td class="left" valign="baseline"><tt class="constant">Py_LE</tt></td> |
| 899 | <td class="center"><code><=</code></td></tr> |
| 900 | <tr><td class="left" valign="baseline"><tt class="constant">Py_EQ</tt></td> |
| 901 | <td class="center"><code>==</code></td></tr> |
| 902 | <tr><td class="left" valign="baseline"><tt class="constant">Py_NE</tt></td> |
| 903 | <td class="center"><code>!=</code></td></tr> |
| 904 | <tr><td class="left" valign="baseline"><tt class="constant">Py_GT</tt></td> |
| 905 | <td class="center"><code>></code></td></tr> |
| 906 | <tr><td class="left" valign="baseline"><tt class="constant">Py_GE</tt></td> |
| 907 | <td class="center"><code>>=</code></td></tr></tbody> |
| 908 | </table></div> |
| 909 | </dl> |
| 910 | |
| 911 | <P> |
| 912 | The next field only exists if the <tt class="constant">Py_TPFLAGS_HAVE_WEAKREFS</tt> |
| 913 | flag bit is set. |
| 914 | |
| 915 | <P> |
| 916 | <dl><dt>long <b><tt id='l2h-977' xml:id='l2h-977' class="cmember">tp_weaklistoffset</tt></b></dt> |
| 917 | <dd> |
| 918 | If the instances of this type are weakly referenceable, this field |
| 919 | is greater than zero and contains the offset in the instance |
| 920 | structure of the weak reference list head (ignoring the GC header, |
| 921 | if present); this offset is used by |
| 922 | <tt class="cfunction">PyObject_ClearWeakRefs()</tt> and the |
| 923 | <tt class="cfunction">PyWeakref_*()</tt> functions. The instance structure needs |
| 924 | to include a field of type <tt class="ctype">PyObject*</tt> which is initialized to |
| 925 | <tt class="constant">NULL</tt>. |
| 926 | |
| 927 | <P> |
| 928 | Do not confuse this field with <tt class="member">tp_weaklist</tt>; that is the |
| 929 | list head for weak references to the type object itself. |
| 930 | |
| 931 | <P> |
| 932 | This field is inherited by subtypes, but see the rules listed below. |
| 933 | A subtype may override this offset; this means that the subtype uses |
| 934 | a different weak reference list head than the base type. Since the |
| 935 | list head is always found via <tt class="member">tp_weaklistoffset</tt>, this |
| 936 | should not be a problem. |
| 937 | |
| 938 | <P> |
| 939 | When a type defined by a class statement has no <tt class="member">__slots__</tt> |
| 940 | declaration, and none of its base types are weakly referenceable, |
| 941 | the type is made weakly referenceable by adding a weak reference |
| 942 | list head slot to the instance layout and setting the |
| 943 | <tt class="member">tp_weaklistoffset</tt> of that slot's offset. |
| 944 | |
| 945 | <P> |
| 946 | When a type's <tt class="member">__slots__</tt> declaration contains a slot named |
| 947 | <tt class="member">__weakref__</tt>, that slot becomes the weak reference list head |
| 948 | for instances of the type, and the slot's offset is stored in the |
| 949 | type's <tt class="member">tp_weaklistoffset</tt>. |
| 950 | |
| 951 | <P> |
| 952 | When a type's <tt class="member">__slots__</tt> declaration does not contain a slot |
| 953 | named <tt class="member">__weakref__</tt>, the type inherits its |
| 954 | <tt class="member">tp_weaklistoffset</tt> from its base type. |
| 955 | </dl> |
| 956 | |
| 957 | <P> |
| 958 | The next two fields only exist if the |
| 959 | <tt class="constant">Py_TPFLAGS_HAVE_CLASS</tt> flag bit is set. |
| 960 | |
| 961 | <P> |
| 962 | <dl><dt>getiterfunc <b><tt id='l2h-978' xml:id='l2h-978' class="cmember">tp_iter</tt></b></dt> |
| 963 | <dd> |
| 964 | An optional pointer to a function that returns an iterator for the |
| 965 | object. Its presence normally signals that the instances of this |
| 966 | type are iterable (although sequences may be iterable without this |
| 967 | function, and classic instances always have this function, even if |
| 968 | they don't define an <tt class="method">__iter__()</tt> method). |
| 969 | |
| 970 | <P> |
| 971 | This function has the same signature as |
| 972 | <tt class="cfunction">PyObject_GetIter()</tt>. |
| 973 | |
| 974 | <P> |
| 975 | This field is inherited by subtypes. |
| 976 | </dl> |
| 977 | |
| 978 | <P> |
| 979 | <dl><dt>iternextfunc <b><tt id='l2h-979' xml:id='l2h-979' class="cmember">tp_iternext</tt></b></dt> |
| 980 | <dd> |
| 981 | An optional pointer to a function that returns the next item in an |
| 982 | iterator, or raises <tt class="exception">StopIteration</tt> when the iterator is |
| 983 | exhausted. Its presence normally signals that the instances of this |
| 984 | type are iterators (although classic instances always have this |
| 985 | function, even if they don't define a <tt class="method">next()</tt> method). |
| 986 | |
| 987 | <P> |
| 988 | Iterator types should also define the <tt class="member">tp_iter</tt> function, and |
| 989 | that function should return the iterator instance itself (not a new |
| 990 | iterator instance). |
| 991 | |
| 992 | <P> |
| 993 | This function has the same signature as <tt class="cfunction">PyIter_Next()</tt>. |
| 994 | |
| 995 | <P> |
| 996 | This field is inherited by subtypes. |
| 997 | </dl> |
| 998 | |
| 999 | <P> |
| 1000 | The next fields, up to and including <tt class="member">tp_weaklist</tt>, only exist |
| 1001 | if the <tt class="constant">Py_TPFLAGS_HAVE_CLASS</tt> flag bit is set. |
| 1002 | |
| 1003 | <P> |
| 1004 | <dl><dt>struct PyMethodDef* <b><tt id='l2h-980' xml:id='l2h-980' class="cmember">tp_methods</tt></b></dt> |
| 1005 | <dd> |
| 1006 | An optional pointer to a static <tt class="constant">NULL</tt>-terminated array of |
| 1007 | <tt class="ctype">PyMethodDef</tt> structures, declaring regular methods of this |
| 1008 | type. |
| 1009 | |
| 1010 | <P> |
| 1011 | For each entry in the array, an entry is added to the type's |
| 1012 | dictionary (see <tt class="member">tp_dict</tt> below) containing a method |
| 1013 | descriptor. |
| 1014 | |
| 1015 | <P> |
| 1016 | This field is not inherited by subtypes (methods are |
| 1017 | inherited through a different mechanism). |
| 1018 | </dl> |
| 1019 | |
| 1020 | <P> |
| 1021 | <dl><dt>struct PyMemberDef* <b><tt id='l2h-981' xml:id='l2h-981' class="cmember">tp_members</tt></b></dt> |
| 1022 | <dd> |
| 1023 | An optional pointer to a static <tt class="constant">NULL</tt>-terminated array of |
| 1024 | <tt class="ctype">PyMemberDef</tt> structures, declaring regular data members |
| 1025 | (fields or slots) of instances of this type. |
| 1026 | |
| 1027 | <P> |
| 1028 | For each entry in the array, an entry is added to the type's |
| 1029 | dictionary (see <tt class="member">tp_dict</tt> below) containing a member |
| 1030 | descriptor. |
| 1031 | |
| 1032 | <P> |
| 1033 | This field is not inherited by subtypes (members are inherited |
| 1034 | through a different mechanism). |
| 1035 | </dl> |
| 1036 | |
| 1037 | <P> |
| 1038 | <dl><dt>struct PyGetSetDef* <b><tt id='l2h-982' xml:id='l2h-982' class="cmember">tp_getset</tt></b></dt> |
| 1039 | <dd> |
| 1040 | An optional pointer to a static <tt class="constant">NULL</tt>-terminated array of |
| 1041 | <tt class="ctype">PyGetSetDef</tt> structures, declaring computed attributes of |
| 1042 | instances of this type. |
| 1043 | |
| 1044 | <P> |
| 1045 | For each entry in the array, an entry is added to the type's |
| 1046 | dictionary (see <tt class="member">tp_dict</tt> below) containing a getset |
| 1047 | descriptor. |
| 1048 | |
| 1049 | <P> |
| 1050 | This field is not inherited by subtypes (computed attributes are |
| 1051 | inherited through a different mechanism). |
| 1052 | |
| 1053 | <P> |
| 1054 | Docs for PyGetSetDef (XXX belong elsewhere): |
| 1055 | |
| 1056 | <P> |
| 1057 | <div class="verbatim"><pre> |
| 1058 | typedef PyObject *(*getter)(PyObject *, void *); |
| 1059 | typedef int (*setter)(PyObject *, PyObject *, void *); |
| 1060 | |
| 1061 | typedef struct PyGetSetDef { |
| 1062 | char *name; /* attribute name */ |
| 1063 | getter get; /* C function to get the attribute */ |
| 1064 | setter set; /* C function to set the attribute */ |
| 1065 | char *doc; /* optional doc string */ |
| 1066 | void *closure; /* optional additional data for getter and setter */ |
| 1067 | } PyGetSetDef; |
| 1068 | </pre></div> |
| 1069 | </dl> |
| 1070 | |
| 1071 | <P> |
| 1072 | <dl><dt>PyTypeObject* <b><tt id='l2h-983' xml:id='l2h-983' class="cmember">tp_base</tt></b></dt> |
| 1073 | <dd> |
| 1074 | An optional pointer to a base type from which type properties are |
| 1075 | inherited. At this level, only single inheritance is supported; |
| 1076 | multiple inheritance require dynamically creating a type object by |
| 1077 | calling the metatype. |
| 1078 | |
| 1079 | <P> |
| 1080 | This field is not inherited by subtypes (obviously), but it defaults |
| 1081 | to <code>&PyBaseObject_Type</code> (which to Python programmers is known |
| 1082 | as the type <tt class="class">object</tt>). |
| 1083 | </dl> |
| 1084 | |
| 1085 | <P> |
| 1086 | <dl><dt>PyObject* <b><tt id='l2h-984' xml:id='l2h-984' class="cmember">tp_dict</tt></b></dt> |
| 1087 | <dd> |
| 1088 | The type's dictionary is stored here by <tt class="cfunction">PyType_Ready()</tt>. |
| 1089 | |
| 1090 | <P> |
| 1091 | This field should normally be initialized to <tt class="constant">NULL</tt> before |
| 1092 | PyType_Ready is called; it may also be initialized to a dictionary |
| 1093 | containing initial attributes for the type. Once |
| 1094 | <tt class="cfunction">PyType_Ready()</tt> has initialized the type, extra |
| 1095 | attributes for the type may be added to this dictionary only if they |
| 1096 | don't correspond to overloaded operations (like <tt class="method">__add__()</tt>). |
| 1097 | |
| 1098 | <P> |
| 1099 | This field is not inherited by subtypes (though the attributes |
| 1100 | defined in here are inherited through a different mechanism). |
| 1101 | </dl> |
| 1102 | |
| 1103 | <P> |
| 1104 | <dl><dt>descrgetfunc <b><tt id='l2h-985' xml:id='l2h-985' class="cmember">tp_descr_get</tt></b></dt> |
| 1105 | <dd> |
| 1106 | An optional pointer to a "descriptor get" function. |
| 1107 | |
| 1108 | <P> |
| 1109 | XXX blah, blah. |
| 1110 | |
| 1111 | <P> |
| 1112 | This field is inherited by subtypes. |
| 1113 | </dl> |
| 1114 | |
| 1115 | <P> |
| 1116 | <dl><dt>descrsetfunc <b><tt id='l2h-986' xml:id='l2h-986' class="cmember">tp_descr_set</tt></b></dt> |
| 1117 | <dd> |
| 1118 | An optional pointer to a "descriptor set" function. |
| 1119 | |
| 1120 | <P> |
| 1121 | XXX blah, blah. |
| 1122 | |
| 1123 | <P> |
| 1124 | This field is inherited by subtypes. |
| 1125 | </dl> |
| 1126 | |
| 1127 | <P> |
| 1128 | <dl><dt>long <b><tt id='l2h-987' xml:id='l2h-987' class="cmember">tp_dictoffset</tt></b></dt> |
| 1129 | <dd> |
| 1130 | If the instances of this type have a dictionary containing instance |
| 1131 | variables, this field is non-zero and contains the offset in the |
| 1132 | instances of the type of the instance variable dictionary; this |
| 1133 | offset is used by <tt class="cfunction">PyObject_GenericGetAttr()</tt>. |
| 1134 | |
| 1135 | <P> |
| 1136 | Do not confuse this field with <tt class="member">tp_dict</tt>; that is the |
| 1137 | dictionary for attributes of the type object itself. |
| 1138 | |
| 1139 | <P> |
| 1140 | If the value of this field is greater than zero, it specifies the |
| 1141 | offset from the start of the instance structure. If the value is |
| 1142 | less than zero, it specifies the offset from the <em>end</em> of the |
| 1143 | instance structure. A negative offset is more expensive to use, and |
| 1144 | should only be used when the instance structure contains a |
| 1145 | variable-length part. This is used for example to add an instance |
| 1146 | variable dictionary to subtypes of <tt class="class">str</tt> or <tt class="class">tuple</tt>. |
| 1147 | Note that the <tt class="member">tp_basicsize</tt> field should account for the |
| 1148 | dictionary added to the end in that case, even though the dictionary |
| 1149 | is not included in the basic object layout. On a system with a |
| 1150 | pointer size of 4 bytes, <tt class="member">tp_dictoffset</tt> should be set to |
| 1151 | <code>-4</code> to indicate that the dictionary is at the very end of the |
| 1152 | structure. |
| 1153 | |
| 1154 | <P> |
| 1155 | The real dictionary offset in an instance can be computed from a |
| 1156 | negative <tt class="member">tp_dictoffset</tt> as follows: |
| 1157 | |
| 1158 | <P> |
| 1159 | <div class="verbatim"><pre> |
| 1160 | dictoffset = tp_basicsize + abs(ob_size)*tp_itemsize + tp_dictoffset |
| 1161 | if dictoffset is not aligned on sizeof(void*): |
| 1162 | round up to sizeof(void*) |
| 1163 | </pre></div> |
| 1164 | |
| 1165 | <P> |
| 1166 | where <tt class="member">tp_basicsize</tt>, <tt class="member">tp_itemsize</tt> and |
| 1167 | <tt class="member">tp_dictoffset</tt> are taken from the type object, and |
| 1168 | <tt class="member">ob_size</tt> is taken from the instance. The absolute value is |
| 1169 | taken because long ints use the sign of <tt class="member">ob_size</tt> to store |
| 1170 | the sign of the number. (There's never a need to do this |
| 1171 | calculation yourself; it is done for you by |
| 1172 | <tt class="cfunction">_PyObject_GetDictPtr()</tt>.) |
| 1173 | |
| 1174 | <P> |
| 1175 | This field is inherited by subtypes, but see the rules listed below. |
| 1176 | A subtype may override this offset; this means that the subtype |
| 1177 | instances store the dictionary at a difference offset than the base |
| 1178 | type. Since the dictionary is always found via |
| 1179 | <tt class="member">tp_dictoffset</tt>, this should not be a problem. |
| 1180 | |
| 1181 | <P> |
| 1182 | When a type defined by a class statement has no <tt class="member">__slots__</tt> |
| 1183 | declaration, and none of its base types has an instance variable |
| 1184 | dictionary, a dictionary slot is added to the instance layout and |
| 1185 | the <tt class="member">tp_dictoffset</tt> is set to that slot's offset. |
| 1186 | |
| 1187 | <P> |
| 1188 | When a type defined by a class statement has a <tt class="member">__slots__</tt> |
| 1189 | declaration, the type inherits its <tt class="member">tp_dictoffset</tt> from its |
| 1190 | base type. |
| 1191 | |
| 1192 | <P> |
| 1193 | (Adding a slot named <tt class="member">__dict__</tt> to the <tt class="member">__slots__</tt> |
| 1194 | declaration does not have the expected effect, it just causes |
| 1195 | confusion. Maybe this should be added as a feature just like |
| 1196 | <tt class="member">__weakref__</tt> though.) |
| 1197 | </dl> |
| 1198 | |
| 1199 | <P> |
| 1200 | <dl><dt>initproc <b><tt id='l2h-988' xml:id='l2h-988' class="cmember">tp_init</tt></b></dt> |
| 1201 | <dd> |
| 1202 | An optional pointer to an instance initialization function. |
| 1203 | |
| 1204 | <P> |
| 1205 | This function corresponds to the <tt class="method">__init__()</tt> method of |
| 1206 | classes. Like <tt class="method">__init__()</tt>, it is possible to create an |
| 1207 | instance without calling <tt class="method">__init__()</tt>, and it is possible to |
| 1208 | reinitialize an instance by calling its <tt class="method">__init__()</tt> method |
| 1209 | again. |
| 1210 | |
| 1211 | <P> |
| 1212 | The function signature is |
| 1213 | |
| 1214 | <P> |
| 1215 | <div class="verbatim"><pre> |
| 1216 | int tp_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 1217 | </pre></div> |
| 1218 | |
| 1219 | <P> |
| 1220 | The self argument is the instance to be initialized; the <var>args</var> |
| 1221 | and <var>kwds</var> arguments represent positional and keyword arguments |
| 1222 | of the call to <tt class="method">__init__()</tt>. |
| 1223 | |
| 1224 | <P> |
| 1225 | The <tt class="member">tp_init</tt> function, if not <tt class="constant">NULL</tt>, is called when an |
| 1226 | instance is created normally by calling its type, after the type's |
| 1227 | <tt class="member">tp_new</tt> function has returned an instance of the type. If |
| 1228 | the <tt class="member">tp_new</tt> function returns an instance of some other type |
| 1229 | that is not a subtype of the original type, no <tt class="member">tp_init</tt> |
| 1230 | function is called; if <tt class="member">tp_new</tt> returns an instance of a |
| 1231 | subtype of the original type, the subtype's <tt class="member">tp_init</tt> is |
| 1232 | called. (VERSION NOTE: described here is what is implemented in |
| 1233 | Python 2.2.1 and later. In Python 2.2, the <tt class="member">tp_init</tt> of the |
| 1234 | type of the object returned by <tt class="member">tp_new</tt> was always called, if |
| 1235 | not <tt class="constant">NULL</tt>.) |
| 1236 | |
| 1237 | <P> |
| 1238 | This field is inherited by subtypes. |
| 1239 | </dl> |
| 1240 | |
| 1241 | <P> |
| 1242 | <dl><dt>allocfunc <b><tt id='l2h-989' xml:id='l2h-989' class="cmember">tp_alloc</tt></b></dt> |
| 1243 | <dd> |
| 1244 | An optional pointer to an instance allocation function. |
| 1245 | |
| 1246 | <P> |
| 1247 | The function signature is |
| 1248 | |
| 1249 | <P> |
| 1250 | <div class="verbatim"><pre> |
| 1251 | PyObject *tp_alloc(PyTypeObject *self, int nitems) |
| 1252 | </pre></div> |
| 1253 | |
| 1254 | <P> |
| 1255 | The purpose of this function is to separate memory allocation from |
| 1256 | memory initialization. It should return a pointer to a block of |
| 1257 | memory of adequate length for the instance, suitably aligned, and |
| 1258 | initialized to zeros, but with <tt class="member">ob_refcnt</tt> set to <code>1</code> |
| 1259 | and <tt class="member">ob_type</tt> set to the type argument. If the type's |
| 1260 | <tt class="member">tp_itemsize</tt> is non-zero, the object's <tt class="member">ob_size</tt> field |
| 1261 | should be initialized to <var>nitems</var> and the length of the |
| 1262 | allocated memory block should be <code>tp_basicsize + |
| 1263 | <var>nitems</var>*tp_itemsize</code>, rounded up to a multiple of |
| 1264 | <code>sizeof(void*)</code>; otherwise, <var>nitems</var> is not used and the |
| 1265 | length of the block should be <tt class="member">tp_basicsize</tt>. |
| 1266 | |
| 1267 | <P> |
| 1268 | Do not use this function to do any other instance initialization, |
| 1269 | not even to allocate additional memory; that should be done by |
| 1270 | <tt class="member">tp_new</tt>. |
| 1271 | |
| 1272 | <P> |
| 1273 | This field is inherited by static subtypes, but not by dynamic |
| 1274 | subtypes (subtypes created by a class statement); in the latter, |
| 1275 | this field is always set to <tt class="cfunction">PyType_GenericAlloc()</tt>, to |
| 1276 | force a standard heap allocation strategy. That is also the |
| 1277 | recommended value for statically defined types. |
| 1278 | </dl> |
| 1279 | |
| 1280 | <P> |
| 1281 | <dl><dt>newfunc <b><tt id='l2h-990' xml:id='l2h-990' class="cmember">tp_new</tt></b></dt> |
| 1282 | <dd> |
| 1283 | An optional pointer to an instance creation function. |
| 1284 | |
| 1285 | <P> |
| 1286 | If this function is <tt class="constant">NULL</tt> for a particular type, that type cannot |
| 1287 | be called to create new instances; presumably there is some other |
| 1288 | way to create instances, like a factory function. |
| 1289 | |
| 1290 | <P> |
| 1291 | The function signature is |
| 1292 | |
| 1293 | <P> |
| 1294 | <div class="verbatim"><pre> |
| 1295 | PyObject *tp_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds) |
| 1296 | </pre></div> |
| 1297 | |
| 1298 | <P> |
| 1299 | The subtype argument is the type of the object being created; the |
| 1300 | <var>args</var> and <var>kwds</var> arguments represent positional and keyword |
| 1301 | arguments of the call to the type. Note that subtype doesn't have |
| 1302 | to equal the type whose <tt class="member">tp_new</tt> function is called; it may |
| 1303 | be a subtype of that type (but not an unrelated type). |
| 1304 | |
| 1305 | <P> |
| 1306 | The <tt class="member">tp_new</tt> function should call |
| 1307 | <code><var>subtype</var>->tp_alloc(<var>subtype</var>, <var>nitems</var>)</code> to |
| 1308 | allocate space for the object, and then do only as much further |
| 1309 | initialization as is absolutely necessary. Initialization that can |
| 1310 | safely be ignored or repeated should be placed in the |
| 1311 | <tt class="member">tp_init</tt> handler. A good rule of thumb is that for |
| 1312 | immutable types, all initialization should take place in |
| 1313 | <tt class="member">tp_new</tt>, while for mutable types, most initialization should |
| 1314 | be deferred to <tt class="member">tp_init</tt>. |
| 1315 | |
| 1316 | <P> |
| 1317 | This field is inherited by subtypes, except it is not inherited by |
| 1318 | static types whose <tt class="member">tp_base</tt> is <tt class="constant">NULL</tt> or |
| 1319 | <code>&PyBaseObject_Type</code>. The latter exception is a precaution so |
| 1320 | that old extension types don't become callable simply by being |
| 1321 | linked with Python 2.2. |
| 1322 | </dl> |
| 1323 | |
| 1324 | <P> |
| 1325 | <dl><dt>destructor <b><tt id='l2h-991' xml:id='l2h-991' class="cmember">tp_free</tt></b></dt> |
| 1326 | <dd> |
| 1327 | An optional pointer to an instance deallocation function. |
| 1328 | |
| 1329 | <P> |
| 1330 | The signature of this function has changed slightly: in Python |
| 1331 | 2.2 and 2.2.1, its signature is <tt class="ctype">destructor</tt>: |
| 1332 | |
| 1333 | <P> |
| 1334 | <div class="verbatim"><pre> |
| 1335 | void tp_free(PyObject *) |
| 1336 | </pre></div> |
| 1337 | |
| 1338 | <P> |
| 1339 | In Python 2.3 and beyond, its signature is <tt class="ctype">freefunc</tt>: |
| 1340 | |
| 1341 | <P> |
| 1342 | <div class="verbatim"><pre> |
| 1343 | void tp_free(void *) |
| 1344 | </pre></div> |
| 1345 | |
| 1346 | <P> |
| 1347 | The only initializer that is compatible with both versions is |
| 1348 | <code>_PyObject_Del</code>, whose definition has suitably adapted in |
| 1349 | Python 2.3. |
| 1350 | |
| 1351 | <P> |
| 1352 | This field is inherited by static subtypes, but not by dynamic |
| 1353 | subtypes (subtypes created by a class statement); in the latter, |
| 1354 | this field is set to a deallocator suitable to match |
| 1355 | <tt class="cfunction">PyType_GenericAlloc()</tt> and the value of the |
| 1356 | <tt class="constant">Py_TPFLAGS_HAVE_GC</tt> flag bit. |
| 1357 | </dl> |
| 1358 | |
| 1359 | <P> |
| 1360 | <dl><dt>inquiry <b><tt id='l2h-992' xml:id='l2h-992' class="cmember">tp_is_gc</tt></b></dt> |
| 1361 | <dd> |
| 1362 | An optional pointer to a function called by the garbage collector. |
| 1363 | |
| 1364 | <P> |
| 1365 | The garbage collector needs to know whether a particular object is |
| 1366 | collectible or not. Normally, it is sufficient to look at the |
| 1367 | object's type's <tt class="member">tp_flags</tt> field, and check the |
| 1368 | <tt class="constant">Py_TPFLAGS_HAVE_GC</tt> flag bit. But some types have a |
| 1369 | mixture of statically and dynamically allocated instances, and the |
| 1370 | statically allocated instances are not collectible. Such types |
| 1371 | should define this function; it should return <code>1</code> for a |
| 1372 | collectible instance, and <code>0</code> for a non-collectible instance. |
| 1373 | The signature is |
| 1374 | |
| 1375 | <P> |
| 1376 | <div class="verbatim"><pre> |
| 1377 | int tp_is_gc(PyObject *self) |
| 1378 | </pre></div> |
| 1379 | |
| 1380 | <P> |
| 1381 | (The only example of this are types themselves. The metatype, |
| 1382 | <tt class="cdata">PyType_Type</tt>, defines this function to distinguish between |
| 1383 | statically and dynamically allocated types.) |
| 1384 | |
| 1385 | <P> |
| 1386 | This field is inherited by subtypes. (VERSION NOTE: in Python |
| 1387 | 2.2, it was not inherited. It is inherited in 2.2.1 and later |
| 1388 | versions.) |
| 1389 | </dl> |
| 1390 | |
| 1391 | <P> |
| 1392 | <dl><dt>PyObject* <b><tt id='l2h-993' xml:id='l2h-993' class="cmember">tp_bases</tt></b></dt> |
| 1393 | <dd> |
| 1394 | Tuple of base types. |
| 1395 | |
| 1396 | <P> |
| 1397 | This is set for types created by a class statement. It should be |
| 1398 | <tt class="constant">NULL</tt> for statically defined types. |
| 1399 | |
| 1400 | <P> |
| 1401 | This field is not inherited. |
| 1402 | </dl> |
| 1403 | |
| 1404 | <P> |
| 1405 | <dl><dt>PyObject* <b><tt id='l2h-994' xml:id='l2h-994' class="cmember">tp_mro</tt></b></dt> |
| 1406 | <dd> |
| 1407 | Tuple containing the expanded set of base types, starting with the |
| 1408 | type itself and ending with <tt class="class">object</tt>, in Method Resolution |
| 1409 | Order. |
| 1410 | |
| 1411 | <P> |
| 1412 | This field is not inherited; it is calculated fresh by |
| 1413 | <tt class="cfunction">PyType_Ready()</tt>. |
| 1414 | </dl> |
| 1415 | |
| 1416 | <P> |
| 1417 | <dl><dt>PyObject* <b><tt id='l2h-995' xml:id='l2h-995' class="cmember">tp_cache</tt></b></dt> |
| 1418 | <dd> |
| 1419 | Unused. Not inherited. Internal use only. |
| 1420 | </dl> |
| 1421 | |
| 1422 | <P> |
| 1423 | <dl><dt>PyObject* <b><tt id='l2h-996' xml:id='l2h-996' class="cmember">tp_subclasses</tt></b></dt> |
| 1424 | <dd> |
| 1425 | List of weak references to subclasses. Not inherited. Internal |
| 1426 | use only. |
| 1427 | </dl> |
| 1428 | |
| 1429 | <P> |
| 1430 | <dl><dt>PyObject* <b><tt id='l2h-997' xml:id='l2h-997' class="cmember">tp_weaklist</tt></b></dt> |
| 1431 | <dd> |
| 1432 | Weak reference list head, for weak references to this type |
| 1433 | object. Not inherited. Internal use only. |
| 1434 | </dl> |
| 1435 | |
| 1436 | <P> |
| 1437 | The remaining fields are only defined if the feature test macro |
| 1438 | <tt class="constant">COUNT_ALLOCS</tt> is defined, and are for internal use only. |
| 1439 | They are documented here for completeness. None of these fields are |
| 1440 | inherited by subtypes. |
| 1441 | |
| 1442 | <P> |
| 1443 | <dl><dt>int <b><tt id='l2h-998' xml:id='l2h-998' class="cmember">tp_allocs</tt></b></dt> |
| 1444 | <dd> |
| 1445 | Number of allocations. |
| 1446 | </dl> |
| 1447 | |
| 1448 | <P> |
| 1449 | <dl><dt>int <b><tt id='l2h-999' xml:id='l2h-999' class="cmember">tp_frees</tt></b></dt> |
| 1450 | <dd> |
| 1451 | Number of frees. |
| 1452 | </dl> |
| 1453 | |
| 1454 | <P> |
| 1455 | <dl><dt>int <b><tt id='l2h-1000' xml:id='l2h-1000' class="cmember">tp_maxalloc</tt></b></dt> |
| 1456 | <dd> |
| 1457 | Maximum simultaneously allocated objects. |
| 1458 | </dl> |
| 1459 | |
| 1460 | <P> |
| 1461 | <dl><dt>PyTypeObject* <b><tt id='l2h-1001' xml:id='l2h-1001' class="cmember">tp_next</tt></b></dt> |
| 1462 | <dd> |
| 1463 | Pointer to the next type object with a non-zero <tt class="member">tp_allocs</tt> |
| 1464 | field. |
| 1465 | </dl> |
| 1466 | |
| 1467 | <P> |
| 1468 | Also, note that, in a garbage collected Python, tp_dealloc may be |
| 1469 | called from any Python thread, not just the thread which created the |
| 1470 | object (if the object becomes part of a refcount cycle, that cycle |
| 1471 | might be collected by a garbage collection on any thread). This is |
| 1472 | not a problem for Python API calls, since the thread on which |
| 1473 | tp_dealloc is called will own the Global Interpreter Lock (GIL). |
| 1474 | However, if the object being destroyed in turn destroys objects from |
| 1475 | some other C or C++ library, care should be taken to ensure that |
| 1476 | destroying those objects on the thread which called tp_dealloc will |
| 1477 | not violate any assumptions of the library. |
| 1478 | |
| 1479 | <P> |
| 1480 | |
| 1481 | <DIV CLASS="navigation"> |
| 1482 | <div class='online-navigation'> |
| 1483 | <p></p><hr /> |
| 1484 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 1485 | <tr> |
| 1486 | <td class='online-navigation'><a rel="prev" title="10.2 Common Object Structures" |
| 1487 | href="common-structs.html"><img src='../icons/previous.png' |
| 1488 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 1489 | <td class='online-navigation'><a rel="parent" title="10. Object Implementation Support" |
| 1490 | href="newTypes.html"><img src='../icons/up.png' |
| 1491 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 1492 | <td class='online-navigation'><a rel="next" title="10.4 Mapping Object Structures" |
| 1493 | href="mapping-structs.html"><img src='../icons/next.png' |
| 1494 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 1495 | <td align="center" width="100%">Python/C API Reference Manual</td> |
| 1496 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 1497 | href="contents.html"><img src='../icons/contents.png' |
| 1498 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 1499 | <td class='online-navigation'><img src='../icons/blank.png' |
| 1500 | border='0' height='32' alt='' width='32' /></td> |
| 1501 | <td class='online-navigation'><a rel="index" title="Index" |
| 1502 | href="genindex.html"><img src='../icons/index.png' |
| 1503 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 1504 | </tr></table> |
| 1505 | <div class='online-navigation'> |
| 1506 | <b class="navlabel">Previous:</b> |
| 1507 | <a class="sectref" rel="prev" href="common-structs.html">10.2 Common Object Structures</A> |
| 1508 | <b class="navlabel">Up:</b> |
| 1509 | <a class="sectref" rel="parent" href="newTypes.html">10. Object Implementation Support</A> |
| 1510 | <b class="navlabel">Next:</b> |
| 1511 | <a class="sectref" rel="next" href="mapping-structs.html">10.4 Mapping Object Structures</A> |
| 1512 | </div> |
| 1513 | </div> |
| 1514 | <hr /> |
| 1515 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 1516 | </DIV> |
| 1517 | <!--End of Navigation Panel--> |
| 1518 | <ADDRESS> |
| 1519 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 1520 | </ADDRESS> |
| 1521 | </BODY> |
| 1522 | </HTML> |