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 | |
Jeroen Ruigrok van der Werven | f4a9f96 | 2009-04-26 20:21:12 +0000 | [diff] [blame] | 12 | All Python objects ultimately share a small number of fields at the beginning |
| 13 | of the object's representation in memory. These are represented by the |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 14 | :c:type:`PyObject` and :c:type:`PyVarObject` types, which are defined, in turn, |
Jeroen Ruigrok van der Werven | f4a9f96 | 2009-04-26 20:21:12 +0000 | [diff] [blame] | 15 | by the expansions of some macros also used, whether directly or indirectly, in |
| 16 | the definition of all other Python objects. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 17 | |
| 18 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 19 | .. c:type:: PyObject |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 20 | |
Jeroen Ruigrok van der Werven | f4a9f96 | 2009-04-26 20:21:12 +0000 | [diff] [blame] | 21 | All object types are extensions of this type. This is a type which |
| 22 | contains the information Python needs to treat a pointer to an object as an |
| 23 | object. In a normal "release" build, it contains only the object's |
| 24 | reference count and a pointer to the corresponding type object. It |
| 25 | corresponds to the fields defined by the expansion of the ``PyObject_HEAD`` |
| 26 | macro. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 27 | |
| 28 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 29 | .. c:type:: PyVarObject |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 30 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 31 | This is an extension of :c:type:`PyObject` that adds the :attr:`ob_size` |
Jeroen Ruigrok van der Werven | f4a9f96 | 2009-04-26 20:21:12 +0000 | [diff] [blame] | 32 | field. This is only used for objects that have some notion of *length*. |
| 33 | This type does not often appear in the Python/C API. It corresponds to the |
| 34 | fields defined by the expansion of the ``PyObject_VAR_HEAD`` macro. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 35 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 36 | These macros are used in the definition of :c:type:`PyObject` and |
| 37 | :c:type:`PyVarObject`: |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 38 | |
| 39 | .. XXX need to document PEP 3123 changes here |
| 40 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 41 | .. c:macro:: PyObject_HEAD |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 42 | |
| 43 | This is a macro which expands to the declarations of the fields of the |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 44 | :c:type:`PyObject` type; it is used when declaring new types which represent |
Jeroen Ruigrok van der Werven | f4a9f96 | 2009-04-26 20:21:12 +0000 | [diff] [blame] | 45 | objects without a varying length. The specific fields it expands to depend |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 46 | on the definition of :c:macro:`Py_TRACE_REFS`. By default, that macro is |
| 47 | not defined, and :c:macro:`PyObject_HEAD` expands to:: |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 48 | |
| 49 | Py_ssize_t ob_refcnt; |
| 50 | PyTypeObject *ob_type; |
| 51 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 52 | When :c:macro:`Py_TRACE_REFS` is defined, it expands to:: |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 53 | |
| 54 | PyObject *_ob_next, *_ob_prev; |
| 55 | Py_ssize_t ob_refcnt; |
| 56 | PyTypeObject *ob_type; |
| 57 | |
| 58 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 59 | .. c:macro:: PyObject_VAR_HEAD |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 60 | |
| 61 | This is a macro which expands to the declarations of the fields of the |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 62 | :c:type:`PyVarObject` type; it is used when declaring new types which |
Jeroen Ruigrok van der Werven | f4a9f96 | 2009-04-26 20:21:12 +0000 | [diff] [blame] | 63 | represent objects with a length that varies from instance to instance. |
| 64 | This macro always expands to:: |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 65 | |
| 66 | PyObject_HEAD |
| 67 | Py_ssize_t ob_size; |
| 68 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 69 | Note that :c:macro:`PyObject_HEAD` is part of the expansion, and that its own |
| 70 | expansion varies depending on the definition of :c:macro:`Py_TRACE_REFS`. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 71 | |
Jeroen Ruigrok van der Werven | 939c178 | 2009-04-26 20:25:45 +0000 | [diff] [blame] | 72 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 73 | .. c:macro:: PyObject_HEAD_INIT(type) |
Jeroen Ruigrok van der Werven | 939c178 | 2009-04-26 20:25:45 +0000 | [diff] [blame] | 74 | |
| 75 | This is a macro which expands to initialization values for a new |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 76 | :c:type:`PyObject` type. This macro expands to:: |
Jeroen Ruigrok van der Werven | 939c178 | 2009-04-26 20:25:45 +0000 | [diff] [blame] | 77 | |
| 78 | _PyObject_EXTRA_INIT |
| 79 | 1, type, |
| 80 | |
| 81 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 82 | .. c:macro:: PyVarObject_HEAD_INIT(type, size) |
Jeroen Ruigrok van der Werven | 939c178 | 2009-04-26 20:25:45 +0000 | [diff] [blame] | 83 | |
| 84 | This is a macro which expands to initialization values for a new |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 85 | :c:type:`PyVarObject` type, including the :attr:`ob_size` field. |
Jeroen Ruigrok van der Werven | 939c178 | 2009-04-26 20:25:45 +0000 | [diff] [blame] | 86 | This macro expands to:: |
| 87 | |
| 88 | _PyObject_EXTRA_INIT |
| 89 | 1, type, size, |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 90 | |
| 91 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 92 | .. c:type:: PyCFunction |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 93 | |
Jeroen Ruigrok van der Werven | f4a9f96 | 2009-04-26 20:21:12 +0000 | [diff] [blame] | 94 | Type of the functions used to implement most Python callables in C. |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 95 | Functions of this type take two :c:type:`PyObject\*` parameters and return |
Jeroen Ruigrok van der Werven | f4a9f96 | 2009-04-26 20:21:12 +0000 | [diff] [blame] | 96 | one such value. If the return value is *NULL*, an exception shall have |
| 97 | been set. If not *NULL*, the return value is interpreted as the return |
| 98 | value of the function as exposed in Python. The function must return a new |
| 99 | reference. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 100 | |
| 101 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 102 | .. c:type:: PyCFunctionWithKeywords |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 103 | |
| 104 | Type of the functions used to implement Python callables in C that take |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 105 | keyword arguments: they take three :c:type:`PyObject\*` parameters and return |
| 106 | one such value. See :c:type:`PyCFunction` above for the meaning of the return |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 107 | value. |
| 108 | |
| 109 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 110 | .. c:type:: PyMethodDef |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 111 | |
| 112 | Structure used to describe a method of an extension type. This structure has |
| 113 | four fields: |
| 114 | |
| 115 | +------------------+-------------+-------------------------------+ |
| 116 | | Field | C Type | Meaning | |
| 117 | +==================+=============+===============================+ |
| 118 | | :attr:`ml_name` | char \* | name of the method | |
| 119 | +------------------+-------------+-------------------------------+ |
| 120 | | :attr:`ml_meth` | PyCFunction | pointer to the C | |
| 121 | | | | implementation | |
| 122 | +------------------+-------------+-------------------------------+ |
| 123 | | :attr:`ml_flags` | int | flag bits indicating how the | |
| 124 | | | | call should be constructed | |
| 125 | +------------------+-------------+-------------------------------+ |
| 126 | | :attr:`ml_doc` | char \* | points to the contents of the | |
| 127 | | | | docstring | |
| 128 | +------------------+-------------+-------------------------------+ |
| 129 | |
| 130 | The :attr:`ml_meth` is a C function pointer. The functions may be of different |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 131 | types, but they always return :c:type:`PyObject\*`. If the function is not of |
| 132 | the :c:type:`PyCFunction`, the compiler will require a cast in the method table. |
| 133 | Even though :c:type:`PyCFunction` defines the first parameter as |
| 134 | :c:type:`PyObject\*`, it is common that the method implementation uses a the |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 135 | specific C type of the *self* object. |
| 136 | |
| 137 | The :attr:`ml_flags` field is a bitfield which can include the following flags. |
| 138 | The individual flags indicate either a calling convention or a binding |
| 139 | convention. Of the calling convention flags, only :const:`METH_VARARGS` and |
| 140 | :const:`METH_KEYWORDS` can be combined (but note that :const:`METH_KEYWORDS` |
| 141 | alone is equivalent to ``METH_VARARGS | METH_KEYWORDS``). Any of the calling |
| 142 | convention flags can be combined with a binding flag. |
| 143 | |
| 144 | |
| 145 | .. data:: METH_VARARGS |
| 146 | |
| 147 | This is the typical calling convention, where the methods have the type |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 148 | :c:type:`PyCFunction`. The function expects two :c:type:`PyObject\*` values. |
Georg Brandl | 21dc5ba | 2009-07-11 10:43:08 +0000 | [diff] [blame] | 149 | The first one is the *self* object for methods; for module functions, it is |
| 150 | the module object. The second parameter (often called *args*) is a tuple |
| 151 | object representing all arguments. This parameter is typically processed |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 152 | using :c:func:`PyArg_ParseTuple` or :c:func:`PyArg_UnpackTuple`. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 153 | |
| 154 | |
| 155 | .. data:: METH_KEYWORDS |
| 156 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 157 | Methods with these flags must be of type :c:type:`PyCFunctionWithKeywords`. |
Jeroen Ruigrok van der Werven | f4a9f96 | 2009-04-26 20:21:12 +0000 | [diff] [blame] | 158 | The function expects three parameters: *self*, *args*, and a dictionary of |
| 159 | all the keyword arguments. The flag is typically combined with |
| 160 | :const:`METH_VARARGS`, and the parameters are typically processed using |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 161 | :c:func:`PyArg_ParseTupleAndKeywords`. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 162 | |
| 163 | |
| 164 | .. data:: METH_NOARGS |
| 165 | |
| 166 | Methods without parameters don't need to check whether arguments are given if |
| 167 | they are listed with the :const:`METH_NOARGS` flag. They need to be of type |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 168 | :c:type:`PyCFunction`. The first parameter is typically named *self* and will |
Georg Brandl | 21dc5ba | 2009-07-11 10:43:08 +0000 | [diff] [blame] | 169 | hold a reference to the module or object instance. In all cases the second |
| 170 | parameter will be *NULL*. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 171 | |
| 172 | |
| 173 | .. data:: METH_O |
| 174 | |
| 175 | Methods with a single object argument can be listed with the :const:`METH_O` |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 176 | flag, instead of invoking :c:func:`PyArg_ParseTuple` with a ``"O"`` argument. |
| 177 | They have the type :c:type:`PyCFunction`, with the *self* parameter, and a |
| 178 | :c:type:`PyObject\*` parameter representing the single argument. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 179 | |
| 180 | |
| 181 | These two constants are not used to indicate the calling convention but the |
| 182 | binding when use with methods of classes. These may not be used for functions |
| 183 | defined for modules. At most one of these flags may be set for any given |
| 184 | method. |
| 185 | |
| 186 | |
| 187 | .. data:: METH_CLASS |
| 188 | |
| 189 | .. index:: builtin: classmethod |
| 190 | |
Jeroen Ruigrok van der Werven | f4a9f96 | 2009-04-26 20:21:12 +0000 | [diff] [blame] | 191 | The method will be passed the type object as the first parameter rather |
| 192 | than an instance of the type. This is used to create *class methods*, |
| 193 | similar to what is created when using the :func:`classmethod` built-in |
| 194 | function. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 195 | |
| 196 | |
| 197 | .. data:: METH_STATIC |
| 198 | |
| 199 | .. index:: builtin: staticmethod |
| 200 | |
Jeroen Ruigrok van der Werven | f4a9f96 | 2009-04-26 20:21:12 +0000 | [diff] [blame] | 201 | The method will be passed *NULL* as the first parameter rather than an |
| 202 | instance of the type. This is used to create *static methods*, similar to |
| 203 | what is created when using the :func:`staticmethod` built-in function. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 204 | |
| 205 | One other constant controls whether a method is loaded in place of another |
| 206 | definition with the same method name. |
| 207 | |
| 208 | |
| 209 | .. data:: METH_COEXIST |
| 210 | |
| 211 | The method will be loaded in place of existing definitions. Without |
| 212 | *METH_COEXIST*, the default is to skip repeated definitions. Since slot |
Jeroen Ruigrok van der Werven | f4a9f96 | 2009-04-26 20:21:12 +0000 | [diff] [blame] | 213 | wrappers are loaded before the method table, the existence of a |
| 214 | *sq_contains* slot, for example, would generate a wrapped method named |
| 215 | :meth:`__contains__` and preclude the loading of a corresponding |
| 216 | PyCFunction with the same name. With the flag defined, the PyCFunction |
| 217 | will be loaded in place of the wrapper object and will co-exist with the |
| 218 | slot. This is helpful because calls to PyCFunctions are optimized more |
| 219 | than wrapper object calls. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 220 | |
Georg Brandl | 1f01deb | 2009-01-03 22:47:39 +0000 | [diff] [blame] | 221 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 222 | .. c:type:: PyMemberDef |
Georg Brandl | 1f01deb | 2009-01-03 22:47:39 +0000 | [diff] [blame] | 223 | |
| 224 | Structure which describes an attribute of a type which corresponds to a C |
| 225 | struct member. Its fields are: |
| 226 | |
| 227 | +------------------+-------------+-------------------------------+ |
| 228 | | Field | C Type | Meaning | |
| 229 | +==================+=============+===============================+ |
| 230 | | :attr:`name` | char \* | name of the member | |
| 231 | +------------------+-------------+-------------------------------+ |
| 232 | | :attr:`type` | int | the type of the member in the | |
| 233 | | | | C struct | |
| 234 | +------------------+-------------+-------------------------------+ |
| 235 | | :attr:`offset` | Py_ssize_t | the offset in bytes that the | |
| 236 | | | | member is located on the | |
| 237 | | | | type's object struct | |
| 238 | +------------------+-------------+-------------------------------+ |
| 239 | | :attr:`flags` | int | flag bits indicating if the | |
| 240 | | | | field should be read-only or | |
| 241 | | | | writable | |
| 242 | +------------------+-------------+-------------------------------+ |
| 243 | | :attr:`doc` | char \* | points to the contents of the | |
| 244 | | | | docstring | |
| 245 | +------------------+-------------+-------------------------------+ |
| 246 | |
| 247 | :attr:`type` can be one of many ``T_`` macros corresponding to various C |
| 248 | types. When the member is accessed in Python, it will be converted to the |
| 249 | equivalent Python type. |
| 250 | |
| 251 | =============== ================== |
| 252 | Macro name C type |
| 253 | =============== ================== |
| 254 | T_SHORT short |
| 255 | T_INT int |
| 256 | T_LONG long |
| 257 | T_FLOAT float |
| 258 | T_DOUBLE double |
| 259 | T_STRING char \* |
| 260 | T_OBJECT PyObject \* |
| 261 | T_OBJECT_EX PyObject \* |
| 262 | T_CHAR char |
| 263 | T_BYTE char |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 264 | T_UBYTE unsigned char |
Georg Brandl | 1f01deb | 2009-01-03 22:47:39 +0000 | [diff] [blame] | 265 | T_UINT unsigned int |
| 266 | T_USHORT unsigned short |
| 267 | T_ULONG unsigned long |
| 268 | T_BOOL char |
| 269 | T_LONGLONG long long |
| 270 | T_ULONGLONG unsigned long long |
| 271 | T_PYSSIZET Py_ssize_t |
| 272 | =============== ================== |
| 273 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 274 | :c:macro:`T_OBJECT` and :c:macro:`T_OBJECT_EX` differ in that |
| 275 | :c:macro:`T_OBJECT` returns ``None`` if the member is *NULL* and |
| 276 | :c:macro:`T_OBJECT_EX` raises an :exc:`AttributeError`. Try to use |
| 277 | :c:macro:`T_OBJECT_EX` over :c:macro:`T_OBJECT` because :c:macro:`T_OBJECT_EX` |
Ezio Melotti | 479def3 | 2010-01-03 09:11:59 +0000 | [diff] [blame] | 278 | handles use of the :keyword:`del` statement on that attribute more correctly |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 279 | than :c:macro:`T_OBJECT`. |
Georg Brandl | 1f01deb | 2009-01-03 22:47:39 +0000 | [diff] [blame] | 280 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 281 | :attr:`flags` can be 0 for write and read access or :c:macro:`READONLY` for |
| 282 | read-only access. Using :c:macro:`T_STRING` for :attr:`type` implies |
| 283 | :c:macro:`READONLY`. Only :c:macro:`T_OBJECT` and :c:macro:`T_OBJECT_EX` |
Georg Brandl | 1f01deb | 2009-01-03 22:47:39 +0000 | [diff] [blame] | 284 | members can be deleted. (They are set to *NULL*). |