Stéphane Wirtel | cbb6484 | 2019-05-17 11:55:34 +0200 | [diff] [blame] | 1 | .. highlight:: c |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 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 Demeyer | 9669931 | 2019-09-10 12:41:59 +0200 | [diff] [blame] | 12 | |
| 13 | Base object types and macros |
| 14 | ---------------------------- |
| 15 | |
Jeroen Ruigrok van der Werven | f4a9f96 | 2009-04-26 20:21:12 +0000 | [diff] [blame] | 16 | All Python objects ultimately share a small number of fields at the beginning |
| 17 | of the object's representation in memory. These are represented by the |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 18 | :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] | 19 | by the expansions of some macros also used, whether directly or indirectly, in |
| 20 | the definition of all other Python objects. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 21 | |
| 22 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 23 | .. c:type:: PyObject |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 24 | |
Jeroen Ruigrok van der Werven | f4a9f96 | 2009-04-26 20:21:12 +0000 | [diff] [blame] | 25 | All object types are extensions of this type. This is a type which |
| 26 | contains the information Python needs to treat a pointer to an object as an |
| 27 | object. In a normal "release" build, it contains only the object's |
Gregory P. Smith | 1b24465 | 2015-04-14 11:12:53 -0700 | [diff] [blame] | 28 | reference count and a pointer to the corresponding type object. |
Gregory P. Smith | 0f2f3bc | 2015-04-14 11:21:05 -0700 | [diff] [blame] | 29 | Nothing is actually declared to be a :c:type:`PyObject`, but every pointer |
| 30 | to a Python object can be cast to a :c:type:`PyObject*`. Access to the |
| 31 | members must be done by using the macros :c:macro:`Py_REFCNT` and |
Gregory P. Smith | 1b24465 | 2015-04-14 11:12:53 -0700 | [diff] [blame] | 32 | :c:macro:`Py_TYPE`. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 33 | |
| 34 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 35 | .. c:type:: PyVarObject |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 36 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 37 | 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] | 38 | field. This is only used for objects that have some notion of *length*. |
Gregory P. Smith | 1b24465 | 2015-04-14 11:12:53 -0700 | [diff] [blame] | 39 | This type does not often appear in the Python/C API. |
| 40 | Access to the members must be done by using the macros |
| 41 | :c:macro:`Py_REFCNT`, :c:macro:`Py_TYPE`, and :c:macro:`Py_SIZE`. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 42 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 43 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 44 | .. c:macro:: PyObject_HEAD |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 45 | |
Gregory P. Smith | 1b24465 | 2015-04-14 11:12:53 -0700 | [diff] [blame] | 46 | This is a macro used when declaring new types which represent objects |
| 47 | without a varying length. The PyObject_HEAD macro expands to:: |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 48 | |
Gregory P. Smith | 1b24465 | 2015-04-14 11:12:53 -0700 | [diff] [blame] | 49 | PyObject ob_base; |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 50 | |
Zachary Ware | 5c676f6 | 2015-07-06 23:27:15 -0500 | [diff] [blame] | 51 | See documentation of :c:type:`PyObject` above. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 52 | |
| 53 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 54 | .. c:macro:: PyObject_VAR_HEAD |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 55 | |
Gregory P. Smith | 1b24465 | 2015-04-14 11:12:53 -0700 | [diff] [blame] | 56 | This is a macro used when declaring new types which represent objects |
| 57 | with a length that varies from instance to instance. |
| 58 | The PyObject_VAR_HEAD macro expands to:: |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 59 | |
Gregory P. Smith | 1b24465 | 2015-04-14 11:12:53 -0700 | [diff] [blame] | 60 | PyVarObject ob_base; |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 61 | |
Gregory P. Smith | 1b24465 | 2015-04-14 11:12:53 -0700 | [diff] [blame] | 62 | See documentation of :c:type:`PyVarObject` above. |
| 63 | |
| 64 | |
| 65 | .. c:macro:: Py_TYPE(o) |
| 66 | |
Zachary Ware | e36402a | 2015-07-06 23:58:12 -0500 | [diff] [blame] | 67 | This macro is used to access the :attr:`ob_type` member of a Python object. |
Gregory P. Smith | 1b24465 | 2015-04-14 11:12:53 -0700 | [diff] [blame] | 68 | It expands to:: |
| 69 | |
| 70 | (((PyObject*)(o))->ob_type) |
| 71 | |
| 72 | |
Victor Stinner | d2ec81a | 2020-02-07 09:17:07 +0100 | [diff] [blame^] | 73 | .. c:function:: void Py_SET_TYPE(PyObject *o, PyTypeObject *type) |
| 74 | |
| 75 | Set the object *o* type to *type*. |
| 76 | |
| 77 | .. versionadded:: 3.9 |
| 78 | |
| 79 | |
Gregory P. Smith | 1b24465 | 2015-04-14 11:12:53 -0700 | [diff] [blame] | 80 | .. c:macro:: Py_REFCNT(o) |
| 81 | |
Zachary Ware | e36402a | 2015-07-06 23:58:12 -0500 | [diff] [blame] | 82 | This macro is used to access the :attr:`ob_refcnt` member of a Python |
| 83 | object. |
Gregory P. Smith | 1b24465 | 2015-04-14 11:12:53 -0700 | [diff] [blame] | 84 | It expands to:: |
| 85 | |
| 86 | (((PyObject*)(o))->ob_refcnt) |
| 87 | |
| 88 | |
Victor Stinner | c86a112 | 2020-02-07 01:24:29 +0100 | [diff] [blame] | 89 | .. c:function:: void Py_SET_REFCNT(PyObject *o, Py_ssize_t refcnt) |
| 90 | |
| 91 | Set the object *o* reference counter to *refcnt*. |
| 92 | |
| 93 | .. versionadded:: 3.9 |
| 94 | |
| 95 | |
Gregory P. Smith | 1b24465 | 2015-04-14 11:12:53 -0700 | [diff] [blame] | 96 | .. c:macro:: Py_SIZE(o) |
| 97 | |
Zachary Ware | e36402a | 2015-07-06 23:58:12 -0500 | [diff] [blame] | 98 | This macro is used to access the :attr:`ob_size` member of a Python object. |
Gregory P. Smith | 1b24465 | 2015-04-14 11:12:53 -0700 | [diff] [blame] | 99 | It expands to:: |
| 100 | |
| 101 | (((PyVarObject*)(o))->ob_size) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 102 | |
Jeroen Ruigrok van der Werven | 939c178 | 2009-04-26 20:25:45 +0000 | [diff] [blame] | 103 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 104 | .. c:macro:: PyObject_HEAD_INIT(type) |
Jeroen Ruigrok van der Werven | 939c178 | 2009-04-26 20:25:45 +0000 | [diff] [blame] | 105 | |
| 106 | This is a macro which expands to initialization values for a new |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 107 | :c:type:`PyObject` type. This macro expands to:: |
Jeroen Ruigrok van der Werven | 939c178 | 2009-04-26 20:25:45 +0000 | [diff] [blame] | 108 | |
| 109 | _PyObject_EXTRA_INIT |
| 110 | 1, type, |
| 111 | |
| 112 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 113 | .. c:macro:: PyVarObject_HEAD_INIT(type, size) |
Jeroen Ruigrok van der Werven | 939c178 | 2009-04-26 20:25:45 +0000 | [diff] [blame] | 114 | |
| 115 | This is a macro which expands to initialization values for a new |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 116 | :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] | 117 | This macro expands to:: |
| 118 | |
| 119 | _PyObject_EXTRA_INIT |
| 120 | 1, type, size, |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 121 | |
| 122 | |
Jeroen Demeyer | 9669931 | 2019-09-10 12:41:59 +0200 | [diff] [blame] | 123 | Implementing functions and methods |
| 124 | ---------------------------------- |
| 125 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 126 | .. c:type:: PyCFunction |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 127 | |
Jeroen Ruigrok van der Werven | f4a9f96 | 2009-04-26 20:21:12 +0000 | [diff] [blame] | 128 | Type of the functions used to implement most Python callables in C. |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 129 | Functions of this type take two :c:type:`PyObject\*` parameters and return |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 130 | one such value. If the return value is ``NULL``, an exception shall have |
| 131 | been set. If not ``NULL``, the return value is interpreted as the return |
Jeroen Ruigrok van der Werven | f4a9f96 | 2009-04-26 20:21:12 +0000 | [diff] [blame] | 132 | value of the function as exposed in Python. The function must return a new |
| 133 | reference. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 134 | |
| 135 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 136 | .. c:type:: PyCFunctionWithKeywords |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 137 | |
Jeroen Demeyer | 5600b5e | 2019-06-16 19:03:23 +0200 | [diff] [blame] | 138 | Type of the functions used to implement Python callables in C |
| 139 | with signature :const:`METH_VARARGS | METH_KEYWORDS`. |
| 140 | |
| 141 | |
| 142 | .. c:type:: _PyCFunctionFast |
| 143 | |
| 144 | Type of the functions used to implement Python callables in C |
| 145 | with signature :const:`METH_FASTCALL`. |
| 146 | |
| 147 | |
| 148 | .. c:type:: _PyCFunctionFastWithKeywords |
| 149 | |
| 150 | Type of the functions used to implement Python callables in C |
| 151 | with signature :const:`METH_FASTCALL | METH_KEYWORDS`. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 152 | |
| 153 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 154 | .. c:type:: PyMethodDef |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 155 | |
| 156 | Structure used to describe a method of an extension type. This structure has |
| 157 | four fields: |
| 158 | |
Serhiy Storchaka | 84b8e92 | 2017-03-30 10:01:03 +0300 | [diff] [blame] | 159 | +------------------+---------------+-------------------------------+ |
| 160 | | Field | C Type | Meaning | |
| 161 | +==================+===============+===============================+ |
| 162 | | :attr:`ml_name` | const char \* | name of the method | |
| 163 | +------------------+---------------+-------------------------------+ |
| 164 | | :attr:`ml_meth` | PyCFunction | pointer to the C | |
| 165 | | | | implementation | |
| 166 | +------------------+---------------+-------------------------------+ |
| 167 | | :attr:`ml_flags` | int | flag bits indicating how the | |
| 168 | | | | call should be constructed | |
| 169 | +------------------+---------------+-------------------------------+ |
| 170 | | :attr:`ml_doc` | const char \* | points to the contents of the | |
| 171 | | | | docstring | |
| 172 | +------------------+---------------+-------------------------------+ |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 173 | |
| 174 | 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] | 175 | types, but they always return :c:type:`PyObject\*`. If the function is not of |
| 176 | the :c:type:`PyCFunction`, the compiler will require a cast in the method table. |
| 177 | Even though :c:type:`PyCFunction` defines the first parameter as |
Benjamin Peterson | 82f34ad | 2015-01-13 09:17:24 -0500 | [diff] [blame] | 178 | :c:type:`PyObject\*`, it is common that the method implementation uses the |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 179 | specific C type of the *self* object. |
| 180 | |
| 181 | The :attr:`ml_flags` field is a bitfield which can include the following flags. |
| 182 | The individual flags indicate either a calling convention or a binding |
Jeroen Demeyer | 5600b5e | 2019-06-16 19:03:23 +0200 | [diff] [blame] | 183 | convention. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 184 | |
Jeroen Demeyer | 5600b5e | 2019-06-16 19:03:23 +0200 | [diff] [blame] | 185 | There are four basic calling conventions for positional arguments |
| 186 | and two of them can be combined with :const:`METH_KEYWORDS` to support |
| 187 | also keyword arguments. So there are a total of 6 calling conventions: |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 188 | |
| 189 | .. data:: METH_VARARGS |
| 190 | |
| 191 | This is the typical calling convention, where the methods have the type |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 192 | :c:type:`PyCFunction`. The function expects two :c:type:`PyObject\*` values. |
Georg Brandl | 21dc5ba | 2009-07-11 10:43:08 +0000 | [diff] [blame] | 193 | The first one is the *self* object for methods; for module functions, it is |
| 194 | the module object. The second parameter (often called *args*) is a tuple |
| 195 | object representing all arguments. This parameter is typically processed |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 196 | using :c:func:`PyArg_ParseTuple` or :c:func:`PyArg_UnpackTuple`. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 197 | |
| 198 | |
Jeroen Demeyer | 5600b5e | 2019-06-16 19:03:23 +0200 | [diff] [blame] | 199 | .. data:: METH_VARARGS | METH_KEYWORDS |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 200 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 201 | Methods with these flags must be of type :c:type:`PyCFunctionWithKeywords`. |
Jeroen Demeyer | 5600b5e | 2019-06-16 19:03:23 +0200 | [diff] [blame] | 202 | The function expects three parameters: *self*, *args*, *kwargs* where |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 203 | *kwargs* is a dictionary of all the keyword arguments or possibly ``NULL`` |
Jeroen Demeyer | 5600b5e | 2019-06-16 19:03:23 +0200 | [diff] [blame] | 204 | if there are no keyword arguments. The parameters are typically processed |
| 205 | using :c:func:`PyArg_ParseTupleAndKeywords`. |
| 206 | |
| 207 | |
| 208 | .. data:: METH_FASTCALL |
| 209 | |
| 210 | Fast calling convention supporting only positional arguments. |
| 211 | The methods have the type :c:type:`_PyCFunctionFast`. |
| 212 | The first parameter is *self*, the second parameter is a C array |
| 213 | of :c:type:`PyObject\*` values indicating the arguments and the third |
| 214 | parameter is the number of arguments (the length of the array). |
| 215 | |
| 216 | This is not part of the :ref:`limited API <stable>`. |
| 217 | |
| 218 | .. versionadded:: 3.7 |
| 219 | |
| 220 | |
| 221 | .. data:: METH_FASTCALL | METH_KEYWORDS |
| 222 | |
| 223 | Extension of :const:`METH_FASTCALL` supporting also keyword arguments, |
| 224 | with methods of type :c:type:`_PyCFunctionFastWithKeywords`. |
Jeroen Demeyer | 9a13a38 | 2019-11-12 14:08:00 +0100 | [diff] [blame] | 225 | Keyword arguments are passed the same way as in the |
| 226 | :ref:`vectorcall protocol <vectorcall>`: |
Jeroen Demeyer | 5600b5e | 2019-06-16 19:03:23 +0200 | [diff] [blame] | 227 | there is an additional fourth :c:type:`PyObject\*` parameter |
| 228 | which is a tuple representing the names of the keyword arguments |
Jeroen Demeyer | 0567786 | 2019-08-16 12:41:27 +0200 | [diff] [blame] | 229 | (which are guaranteed to be strings) |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 230 | or possibly ``NULL`` if there are no keywords. The values of the keyword |
Jeroen Demeyer | 5600b5e | 2019-06-16 19:03:23 +0200 | [diff] [blame] | 231 | arguments are stored in the *args* array, after the positional arguments. |
| 232 | |
| 233 | This is not part of the :ref:`limited API <stable>`. |
| 234 | |
| 235 | .. versionadded:: 3.7 |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 236 | |
| 237 | |
| 238 | .. data:: METH_NOARGS |
| 239 | |
| 240 | Methods without parameters don't need to check whether arguments are given if |
| 241 | 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] | 242 | :c:type:`PyCFunction`. The first parameter is typically named *self* and will |
Georg Brandl | 21dc5ba | 2009-07-11 10:43:08 +0000 | [diff] [blame] | 243 | hold a reference to the module or object instance. In all cases the second |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 244 | parameter will be ``NULL``. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 245 | |
| 246 | |
| 247 | .. data:: METH_O |
| 248 | |
| 249 | 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] | 250 | flag, instead of invoking :c:func:`PyArg_ParseTuple` with a ``"O"`` argument. |
| 251 | They have the type :c:type:`PyCFunction`, with the *self* parameter, and a |
| 252 | :c:type:`PyObject\*` parameter representing the single argument. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 253 | |
| 254 | |
| 255 | These two constants are not used to indicate the calling convention but the |
| 256 | binding when use with methods of classes. These may not be used for functions |
| 257 | defined for modules. At most one of these flags may be set for any given |
| 258 | method. |
| 259 | |
| 260 | |
| 261 | .. data:: METH_CLASS |
| 262 | |
| 263 | .. index:: builtin: classmethod |
| 264 | |
Jeroen Ruigrok van der Werven | f4a9f96 | 2009-04-26 20:21:12 +0000 | [diff] [blame] | 265 | The method will be passed the type object as the first parameter rather |
| 266 | than an instance of the type. This is used to create *class methods*, |
| 267 | similar to what is created when using the :func:`classmethod` built-in |
| 268 | function. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 269 | |
| 270 | |
| 271 | .. data:: METH_STATIC |
| 272 | |
| 273 | .. index:: builtin: staticmethod |
| 274 | |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 275 | The method will be passed ``NULL`` as the first parameter rather than an |
Jeroen Ruigrok van der Werven | f4a9f96 | 2009-04-26 20:21:12 +0000 | [diff] [blame] | 276 | instance of the type. This is used to create *static methods*, similar to |
| 277 | what is created when using the :func:`staticmethod` built-in function. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 278 | |
| 279 | One other constant controls whether a method is loaded in place of another |
| 280 | definition with the same method name. |
| 281 | |
| 282 | |
| 283 | .. data:: METH_COEXIST |
| 284 | |
| 285 | The method will be loaded in place of existing definitions. Without |
| 286 | *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] | 287 | wrappers are loaded before the method table, the existence of a |
| 288 | *sq_contains* slot, for example, would generate a wrapped method named |
| 289 | :meth:`__contains__` and preclude the loading of a corresponding |
| 290 | PyCFunction with the same name. With the flag defined, the PyCFunction |
| 291 | will be loaded in place of the wrapper object and will co-exist with the |
| 292 | slot. This is helpful because calls to PyCFunctions are optimized more |
| 293 | than wrapper object calls. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 294 | |
Georg Brandl | 1f01deb | 2009-01-03 22:47:39 +0000 | [diff] [blame] | 295 | |
Jeroen Demeyer | 9669931 | 2019-09-10 12:41:59 +0200 | [diff] [blame] | 296 | Accessing attributes of extension types |
| 297 | --------------------------------------- |
| 298 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 299 | .. c:type:: PyMemberDef |
Georg Brandl | 1f01deb | 2009-01-03 22:47:39 +0000 | [diff] [blame] | 300 | |
| 301 | Structure which describes an attribute of a type which corresponds to a C |
| 302 | struct member. Its fields are: |
| 303 | |
Serhiy Storchaka | 84b8e92 | 2017-03-30 10:01:03 +0300 | [diff] [blame] | 304 | +------------------+---------------+-------------------------------+ |
| 305 | | Field | C Type | Meaning | |
| 306 | +==================+===============+===============================+ |
| 307 | | :attr:`name` | const char \* | name of the member | |
| 308 | +------------------+---------------+-------------------------------+ |
| 309 | | :attr:`!type` | int | the type of the member in the | |
| 310 | | | | C struct | |
| 311 | +------------------+---------------+-------------------------------+ |
| 312 | | :attr:`offset` | Py_ssize_t | the offset in bytes that the | |
| 313 | | | | member is located on the | |
| 314 | | | | type's object struct | |
| 315 | +------------------+---------------+-------------------------------+ |
| 316 | | :attr:`flags` | int | flag bits indicating if the | |
| 317 | | | | field should be read-only or | |
| 318 | | | | writable | |
| 319 | +------------------+---------------+-------------------------------+ |
| 320 | | :attr:`doc` | const char \* | points to the contents of the | |
| 321 | | | | docstring | |
| 322 | +------------------+---------------+-------------------------------+ |
Georg Brandl | 1f01deb | 2009-01-03 22:47:39 +0000 | [diff] [blame] | 323 | |
csabella | c3c7ef0 | 2017-03-29 20:27:50 -0400 | [diff] [blame] | 324 | :attr:`!type` can be one of many ``T_`` macros corresponding to various C |
Georg Brandl | 1f01deb | 2009-01-03 22:47:39 +0000 | [diff] [blame] | 325 | types. When the member is accessed in Python, it will be converted to the |
| 326 | equivalent Python type. |
| 327 | |
| 328 | =============== ================== |
| 329 | Macro name C type |
| 330 | =============== ================== |
| 331 | T_SHORT short |
| 332 | T_INT int |
| 333 | T_LONG long |
| 334 | T_FLOAT float |
| 335 | T_DOUBLE double |
Serhiy Storchaka | 84b8e92 | 2017-03-30 10:01:03 +0300 | [diff] [blame] | 336 | T_STRING const char \* |
Georg Brandl | 1f01deb | 2009-01-03 22:47:39 +0000 | [diff] [blame] | 337 | T_OBJECT PyObject \* |
| 338 | T_OBJECT_EX PyObject \* |
| 339 | T_CHAR char |
| 340 | T_BYTE char |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 341 | T_UBYTE unsigned char |
Georg Brandl | 1f01deb | 2009-01-03 22:47:39 +0000 | [diff] [blame] | 342 | T_UINT unsigned int |
| 343 | T_USHORT unsigned short |
| 344 | T_ULONG unsigned long |
| 345 | T_BOOL char |
| 346 | T_LONGLONG long long |
| 347 | T_ULONGLONG unsigned long long |
| 348 | T_PYSSIZET Py_ssize_t |
| 349 | =============== ================== |
| 350 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 351 | :c:macro:`T_OBJECT` and :c:macro:`T_OBJECT_EX` differ in that |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 352 | :c:macro:`T_OBJECT` returns ``None`` if the member is ``NULL`` and |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 353 | :c:macro:`T_OBJECT_EX` raises an :exc:`AttributeError`. Try to use |
| 354 | :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] | 355 | handles use of the :keyword:`del` statement on that attribute more correctly |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 356 | than :c:macro:`T_OBJECT`. |
Georg Brandl | 1f01deb | 2009-01-03 22:47:39 +0000 | [diff] [blame] | 357 | |
Serhiy Storchaka | 1ecf7d2 | 2016-10-27 21:41:19 +0300 | [diff] [blame] | 358 | :attr:`flags` can be ``0`` for write and read access or :c:macro:`READONLY` for |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 359 | read-only access. Using :c:macro:`T_STRING` for :attr:`type` implies |
Windson yang | 689d555 | 2018-11-18 03:16:51 +0800 | [diff] [blame] | 360 | :c:macro:`READONLY`. :c:macro:`T_STRING` data is interpreted as UTF-8. |
| 361 | Only :c:macro:`T_OBJECT` and :c:macro:`T_OBJECT_EX` |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 362 | members can be deleted. (They are set to ``NULL``). |
Michael Seifert | da67e0d | 2017-09-15 18:25:27 +0200 | [diff] [blame] | 363 | |
Petr Viktorin | 468f8a6 | 2019-09-25 13:06:16 +0200 | [diff] [blame] | 364 | .. _pymemberdef-offsets: |
| 365 | |
| 366 | Heap allocated types (created using :c:func:`PyType_FromSpec` or similar), |
Gurupad Hegde | 6c7bb38 | 2019-12-28 17:16:02 -0500 | [diff] [blame] | 367 | ``PyMemberDef`` may contain definitions for the special members |
Petr Viktorin | 468f8a6 | 2019-09-25 13:06:16 +0200 | [diff] [blame] | 368 | ``__dictoffset__`` and ``__weaklistoffset__``, corresponding to |
| 369 | :c:member:`~PyTypeObject.tp_dictoffset` and |
| 370 | :c:member:`~PyTypeObject.tp_weaklistoffset` in type objects. |
| 371 | These must be defined with ``T_PYSSIZET`` and ``READONLY``, for example:: |
| 372 | |
| 373 | static PyMemberDef spam_type_members[] = { |
| 374 | {"__dictoffset__", T_PYSSIZET, offsetof(Spam_object, dict), READONLY}, |
| 375 | {NULL} /* Sentinel */ |
| 376 | }; |
Michael Seifert | da67e0d | 2017-09-15 18:25:27 +0200 | [diff] [blame] | 377 | |
| 378 | .. c:type:: PyGetSetDef |
| 379 | |
| 380 | Structure to define property-like access for a type. See also description of |
| 381 | the :c:member:`PyTypeObject.tp_getset` slot. |
| 382 | |
| 383 | +-------------+------------------+-----------------------------------+ |
| 384 | | Field | C Type | Meaning | |
| 385 | +=============+==================+===================================+ |
| 386 | | name | const char \* | attribute name | |
| 387 | +-------------+------------------+-----------------------------------+ |
| 388 | | get | getter | C Function to get the attribute | |
| 389 | +-------------+------------------+-----------------------------------+ |
| 390 | | set | setter | optional C function to set or | |
| 391 | | | | delete the attribute, if omitted | |
| 392 | | | | the attribute is readonly | |
| 393 | +-------------+------------------+-----------------------------------+ |
| 394 | | doc | const char \* | optional docstring | |
| 395 | +-------------+------------------+-----------------------------------+ |
| 396 | | closure | void \* | optional function pointer, | |
| 397 | | | | providing additional data for | |
| 398 | | | | getter and setter | |
| 399 | +-------------+------------------+-----------------------------------+ |
| 400 | |
| 401 | The ``get`` function takes one :c:type:`PyObject\*` parameter (the |
| 402 | instance) and a function pointer (the associated ``closure``):: |
| 403 | |
| 404 | typedef PyObject *(*getter)(PyObject *, void *); |
| 405 | |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 406 | It should return a new reference on success or ``NULL`` with a set exception |
Michael Seifert | da67e0d | 2017-09-15 18:25:27 +0200 | [diff] [blame] | 407 | on failure. |
| 408 | |
| 409 | ``set`` functions take two :c:type:`PyObject\*` parameters (the instance and |
| 410 | the value to be set) and a function pointer (the associated ``closure``):: |
| 411 | |
| 412 | typedef int (*setter)(PyObject *, PyObject *, void *); |
| 413 | |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 414 | In case the attribute should be deleted the second parameter is ``NULL``. |
Michael Seifert | da67e0d | 2017-09-15 18:25:27 +0200 | [diff] [blame] | 415 | Should return ``0`` on success or ``-1`` with a set exception on failure. |