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