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 | |
Victor Stinner | 09bbebe | 2021-04-11 00:17:39 +0200 | [diff] [blame^] | 65 | .. c:function:: int Py_Is(const PyObject *x, const PyObject *y) |
| 66 | |
| 67 | Test if the *x* object is the *y* object, the same as ``x is y`` in Python. |
| 68 | |
| 69 | .. versionadded:: 3.10 |
| 70 | |
| 71 | |
| 72 | .. c:function:: int Py_IsNone(const PyObject *x) |
| 73 | |
| 74 | Test if an object is the ``None`` singleton, |
| 75 | the same as ``x is None`` in Python. |
| 76 | |
| 77 | .. versionadded:: 3.10 |
| 78 | |
| 79 | |
| 80 | .. c:function:: int Py_IsTrue(const PyObject *x) |
| 81 | |
| 82 | Test if an object is the ``True`` singleton, |
| 83 | the same as ``x is True`` in Python. |
| 84 | |
| 85 | .. versionadded:: 3.10 |
| 86 | |
| 87 | |
| 88 | .. c:function:: int Py_IsFalse(const PyObject *x) |
| 89 | |
| 90 | Test if an object is the ``False`` singleton, |
| 91 | the same as ``x is False`` in Python. |
| 92 | |
| 93 | .. versionadded:: 3.10 |
| 94 | |
| 95 | |
Dong-hee Na | ad3252b | 2020-05-26 01:52:54 +0900 | [diff] [blame] | 96 | .. c:function:: PyTypeObject* Py_TYPE(const PyObject *o) |
Gregory P. Smith | 1b24465 | 2015-04-14 11:12:53 -0700 | [diff] [blame] | 97 | |
Dong-hee Na | ad3252b | 2020-05-26 01:52:54 +0900 | [diff] [blame] | 98 | Get the type of the Python object *o*. |
Gregory P. Smith | 1b24465 | 2015-04-14 11:12:53 -0700 | [diff] [blame] | 99 | |
Victor Stinner | 23c5f93 | 2020-11-09 13:40:47 +0100 | [diff] [blame] | 100 | Return a :term:`borrowed reference`. |
Dong-hee Na | ad3252b | 2020-05-26 01:52:54 +0900 | [diff] [blame] | 101 | |
Victor Stinner | 0e2ac21 | 2020-11-18 18:48:06 +0100 | [diff] [blame] | 102 | The :c:func:`Py_SET_TYPE` function must be used to set an object type. |
Gregory P. Smith | 1b24465 | 2015-04-14 11:12:53 -0700 | [diff] [blame] | 103 | |
| 104 | |
Dong-hee Na | d905df7 | 2020-02-14 02:37:17 +0900 | [diff] [blame] | 105 | .. c:function:: int Py_IS_TYPE(PyObject *o, PyTypeObject *type) |
| 106 | |
| 107 | Return non-zero if the object *o* type is *type*. Return zero otherwise. |
| 108 | Equivalent to: ``Py_TYPE(o) == type``. |
| 109 | |
| 110 | .. versionadded:: 3.9 |
| 111 | |
| 112 | |
Victor Stinner | d2ec81a | 2020-02-07 09:17:07 +0100 | [diff] [blame] | 113 | .. c:function:: void Py_SET_TYPE(PyObject *o, PyTypeObject *type) |
| 114 | |
| 115 | Set the object *o* type to *type*. |
| 116 | |
| 117 | .. versionadded:: 3.9 |
| 118 | |
| 119 | |
Victor Stinner | fe2978b | 2020-05-27 14:55:10 +0200 | [diff] [blame] | 120 | .. c:function:: Py_ssize_t Py_REFCNT(const PyObject *o) |
Gregory P. Smith | 1b24465 | 2015-04-14 11:12:53 -0700 | [diff] [blame] | 121 | |
Victor Stinner | fe2978b | 2020-05-27 14:55:10 +0200 | [diff] [blame] | 122 | Get the reference count of the Python object *o*. |
Gregory P. Smith | 1b24465 | 2015-04-14 11:12:53 -0700 | [diff] [blame] | 123 | |
Victor Stinner | fe2978b | 2020-05-27 14:55:10 +0200 | [diff] [blame] | 124 | .. versionchanged:: 3.10 |
| 125 | :c:func:`Py_REFCNT()` is changed to the inline static function. |
| 126 | Use :c:func:`Py_SET_REFCNT()` to set an object reference count. |
Gregory P. Smith | 1b24465 | 2015-04-14 11:12:53 -0700 | [diff] [blame] | 127 | |
| 128 | |
Victor Stinner | c86a112 | 2020-02-07 01:24:29 +0100 | [diff] [blame] | 129 | .. c:function:: void Py_SET_REFCNT(PyObject *o, Py_ssize_t refcnt) |
| 130 | |
| 131 | Set the object *o* reference counter to *refcnt*. |
| 132 | |
| 133 | .. versionadded:: 3.9 |
| 134 | |
| 135 | |
Victor Stinner | fe2978b | 2020-05-27 14:55:10 +0200 | [diff] [blame] | 136 | .. c:function:: Py_ssize_t Py_SIZE(const PyVarObject *o) |
Gregory P. Smith | 1b24465 | 2015-04-14 11:12:53 -0700 | [diff] [blame] | 137 | |
Victor Stinner | fe2978b | 2020-05-27 14:55:10 +0200 | [diff] [blame] | 138 | Get the size of the Python object *o*. |
Gregory P. Smith | 1b24465 | 2015-04-14 11:12:53 -0700 | [diff] [blame] | 139 | |
Victor Stinner | 0e2ac21 | 2020-11-18 18:48:06 +0100 | [diff] [blame] | 140 | The :c:func:`Py_SET_SIZE` function must be used to set an object size. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 141 | |
Jeroen Ruigrok van der Werven | 939c178 | 2009-04-26 20:25:45 +0000 | [diff] [blame] | 142 | |
Victor Stinner | b10dc3e | 2020-02-07 12:05:12 +0100 | [diff] [blame] | 143 | .. c:function:: void Py_SET_SIZE(PyVarObject *o, Py_ssize_t size) |
| 144 | |
Brandt Bucher | 968dcd9 | 2020-02-13 09:34:45 -0800 | [diff] [blame] | 145 | Set the object *o* size to *size*. |
Victor Stinner | b10dc3e | 2020-02-07 12:05:12 +0100 | [diff] [blame] | 146 | |
| 147 | .. versionadded:: 3.9 |
| 148 | |
| 149 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 150 | .. c:macro:: PyObject_HEAD_INIT(type) |
Jeroen Ruigrok van der Werven | 939c178 | 2009-04-26 20:25:45 +0000 | [diff] [blame] | 151 | |
| 152 | This is a macro which expands to initialization values for a new |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 153 | :c:type:`PyObject` type. This macro expands to:: |
Jeroen Ruigrok van der Werven | 939c178 | 2009-04-26 20:25:45 +0000 | [diff] [blame] | 154 | |
| 155 | _PyObject_EXTRA_INIT |
| 156 | 1, type, |
| 157 | |
| 158 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 159 | .. c:macro:: PyVarObject_HEAD_INIT(type, size) |
Jeroen Ruigrok van der Werven | 939c178 | 2009-04-26 20:25:45 +0000 | [diff] [blame] | 160 | |
| 161 | This is a macro which expands to initialization values for a new |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 162 | :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] | 163 | This macro expands to:: |
| 164 | |
| 165 | _PyObject_EXTRA_INIT |
| 166 | 1, type, size, |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 167 | |
| 168 | |
Jeroen Demeyer | 9669931 | 2019-09-10 12:41:59 +0200 | [diff] [blame] | 169 | Implementing functions and methods |
| 170 | ---------------------------------- |
| 171 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 172 | .. c:type:: PyCFunction |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 173 | |
Jeroen Ruigrok van der Werven | f4a9f96 | 2009-04-26 20:21:12 +0000 | [diff] [blame] | 174 | Type of the functions used to implement most Python callables in C. |
Victor Stinner | 474652f | 2020-08-13 22:11:50 +0200 | [diff] [blame] | 175 | Functions of this type take two :c:type:`PyObject*` parameters and return |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 176 | one such value. If the return value is ``NULL``, an exception shall have |
| 177 | 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] | 178 | value of the function as exposed in Python. The function must return a new |
| 179 | reference. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 180 | |
Petr Viktorin | e1becf4 | 2020-05-07 15:39:59 +0200 | [diff] [blame] | 181 | The function signature is:: |
| 182 | |
| 183 | PyObject *PyCFunction(PyObject *self, |
Hai Shi | c068b53 | 2020-05-08 01:16:01 +0800 | [diff] [blame] | 184 | PyObject *args); |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 185 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 186 | .. c:type:: PyCFunctionWithKeywords |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 187 | |
Jeroen Demeyer | 5600b5e | 2019-06-16 19:03:23 +0200 | [diff] [blame] | 188 | Type of the functions used to implement Python callables in C |
| 189 | with signature :const:`METH_VARARGS | METH_KEYWORDS`. |
Petr Viktorin | e1becf4 | 2020-05-07 15:39:59 +0200 | [diff] [blame] | 190 | The function signature is:: |
| 191 | |
| 192 | PyObject *PyCFunctionWithKeywords(PyObject *self, |
Hai Shi | c068b53 | 2020-05-08 01:16:01 +0800 | [diff] [blame] | 193 | PyObject *args, |
Petr Viktorin | e1becf4 | 2020-05-07 15:39:59 +0200 | [diff] [blame] | 194 | PyObject *kwargs); |
Jeroen Demeyer | 5600b5e | 2019-06-16 19:03:23 +0200 | [diff] [blame] | 195 | |
| 196 | |
| 197 | .. c:type:: _PyCFunctionFast |
| 198 | |
| 199 | Type of the functions used to implement Python callables in C |
| 200 | with signature :const:`METH_FASTCALL`. |
Petr Viktorin | e1becf4 | 2020-05-07 15:39:59 +0200 | [diff] [blame] | 201 | The function signature is:: |
Jeroen Demeyer | 5600b5e | 2019-06-16 19:03:23 +0200 | [diff] [blame] | 202 | |
Petr Viktorin | e1becf4 | 2020-05-07 15:39:59 +0200 | [diff] [blame] | 203 | PyObject *_PyCFunctionFast(PyObject *self, |
| 204 | PyObject *const *args, |
| 205 | Py_ssize_t nargs); |
Jeroen Demeyer | 5600b5e | 2019-06-16 19:03:23 +0200 | [diff] [blame] | 206 | |
| 207 | .. c:type:: _PyCFunctionFastWithKeywords |
| 208 | |
| 209 | Type of the functions used to implement Python callables in C |
| 210 | with signature :const:`METH_FASTCALL | METH_KEYWORDS`. |
Petr Viktorin | e1becf4 | 2020-05-07 15:39:59 +0200 | [diff] [blame] | 211 | The function signature is:: |
| 212 | |
| 213 | PyObject *_PyCFunctionFastWithKeywords(PyObject *self, |
| 214 | PyObject *const *args, |
| 215 | Py_ssize_t nargs, |
| 216 | PyObject *kwnames); |
| 217 | |
| 218 | .. c:type:: PyCMethod |
| 219 | |
| 220 | Type of the functions used to implement Python callables in C |
| 221 | with signature :const:`METH_METHOD | METH_FASTCALL | METH_KEYWORDS`. |
| 222 | The function signature is:: |
| 223 | |
| 224 | PyObject *PyCMethod(PyObject *self, |
| 225 | PyTypeObject *defining_class, |
| 226 | PyObject *const *args, |
| 227 | Py_ssize_t nargs, |
| 228 | PyObject *kwnames) |
| 229 | |
| 230 | .. versionadded:: 3.9 |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 231 | |
| 232 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 233 | .. c:type:: PyMethodDef |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 234 | |
| 235 | Structure used to describe a method of an extension type. This structure has |
| 236 | four fields: |
| 237 | |
Serhiy Storchaka | 84b8e92 | 2017-03-30 10:01:03 +0300 | [diff] [blame] | 238 | +------------------+---------------+-------------------------------+ |
| 239 | | Field | C Type | Meaning | |
| 240 | +==================+===============+===============================+ |
| 241 | | :attr:`ml_name` | const char \* | name of the method | |
| 242 | +------------------+---------------+-------------------------------+ |
| 243 | | :attr:`ml_meth` | PyCFunction | pointer to the C | |
| 244 | | | | implementation | |
| 245 | +------------------+---------------+-------------------------------+ |
| 246 | | :attr:`ml_flags` | int | flag bits indicating how the | |
| 247 | | | | call should be constructed | |
| 248 | +------------------+---------------+-------------------------------+ |
| 249 | | :attr:`ml_doc` | const char \* | points to the contents of the | |
| 250 | | | | docstring | |
| 251 | +------------------+---------------+-------------------------------+ |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 252 | |
| 253 | The :attr:`ml_meth` is a C function pointer. The functions may be of different |
Victor Stinner | 474652f | 2020-08-13 22:11:50 +0200 | [diff] [blame] | 254 | types, but they always return :c:type:`PyObject*`. If the function is not of |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 255 | the :c:type:`PyCFunction`, the compiler will require a cast in the method table. |
| 256 | Even though :c:type:`PyCFunction` defines the first parameter as |
Victor Stinner | 474652f | 2020-08-13 22:11:50 +0200 | [diff] [blame] | 257 | :c:type:`PyObject*`, it is common that the method implementation uses the |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 258 | specific C type of the *self* object. |
| 259 | |
| 260 | The :attr:`ml_flags` field is a bitfield which can include the following flags. |
| 261 | The individual flags indicate either a calling convention or a binding |
Jeroen Demeyer | 5600b5e | 2019-06-16 19:03:23 +0200 | [diff] [blame] | 262 | convention. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 263 | |
Petr Viktorin | e1becf4 | 2020-05-07 15:39:59 +0200 | [diff] [blame] | 264 | There are these calling conventions: |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 265 | |
| 266 | .. data:: METH_VARARGS |
| 267 | |
| 268 | This is the typical calling convention, where the methods have the type |
Victor Stinner | 474652f | 2020-08-13 22:11:50 +0200 | [diff] [blame] | 269 | :c:type:`PyCFunction`. The function expects two :c:type:`PyObject*` values. |
Georg Brandl | 21dc5ba | 2009-07-11 10:43:08 +0000 | [diff] [blame] | 270 | The first one is the *self* object for methods; for module functions, it is |
| 271 | the module object. The second parameter (often called *args*) is a tuple |
| 272 | object representing all arguments. This parameter is typically processed |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 273 | using :c:func:`PyArg_ParseTuple` or :c:func:`PyArg_UnpackTuple`. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 274 | |
| 275 | |
Jeroen Demeyer | 5600b5e | 2019-06-16 19:03:23 +0200 | [diff] [blame] | 276 | .. data:: METH_VARARGS | METH_KEYWORDS |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 277 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 278 | Methods with these flags must be of type :c:type:`PyCFunctionWithKeywords`. |
Jeroen Demeyer | 5600b5e | 2019-06-16 19:03:23 +0200 | [diff] [blame] | 279 | The function expects three parameters: *self*, *args*, *kwargs* where |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 280 | *kwargs* is a dictionary of all the keyword arguments or possibly ``NULL`` |
Jeroen Demeyer | 5600b5e | 2019-06-16 19:03:23 +0200 | [diff] [blame] | 281 | if there are no keyword arguments. The parameters are typically processed |
| 282 | using :c:func:`PyArg_ParseTupleAndKeywords`. |
| 283 | |
| 284 | |
| 285 | .. data:: METH_FASTCALL |
| 286 | |
| 287 | Fast calling convention supporting only positional arguments. |
| 288 | The methods have the type :c:type:`_PyCFunctionFast`. |
| 289 | The first parameter is *self*, the second parameter is a C array |
Victor Stinner | 474652f | 2020-08-13 22:11:50 +0200 | [diff] [blame] | 290 | of :c:type:`PyObject*` values indicating the arguments and the third |
Jeroen Demeyer | 5600b5e | 2019-06-16 19:03:23 +0200 | [diff] [blame] | 291 | parameter is the number of arguments (the length of the array). |
| 292 | |
Jeroen Demeyer | 5600b5e | 2019-06-16 19:03:23 +0200 | [diff] [blame] | 293 | .. versionadded:: 3.7 |
| 294 | |
Petr Viktorin | 0b9c4c6 | 2020-11-10 14:47:31 +0100 | [diff] [blame] | 295 | .. versionchanged:: 3.10 |
| 296 | |
| 297 | ``METH_FASTCALL`` is now part of the stable ABI. |
| 298 | |
Jeroen Demeyer | 5600b5e | 2019-06-16 19:03:23 +0200 | [diff] [blame] | 299 | |
| 300 | .. data:: METH_FASTCALL | METH_KEYWORDS |
| 301 | |
| 302 | Extension of :const:`METH_FASTCALL` supporting also keyword arguments, |
| 303 | with methods of type :c:type:`_PyCFunctionFastWithKeywords`. |
Jeroen Demeyer | 9a13a38 | 2019-11-12 14:08:00 +0100 | [diff] [blame] | 304 | Keyword arguments are passed the same way as in the |
| 305 | :ref:`vectorcall protocol <vectorcall>`: |
Victor Stinner | 474652f | 2020-08-13 22:11:50 +0200 | [diff] [blame] | 306 | there is an additional fourth :c:type:`PyObject*` parameter |
Jeroen Demeyer | 5600b5e | 2019-06-16 19:03:23 +0200 | [diff] [blame] | 307 | which is a tuple representing the names of the keyword arguments |
Jeroen Demeyer | 0567786 | 2019-08-16 12:41:27 +0200 | [diff] [blame] | 308 | (which are guaranteed to be strings) |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 309 | 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] | 310 | arguments are stored in the *args* array, after the positional arguments. |
| 311 | |
| 312 | This is not part of the :ref:`limited API <stable>`. |
| 313 | |
| 314 | .. versionadded:: 3.7 |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 315 | |
| 316 | |
Petr Viktorin | e1becf4 | 2020-05-07 15:39:59 +0200 | [diff] [blame] | 317 | .. data:: METH_METHOD | METH_FASTCALL | METH_KEYWORDS |
| 318 | |
| 319 | Extension of :const:`METH_FASTCALL | METH_KEYWORDS` supporting the *defining |
| 320 | class*, that is, the class that contains the method in question. |
| 321 | The defining class might be a superclass of ``Py_TYPE(self)``. |
| 322 | |
| 323 | The method needs to be of type :c:type:`PyCMethod`, the same as for |
| 324 | ``METH_FASTCALL | METH_KEYWORDS`` with ``defining_class`` argument added after |
| 325 | ``self``. |
| 326 | |
| 327 | .. versionadded:: 3.9 |
| 328 | |
| 329 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 330 | .. data:: METH_NOARGS |
| 331 | |
| 332 | Methods without parameters don't need to check whether arguments are given if |
| 333 | 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] | 334 | :c:type:`PyCFunction`. The first parameter is typically named *self* and will |
Georg Brandl | 21dc5ba | 2009-07-11 10:43:08 +0000 | [diff] [blame] | 335 | 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] | 336 | parameter will be ``NULL``. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 337 | |
| 338 | |
| 339 | .. data:: METH_O |
| 340 | |
| 341 | 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] | 342 | flag, instead of invoking :c:func:`PyArg_ParseTuple` with a ``"O"`` argument. |
| 343 | They have the type :c:type:`PyCFunction`, with the *self* parameter, and a |
Victor Stinner | 474652f | 2020-08-13 22:11:50 +0200 | [diff] [blame] | 344 | :c:type:`PyObject*` parameter representing the single argument. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 345 | |
| 346 | |
| 347 | These two constants are not used to indicate the calling convention but the |
| 348 | binding when use with methods of classes. These may not be used for functions |
| 349 | defined for modules. At most one of these flags may be set for any given |
| 350 | method. |
| 351 | |
| 352 | |
| 353 | .. data:: METH_CLASS |
| 354 | |
| 355 | .. index:: builtin: classmethod |
| 356 | |
Jeroen Ruigrok van der Werven | f4a9f96 | 2009-04-26 20:21:12 +0000 | [diff] [blame] | 357 | The method will be passed the type object as the first parameter rather |
| 358 | than an instance of the type. This is used to create *class methods*, |
| 359 | similar to what is created when using the :func:`classmethod` built-in |
| 360 | function. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 361 | |
| 362 | |
| 363 | .. data:: METH_STATIC |
| 364 | |
| 365 | .. index:: builtin: staticmethod |
| 366 | |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 367 | 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] | 368 | instance of the type. This is used to create *static methods*, similar to |
| 369 | what is created when using the :func:`staticmethod` built-in function. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 370 | |
| 371 | One other constant controls whether a method is loaded in place of another |
| 372 | definition with the same method name. |
| 373 | |
| 374 | |
| 375 | .. data:: METH_COEXIST |
| 376 | |
| 377 | The method will be loaded in place of existing definitions. Without |
| 378 | *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] | 379 | wrappers are loaded before the method table, the existence of a |
| 380 | *sq_contains* slot, for example, would generate a wrapped method named |
| 381 | :meth:`__contains__` and preclude the loading of a corresponding |
| 382 | PyCFunction with the same name. With the flag defined, the PyCFunction |
| 383 | will be loaded in place of the wrapper object and will co-exist with the |
| 384 | slot. This is helpful because calls to PyCFunctions are optimized more |
| 385 | than wrapper object calls. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 386 | |
Georg Brandl | 1f01deb | 2009-01-03 22:47:39 +0000 | [diff] [blame] | 387 | |
Jeroen Demeyer | 9669931 | 2019-09-10 12:41:59 +0200 | [diff] [blame] | 388 | Accessing attributes of extension types |
| 389 | --------------------------------------- |
| 390 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 391 | .. c:type:: PyMemberDef |
Georg Brandl | 1f01deb | 2009-01-03 22:47:39 +0000 | [diff] [blame] | 392 | |
| 393 | Structure which describes an attribute of a type which corresponds to a C |
| 394 | struct member. Its fields are: |
| 395 | |
Serhiy Storchaka | 84b8e92 | 2017-03-30 10:01:03 +0300 | [diff] [blame] | 396 | +------------------+---------------+-------------------------------+ |
| 397 | | Field | C Type | Meaning | |
| 398 | +==================+===============+===============================+ |
| 399 | | :attr:`name` | const char \* | name of the member | |
| 400 | +------------------+---------------+-------------------------------+ |
| 401 | | :attr:`!type` | int | the type of the member in the | |
| 402 | | | | C struct | |
| 403 | +------------------+---------------+-------------------------------+ |
| 404 | | :attr:`offset` | Py_ssize_t | the offset in bytes that the | |
| 405 | | | | member is located on the | |
| 406 | | | | type's object struct | |
| 407 | +------------------+---------------+-------------------------------+ |
| 408 | | :attr:`flags` | int | flag bits indicating if the | |
| 409 | | | | field should be read-only or | |
| 410 | | | | writable | |
| 411 | +------------------+---------------+-------------------------------+ |
| 412 | | :attr:`doc` | const char \* | points to the contents of the | |
| 413 | | | | docstring | |
| 414 | +------------------+---------------+-------------------------------+ |
Georg Brandl | 1f01deb | 2009-01-03 22:47:39 +0000 | [diff] [blame] | 415 | |
csabella | c3c7ef0 | 2017-03-29 20:27:50 -0400 | [diff] [blame] | 416 | :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] | 417 | types. When the member is accessed in Python, it will be converted to the |
| 418 | equivalent Python type. |
| 419 | |
| 420 | =============== ================== |
| 421 | Macro name C type |
| 422 | =============== ================== |
| 423 | T_SHORT short |
| 424 | T_INT int |
| 425 | T_LONG long |
| 426 | T_FLOAT float |
| 427 | T_DOUBLE double |
Serhiy Storchaka | 84b8e92 | 2017-03-30 10:01:03 +0300 | [diff] [blame] | 428 | T_STRING const char \* |
Georg Brandl | 1f01deb | 2009-01-03 22:47:39 +0000 | [diff] [blame] | 429 | T_OBJECT PyObject \* |
| 430 | T_OBJECT_EX PyObject \* |
| 431 | T_CHAR char |
| 432 | T_BYTE char |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 433 | T_UBYTE unsigned char |
Georg Brandl | 1f01deb | 2009-01-03 22:47:39 +0000 | [diff] [blame] | 434 | T_UINT unsigned int |
| 435 | T_USHORT unsigned short |
| 436 | T_ULONG unsigned long |
| 437 | T_BOOL char |
| 438 | T_LONGLONG long long |
| 439 | T_ULONGLONG unsigned long long |
| 440 | T_PYSSIZET Py_ssize_t |
| 441 | =============== ================== |
| 442 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 443 | :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] | 444 | :c:macro:`T_OBJECT` returns ``None`` if the member is ``NULL`` and |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 445 | :c:macro:`T_OBJECT_EX` raises an :exc:`AttributeError`. Try to use |
| 446 | :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] | 447 | handles use of the :keyword:`del` statement on that attribute more correctly |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 448 | than :c:macro:`T_OBJECT`. |
Georg Brandl | 1f01deb | 2009-01-03 22:47:39 +0000 | [diff] [blame] | 449 | |
Serhiy Storchaka | 1ecf7d2 | 2016-10-27 21:41:19 +0300 | [diff] [blame] | 450 | :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] | 451 | read-only access. Using :c:macro:`T_STRING` for :attr:`type` implies |
Windson yang | 689d555 | 2018-11-18 03:16:51 +0800 | [diff] [blame] | 452 | :c:macro:`READONLY`. :c:macro:`T_STRING` data is interpreted as UTF-8. |
| 453 | Only :c:macro:`T_OBJECT` and :c:macro:`T_OBJECT_EX` |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 454 | members can be deleted. (They are set to ``NULL``). |
Michael Seifert | da67e0d | 2017-09-15 18:25:27 +0200 | [diff] [blame] | 455 | |
Petr Viktorin | 468f8a6 | 2019-09-25 13:06:16 +0200 | [diff] [blame] | 456 | .. _pymemberdef-offsets: |
| 457 | |
| 458 | Heap allocated types (created using :c:func:`PyType_FromSpec` or similar), |
Gurupad Hegde | 6c7bb38 | 2019-12-28 17:16:02 -0500 | [diff] [blame] | 459 | ``PyMemberDef`` may contain definitions for the special members |
Hai Shi | 86d6944 | 2020-05-12 05:38:55 +0800 | [diff] [blame] | 460 | ``__dictoffset__``, ``__weaklistoffset__`` and ``__vectorcalloffset__``, |
| 461 | corresponding to |
| 462 | :c:member:`~PyTypeObject.tp_dictoffset`, |
| 463 | :c:member:`~PyTypeObject.tp_weaklistoffset` and |
| 464 | :c:member:`~PyTypeObject.tp_vectorcall_offset` in type objects. |
Petr Viktorin | 468f8a6 | 2019-09-25 13:06:16 +0200 | [diff] [blame] | 465 | These must be defined with ``T_PYSSIZET`` and ``READONLY``, for example:: |
| 466 | |
| 467 | static PyMemberDef spam_type_members[] = { |
| 468 | {"__dictoffset__", T_PYSSIZET, offsetof(Spam_object, dict), READONLY}, |
| 469 | {NULL} /* Sentinel */ |
| 470 | }; |
Michael Seifert | da67e0d | 2017-09-15 18:25:27 +0200 | [diff] [blame] | 471 | |
| 472 | .. c:type:: PyGetSetDef |
| 473 | |
| 474 | Structure to define property-like access for a type. See also description of |
| 475 | the :c:member:`PyTypeObject.tp_getset` slot. |
| 476 | |
| 477 | +-------------+------------------+-----------------------------------+ |
| 478 | | Field | C Type | Meaning | |
| 479 | +=============+==================+===================================+ |
| 480 | | name | const char \* | attribute name | |
| 481 | +-------------+------------------+-----------------------------------+ |
| 482 | | get | getter | C Function to get the attribute | |
| 483 | +-------------+------------------+-----------------------------------+ |
| 484 | | set | setter | optional C function to set or | |
| 485 | | | | delete the attribute, if omitted | |
| 486 | | | | the attribute is readonly | |
| 487 | +-------------+------------------+-----------------------------------+ |
| 488 | | doc | const char \* | optional docstring | |
| 489 | +-------------+------------------+-----------------------------------+ |
| 490 | | closure | void \* | optional function pointer, | |
| 491 | | | | providing additional data for | |
| 492 | | | | getter and setter | |
| 493 | +-------------+------------------+-----------------------------------+ |
| 494 | |
Victor Stinner | 474652f | 2020-08-13 22:11:50 +0200 | [diff] [blame] | 495 | The ``get`` function takes one :c:type:`PyObject*` parameter (the |
Michael Seifert | da67e0d | 2017-09-15 18:25:27 +0200 | [diff] [blame] | 496 | instance) and a function pointer (the associated ``closure``):: |
| 497 | |
| 498 | typedef PyObject *(*getter)(PyObject *, void *); |
| 499 | |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 500 | 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] | 501 | on failure. |
| 502 | |
Victor Stinner | 474652f | 2020-08-13 22:11:50 +0200 | [diff] [blame] | 503 | ``set`` functions take two :c:type:`PyObject*` parameters (the instance and |
Michael Seifert | da67e0d | 2017-09-15 18:25:27 +0200 | [diff] [blame] | 504 | the value to be set) and a function pointer (the associated ``closure``):: |
| 505 | |
| 506 | typedef int (*setter)(PyObject *, PyObject *, void *); |
| 507 | |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 508 | In case the attribute should be deleted the second parameter is ``NULL``. |
Michael Seifert | da67e0d | 2017-09-15 18:25:27 +0200 | [diff] [blame] | 509 | Should return ``0`` on success or ``-1`` with a set exception on failure. |