Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 1 | .. highlightlang:: c |
| 2 | |
| 3 | .. _common-structs: |
| 4 | |
| 5 | Common Object Structures |
| 6 | ======================== |
| 7 | |
| 8 | There are a large number of structures which are used in the definition of |
| 9 | object types for Python. This section describes these structures and how they |
| 10 | are used. |
| 11 | |
| 12 | All Python objects ultimately share a small number of fields at the beginning of |
| 13 | the object's representation in memory. These are represented by the |
| 14 | :ctype:`PyObject` and :ctype:`PyVarObject` types, which are defined, in turn, by |
| 15 | the expansions of some macros also used, whether directly or indirectly, in the |
| 16 | definition of all other Python objects. |
| 17 | |
| 18 | |
| 19 | .. ctype:: PyObject |
| 20 | |
| 21 | All object types are extensions of this type. This is a type which contains the |
| 22 | information Python needs to treat a pointer to an object as an object. In a |
Christian Heimes | 4d6ec85 | 2008-03-26 22:34:47 +0000 | [diff] [blame] | 23 | normal "release" build, it contains only the object's reference count and a |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 24 | pointer to the corresponding type object. It corresponds to the fields defined |
| 25 | by the expansion of the ``PyObject_HEAD`` macro. |
| 26 | |
| 27 | |
| 28 | .. ctype:: PyVarObject |
| 29 | |
| 30 | This is an extension of :ctype:`PyObject` that adds the :attr:`ob_size` field. |
| 31 | This is only used for objects that have some notion of *length*. This type does |
| 32 | not often appear in the Python/C API. It corresponds to the fields defined by |
| 33 | the expansion of the ``PyObject_VAR_HEAD`` macro. |
| 34 | |
| 35 | These macros are used in the definition of :ctype:`PyObject` and |
| 36 | :ctype:`PyVarObject`: |
| 37 | |
| 38 | .. XXX need to document PEP 3123 changes here |
| 39 | |
| 40 | .. cmacro:: PyObject_HEAD |
| 41 | |
| 42 | This is a macro which expands to the declarations of the fields of the |
| 43 | :ctype:`PyObject` type; it is used when declaring new types which represent |
| 44 | objects without a varying length. The specific fields it expands to depend on |
| 45 | the definition of :cmacro:`Py_TRACE_REFS`. By default, that macro is not |
| 46 | defined, and :cmacro:`PyObject_HEAD` expands to:: |
| 47 | |
| 48 | Py_ssize_t ob_refcnt; |
| 49 | PyTypeObject *ob_type; |
| 50 | |
| 51 | When :cmacro:`Py_TRACE_REFS` is defined, it expands to:: |
| 52 | |
| 53 | PyObject *_ob_next, *_ob_prev; |
| 54 | Py_ssize_t ob_refcnt; |
| 55 | PyTypeObject *ob_type; |
| 56 | |
| 57 | |
| 58 | .. cmacro:: PyObject_VAR_HEAD |
| 59 | |
| 60 | This is a macro which expands to the declarations of the fields of the |
| 61 | :ctype:`PyVarObject` type; it is used when declaring new types which represent |
| 62 | objects with a length that varies from instance to instance. This macro always |
| 63 | expands to:: |
| 64 | |
| 65 | PyObject_HEAD |
| 66 | Py_ssize_t ob_size; |
| 67 | |
| 68 | Note that :cmacro:`PyObject_HEAD` is part of the expansion, and that its own |
| 69 | expansion varies depending on the definition of :cmacro:`Py_TRACE_REFS`. |
| 70 | |
| 71 | .. cmacro:: PyObject_HEAD_INIT |
| 72 | |
| 73 | |
| 74 | .. ctype:: PyCFunction |
| 75 | |
| 76 | Type of the functions used to implement most Python callables in C. Functions of |
| 77 | this type take two :ctype:`PyObject\*` parameters and return one such value. If |
| 78 | the return value is *NULL*, an exception shall have been set. If not *NULL*, |
| 79 | the return value is interpreted as the return value of the function as exposed |
| 80 | in Python. The function must return a new reference. |
| 81 | |
| 82 | |
| 83 | .. ctype:: PyCFunctionWithKeywords |
| 84 | |
| 85 | Type of the functions used to implement Python callables in C that take |
| 86 | keyword arguments: they take three :ctype:`PyObject\*` parameters and return |
| 87 | one such value. See :ctype:`PyCFunction` above for the meaning of the return |
| 88 | value. |
| 89 | |
| 90 | |
| 91 | .. ctype:: PyMethodDef |
| 92 | |
| 93 | Structure used to describe a method of an extension type. This structure has |
| 94 | four fields: |
| 95 | |
| 96 | +------------------+-------------+-------------------------------+ |
| 97 | | Field | C Type | Meaning | |
| 98 | +==================+=============+===============================+ |
| 99 | | :attr:`ml_name` | char \* | name of the method | |
| 100 | +------------------+-------------+-------------------------------+ |
| 101 | | :attr:`ml_meth` | PyCFunction | pointer to the C | |
| 102 | | | | implementation | |
| 103 | +------------------+-------------+-------------------------------+ |
| 104 | | :attr:`ml_flags` | int | flag bits indicating how the | |
| 105 | | | | call should be constructed | |
| 106 | +------------------+-------------+-------------------------------+ |
| 107 | | :attr:`ml_doc` | char \* | points to the contents of the | |
| 108 | | | | docstring | |
| 109 | +------------------+-------------+-------------------------------+ |
| 110 | |
| 111 | The :attr:`ml_meth` is a C function pointer. The functions may be of different |
| 112 | types, but they always return :ctype:`PyObject\*`. If the function is not of |
| 113 | the :ctype:`PyCFunction`, the compiler will require a cast in the method table. |
| 114 | Even though :ctype:`PyCFunction` defines the first parameter as |
| 115 | :ctype:`PyObject\*`, it is common that the method implementation uses a the |
| 116 | specific C type of the *self* object. |
| 117 | |
| 118 | The :attr:`ml_flags` field is a bitfield which can include the following flags. |
| 119 | The individual flags indicate either a calling convention or a binding |
| 120 | convention. Of the calling convention flags, only :const:`METH_VARARGS` and |
| 121 | :const:`METH_KEYWORDS` can be combined (but note that :const:`METH_KEYWORDS` |
| 122 | alone is equivalent to ``METH_VARARGS | METH_KEYWORDS``). Any of the calling |
| 123 | convention flags can be combined with a binding flag. |
| 124 | |
| 125 | |
| 126 | .. data:: METH_VARARGS |
| 127 | |
| 128 | This is the typical calling convention, where the methods have the type |
| 129 | :ctype:`PyCFunction`. The function expects two :ctype:`PyObject\*` values. The |
| 130 | first one is the *self* object for methods; for module functions, it has the |
| 131 | value given to :cfunc:`Py_InitModule4` (or *NULL* if :cfunc:`Py_InitModule` was |
| 132 | used). The second parameter (often called *args*) is a tuple object |
| 133 | representing all arguments. This parameter is typically processed using |
| 134 | :cfunc:`PyArg_ParseTuple` or :cfunc:`PyArg_UnpackTuple`. |
| 135 | |
| 136 | |
| 137 | .. data:: METH_KEYWORDS |
| 138 | |
| 139 | Methods with these flags must be of type :ctype:`PyCFunctionWithKeywords`. The |
| 140 | function expects three parameters: *self*, *args*, and a dictionary of all the |
| 141 | keyword arguments. The flag is typically combined with :const:`METH_VARARGS`, |
| 142 | and the parameters are typically processed using |
| 143 | :cfunc:`PyArg_ParseTupleAndKeywords`. |
| 144 | |
| 145 | |
| 146 | .. data:: METH_NOARGS |
| 147 | |
| 148 | Methods without parameters don't need to check whether arguments are given if |
| 149 | they are listed with the :const:`METH_NOARGS` flag. They need to be of type |
| 150 | :ctype:`PyCFunction`. When used with object methods, the first parameter is |
| 151 | typically named ``self`` and will hold a reference to the object instance. In |
| 152 | all cases the second parameter will be *NULL*. |
| 153 | |
| 154 | |
| 155 | .. data:: METH_O |
| 156 | |
| 157 | Methods with a single object argument can be listed with the :const:`METH_O` |
| 158 | flag, instead of invoking :cfunc:`PyArg_ParseTuple` with a ``"O"`` argument. |
| 159 | They have the type :ctype:`PyCFunction`, with the *self* parameter, and a |
| 160 | :ctype:`PyObject\*` parameter representing the single argument. |
| 161 | |
| 162 | |
| 163 | These two constants are not used to indicate the calling convention but the |
| 164 | binding when use with methods of classes. These may not be used for functions |
| 165 | defined for modules. At most one of these flags may be set for any given |
| 166 | method. |
| 167 | |
| 168 | |
| 169 | .. data:: METH_CLASS |
| 170 | |
| 171 | .. index:: builtin: classmethod |
| 172 | |
| 173 | The method will be passed the type object as the first parameter rather than an |
| 174 | instance of the type. This is used to create *class methods*, similar to what |
| 175 | is created when using the :func:`classmethod` built-in function. |
| 176 | |
| 177 | |
| 178 | .. data:: METH_STATIC |
| 179 | |
| 180 | .. index:: builtin: staticmethod |
| 181 | |
| 182 | The method will be passed *NULL* as the first parameter rather than an instance |
| 183 | of the type. This is used to create *static methods*, similar to what is |
| 184 | created when using the :func:`staticmethod` built-in function. |
| 185 | |
| 186 | One other constant controls whether a method is loaded in place of another |
| 187 | definition with the same method name. |
| 188 | |
| 189 | |
| 190 | .. data:: METH_COEXIST |
| 191 | |
| 192 | The method will be loaded in place of existing definitions. Without |
| 193 | *METH_COEXIST*, the default is to skip repeated definitions. Since slot |
| 194 | wrappers are loaded before the method table, the existence of a *sq_contains* |
| 195 | slot, for example, would generate a wrapped method named :meth:`__contains__` |
| 196 | and preclude the loading of a corresponding PyCFunction with the same name. |
| 197 | With the flag defined, the PyCFunction will be loaded in place of the wrapper |
| 198 | object and will co-exist with the slot. This is helpful because calls to |
| 199 | PyCFunctions are optimized more than wrapper object calls. |
| 200 | |