blob: 37602ed5b4dc789c6aadaeccccd302c82c8a6fb7 [file] [log] [blame]
Stéphane Wirtelcbb64842019-05-17 11:55:34 +02001.. highlight:: c
Georg Brandl54a3faa2008-01-20 09:30:57 +00002
3.. _common-structs:
4
5Common Object Structures
6========================
7
8There are a large number of structures which are used in the definition of
9object types for Python. This section describes these structures and how they
10are used.
11
Jeroen Demeyer96699312019-09-10 12:41:59 +020012
13Base object types and macros
14----------------------------
15
Jeroen Ruigrok van der Wervenf4a9f962009-04-26 20:21:12 +000016All Python objects ultimately share a small number of fields at the beginning
17of the object's representation in memory. These are represented by the
Georg Brandl60203b42010-10-06 10:11:56 +000018:c:type:`PyObject` and :c:type:`PyVarObject` types, which are defined, in turn,
Jeroen Ruigrok van der Wervenf4a9f962009-04-26 20:21:12 +000019by the expansions of some macros also used, whether directly or indirectly, in
20the definition of all other Python objects.
Georg Brandl54a3faa2008-01-20 09:30:57 +000021
22
Georg Brandl60203b42010-10-06 10:11:56 +000023.. c:type:: PyObject
Georg Brandl54a3faa2008-01-20 09:30:57 +000024
Jeroen Ruigrok van der Wervenf4a9f962009-04-26 20:21:12 +000025 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. Smith1b244652015-04-14 11:12:53 -070028 reference count and a pointer to the corresponding type object.
Gregory P. Smith0f2f3bc2015-04-14 11:21:05 -070029 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. Smith1b244652015-04-14 11:12:53 -070032 :c:macro:`Py_TYPE`.
Georg Brandl54a3faa2008-01-20 09:30:57 +000033
34
Georg Brandl60203b42010-10-06 10:11:56 +000035.. c:type:: PyVarObject
Georg Brandl54a3faa2008-01-20 09:30:57 +000036
Georg Brandl60203b42010-10-06 10:11:56 +000037 This is an extension of :c:type:`PyObject` that adds the :attr:`ob_size`
Jeroen Ruigrok van der Wervenf4a9f962009-04-26 20:21:12 +000038 field. This is only used for objects that have some notion of *length*.
Gregory P. Smith1b244652015-04-14 11:12:53 -070039 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 Brandl54a3faa2008-01-20 09:30:57 +000042
Georg Brandl54a3faa2008-01-20 09:30:57 +000043
Georg Brandl60203b42010-10-06 10:11:56 +000044.. c:macro:: PyObject_HEAD
Georg Brandl54a3faa2008-01-20 09:30:57 +000045
Gregory P. Smith1b244652015-04-14 11:12:53 -070046 This is a macro used when declaring new types which represent objects
47 without a varying length. The PyObject_HEAD macro expands to::
Georg Brandl54a3faa2008-01-20 09:30:57 +000048
Gregory P. Smith1b244652015-04-14 11:12:53 -070049 PyObject ob_base;
Georg Brandl54a3faa2008-01-20 09:30:57 +000050
Zachary Ware5c676f62015-07-06 23:27:15 -050051 See documentation of :c:type:`PyObject` above.
Georg Brandl54a3faa2008-01-20 09:30:57 +000052
53
Georg Brandl60203b42010-10-06 10:11:56 +000054.. c:macro:: PyObject_VAR_HEAD
Georg Brandl54a3faa2008-01-20 09:30:57 +000055
Gregory P. Smith1b244652015-04-14 11:12:53 -070056 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 Brandl54a3faa2008-01-20 09:30:57 +000059
Gregory P. Smith1b244652015-04-14 11:12:53 -070060 PyVarObject ob_base;
Georg Brandl54a3faa2008-01-20 09:30:57 +000061
Gregory P. Smith1b244652015-04-14 11:12:53 -070062 See documentation of :c:type:`PyVarObject` above.
63
64
Dong-hee Naad3252b2020-05-26 01:52:54 +090065.. c:function:: PyTypeObject* Py_TYPE(const PyObject *o)
Gregory P. Smith1b244652015-04-14 11:12:53 -070066
Dong-hee Naad3252b2020-05-26 01:52:54 +090067 Get the type of the Python object *o*.
Gregory P. Smith1b244652015-04-14 11:12:53 -070068
Victor Stinner23c5f932020-11-09 13:40:47 +010069 Return a :term:`borrowed reference`.
Dong-hee Naad3252b2020-05-26 01:52:54 +090070
71 .. versionchanged:: 3.10
72 :c:func:`Py_TYPE()` is changed to the inline static function.
73 Use :c:func:`Py_SET_TYPE()` to set an object type.
Gregory P. Smith1b244652015-04-14 11:12:53 -070074
75
Dong-hee Nad905df72020-02-14 02:37:17 +090076.. c:function:: int Py_IS_TYPE(PyObject *o, PyTypeObject *type)
77
78 Return non-zero if the object *o* type is *type*. Return zero otherwise.
79 Equivalent to: ``Py_TYPE(o) == type``.
80
81 .. versionadded:: 3.9
82
83
Victor Stinnerd2ec81a2020-02-07 09:17:07 +010084.. c:function:: void Py_SET_TYPE(PyObject *o, PyTypeObject *type)
85
86 Set the object *o* type to *type*.
87
88 .. versionadded:: 3.9
89
90
Victor Stinnerfe2978b2020-05-27 14:55:10 +020091.. c:function:: Py_ssize_t Py_REFCNT(const PyObject *o)
Gregory P. Smith1b244652015-04-14 11:12:53 -070092
Victor Stinnerfe2978b2020-05-27 14:55:10 +020093 Get the reference count of the Python object *o*.
Gregory P. Smith1b244652015-04-14 11:12:53 -070094
Victor Stinnerfe2978b2020-05-27 14:55:10 +020095 .. versionchanged:: 3.10
96 :c:func:`Py_REFCNT()` is changed to the inline static function.
97 Use :c:func:`Py_SET_REFCNT()` to set an object reference count.
Gregory P. Smith1b244652015-04-14 11:12:53 -070098
99
Victor Stinnerc86a1122020-02-07 01:24:29 +0100100.. c:function:: void Py_SET_REFCNT(PyObject *o, Py_ssize_t refcnt)
101
102 Set the object *o* reference counter to *refcnt*.
103
104 .. versionadded:: 3.9
105
106
Victor Stinnerfe2978b2020-05-27 14:55:10 +0200107.. c:function:: Py_ssize_t Py_SIZE(const PyVarObject *o)
Gregory P. Smith1b244652015-04-14 11:12:53 -0700108
Victor Stinnerfe2978b2020-05-27 14:55:10 +0200109 Get the size of the Python object *o*.
Gregory P. Smith1b244652015-04-14 11:12:53 -0700110
Victor Stinnerfe2978b2020-05-27 14:55:10 +0200111 .. versionchanged:: 3.10
112 :c:func:`Py_SIZE()` is changed to the inline static function.
113 Use :c:func:`Py_SET_SIZE()` to set an object size.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000114
Jeroen Ruigrok van der Werven939c1782009-04-26 20:25:45 +0000115
Victor Stinnerb10dc3e2020-02-07 12:05:12 +0100116.. c:function:: void Py_SET_SIZE(PyVarObject *o, Py_ssize_t size)
117
Brandt Bucher968dcd92020-02-13 09:34:45 -0800118 Set the object *o* size to *size*.
Victor Stinnerb10dc3e2020-02-07 12:05:12 +0100119
120 .. versionadded:: 3.9
121
122
Georg Brandl60203b42010-10-06 10:11:56 +0000123.. c:macro:: PyObject_HEAD_INIT(type)
Jeroen Ruigrok van der Werven939c1782009-04-26 20:25:45 +0000124
125 This is a macro which expands to initialization values for a new
Georg Brandl60203b42010-10-06 10:11:56 +0000126 :c:type:`PyObject` type. This macro expands to::
Jeroen Ruigrok van der Werven939c1782009-04-26 20:25:45 +0000127
128 _PyObject_EXTRA_INIT
129 1, type,
130
131
Georg Brandl60203b42010-10-06 10:11:56 +0000132.. c:macro:: PyVarObject_HEAD_INIT(type, size)
Jeroen Ruigrok van der Werven939c1782009-04-26 20:25:45 +0000133
134 This is a macro which expands to initialization values for a new
Georg Brandl60203b42010-10-06 10:11:56 +0000135 :c:type:`PyVarObject` type, including the :attr:`ob_size` field.
Jeroen Ruigrok van der Werven939c1782009-04-26 20:25:45 +0000136 This macro expands to::
137
138 _PyObject_EXTRA_INIT
139 1, type, size,
Georg Brandl54a3faa2008-01-20 09:30:57 +0000140
141
Jeroen Demeyer96699312019-09-10 12:41:59 +0200142Implementing functions and methods
143----------------------------------
144
Georg Brandl60203b42010-10-06 10:11:56 +0000145.. c:type:: PyCFunction
Georg Brandl54a3faa2008-01-20 09:30:57 +0000146
Jeroen Ruigrok van der Wervenf4a9f962009-04-26 20:21:12 +0000147 Type of the functions used to implement most Python callables in C.
Victor Stinner474652f2020-08-13 22:11:50 +0200148 Functions of this type take two :c:type:`PyObject*` parameters and return
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200149 one such value. If the return value is ``NULL``, an exception shall have
150 been set. If not ``NULL``, the return value is interpreted as the return
Jeroen Ruigrok van der Wervenf4a9f962009-04-26 20:21:12 +0000151 value of the function as exposed in Python. The function must return a new
152 reference.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000153
Petr Viktorine1becf42020-05-07 15:39:59 +0200154 The function signature is::
155
156 PyObject *PyCFunction(PyObject *self,
Hai Shic068b532020-05-08 01:16:01 +0800157 PyObject *args);
Georg Brandl54a3faa2008-01-20 09:30:57 +0000158
Georg Brandl60203b42010-10-06 10:11:56 +0000159.. c:type:: PyCFunctionWithKeywords
Georg Brandl54a3faa2008-01-20 09:30:57 +0000160
Jeroen Demeyer5600b5e2019-06-16 19:03:23 +0200161 Type of the functions used to implement Python callables in C
162 with signature :const:`METH_VARARGS | METH_KEYWORDS`.
Petr Viktorine1becf42020-05-07 15:39:59 +0200163 The function signature is::
164
165 PyObject *PyCFunctionWithKeywords(PyObject *self,
Hai Shic068b532020-05-08 01:16:01 +0800166 PyObject *args,
Petr Viktorine1becf42020-05-07 15:39:59 +0200167 PyObject *kwargs);
Jeroen Demeyer5600b5e2019-06-16 19:03:23 +0200168
169
170.. c:type:: _PyCFunctionFast
171
172 Type of the functions used to implement Python callables in C
173 with signature :const:`METH_FASTCALL`.
Petr Viktorine1becf42020-05-07 15:39:59 +0200174 The function signature is::
Jeroen Demeyer5600b5e2019-06-16 19:03:23 +0200175
Petr Viktorine1becf42020-05-07 15:39:59 +0200176 PyObject *_PyCFunctionFast(PyObject *self,
177 PyObject *const *args,
178 Py_ssize_t nargs);
Jeroen Demeyer5600b5e2019-06-16 19:03:23 +0200179
180.. c:type:: _PyCFunctionFastWithKeywords
181
182 Type of the functions used to implement Python callables in C
183 with signature :const:`METH_FASTCALL | METH_KEYWORDS`.
Petr Viktorine1becf42020-05-07 15:39:59 +0200184 The function signature is::
185
186 PyObject *_PyCFunctionFastWithKeywords(PyObject *self,
187 PyObject *const *args,
188 Py_ssize_t nargs,
189 PyObject *kwnames);
190
191.. c:type:: PyCMethod
192
193 Type of the functions used to implement Python callables in C
194 with signature :const:`METH_METHOD | METH_FASTCALL | METH_KEYWORDS`.
195 The function signature is::
196
197 PyObject *PyCMethod(PyObject *self,
198 PyTypeObject *defining_class,
199 PyObject *const *args,
200 Py_ssize_t nargs,
201 PyObject *kwnames)
202
203 .. versionadded:: 3.9
Georg Brandl54a3faa2008-01-20 09:30:57 +0000204
205
Georg Brandl60203b42010-10-06 10:11:56 +0000206.. c:type:: PyMethodDef
Georg Brandl54a3faa2008-01-20 09:30:57 +0000207
208 Structure used to describe a method of an extension type. This structure has
209 four fields:
210
Serhiy Storchaka84b8e922017-03-30 10:01:03 +0300211 +------------------+---------------+-------------------------------+
212 | Field | C Type | Meaning |
213 +==================+===============+===============================+
214 | :attr:`ml_name` | const char \* | name of the method |
215 +------------------+---------------+-------------------------------+
216 | :attr:`ml_meth` | PyCFunction | pointer to the C |
217 | | | implementation |
218 +------------------+---------------+-------------------------------+
219 | :attr:`ml_flags` | int | flag bits indicating how the |
220 | | | call should be constructed |
221 +------------------+---------------+-------------------------------+
222 | :attr:`ml_doc` | const char \* | points to the contents of the |
223 | | | docstring |
224 +------------------+---------------+-------------------------------+
Georg Brandl54a3faa2008-01-20 09:30:57 +0000225
226The :attr:`ml_meth` is a C function pointer. The functions may be of different
Victor Stinner474652f2020-08-13 22:11:50 +0200227types, but they always return :c:type:`PyObject*`. If the function is not of
Georg Brandl60203b42010-10-06 10:11:56 +0000228the :c:type:`PyCFunction`, the compiler will require a cast in the method table.
229Even though :c:type:`PyCFunction` defines the first parameter as
Victor Stinner474652f2020-08-13 22:11:50 +0200230:c:type:`PyObject*`, it is common that the method implementation uses the
Georg Brandl54a3faa2008-01-20 09:30:57 +0000231specific C type of the *self* object.
232
233The :attr:`ml_flags` field is a bitfield which can include the following flags.
234The individual flags indicate either a calling convention or a binding
Jeroen Demeyer5600b5e2019-06-16 19:03:23 +0200235convention.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000236
Petr Viktorine1becf42020-05-07 15:39:59 +0200237There are these calling conventions:
Georg Brandl54a3faa2008-01-20 09:30:57 +0000238
239.. data:: METH_VARARGS
240
241 This is the typical calling convention, where the methods have the type
Victor Stinner474652f2020-08-13 22:11:50 +0200242 :c:type:`PyCFunction`. The function expects two :c:type:`PyObject*` values.
Georg Brandl21dc5ba2009-07-11 10:43:08 +0000243 The first one is the *self* object for methods; for module functions, it is
244 the module object. The second parameter (often called *args*) is a tuple
245 object representing all arguments. This parameter is typically processed
Georg Brandl60203b42010-10-06 10:11:56 +0000246 using :c:func:`PyArg_ParseTuple` or :c:func:`PyArg_UnpackTuple`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000247
248
Jeroen Demeyer5600b5e2019-06-16 19:03:23 +0200249.. data:: METH_VARARGS | METH_KEYWORDS
Georg Brandl54a3faa2008-01-20 09:30:57 +0000250
Georg Brandl60203b42010-10-06 10:11:56 +0000251 Methods with these flags must be of type :c:type:`PyCFunctionWithKeywords`.
Jeroen Demeyer5600b5e2019-06-16 19:03:23 +0200252 The function expects three parameters: *self*, *args*, *kwargs* where
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200253 *kwargs* is a dictionary of all the keyword arguments or possibly ``NULL``
Jeroen Demeyer5600b5e2019-06-16 19:03:23 +0200254 if there are no keyword arguments. The parameters are typically processed
255 using :c:func:`PyArg_ParseTupleAndKeywords`.
256
257
258.. data:: METH_FASTCALL
259
260 Fast calling convention supporting only positional arguments.
261 The methods have the type :c:type:`_PyCFunctionFast`.
262 The first parameter is *self*, the second parameter is a C array
Victor Stinner474652f2020-08-13 22:11:50 +0200263 of :c:type:`PyObject*` values indicating the arguments and the third
Jeroen Demeyer5600b5e2019-06-16 19:03:23 +0200264 parameter is the number of arguments (the length of the array).
265
Jeroen Demeyer5600b5e2019-06-16 19:03:23 +0200266 .. versionadded:: 3.7
267
Petr Viktorin0b9c4c62020-11-10 14:47:31 +0100268 .. versionchanged:: 3.10
269
270 ``METH_FASTCALL`` is now part of the stable ABI.
271
Jeroen Demeyer5600b5e2019-06-16 19:03:23 +0200272
273.. data:: METH_FASTCALL | METH_KEYWORDS
274
275 Extension of :const:`METH_FASTCALL` supporting also keyword arguments,
276 with methods of type :c:type:`_PyCFunctionFastWithKeywords`.
Jeroen Demeyer9a13a382019-11-12 14:08:00 +0100277 Keyword arguments are passed the same way as in the
278 :ref:`vectorcall protocol <vectorcall>`:
Victor Stinner474652f2020-08-13 22:11:50 +0200279 there is an additional fourth :c:type:`PyObject*` parameter
Jeroen Demeyer5600b5e2019-06-16 19:03:23 +0200280 which is a tuple representing the names of the keyword arguments
Jeroen Demeyer05677862019-08-16 12:41:27 +0200281 (which are guaranteed to be strings)
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200282 or possibly ``NULL`` if there are no keywords. The values of the keyword
Jeroen Demeyer5600b5e2019-06-16 19:03:23 +0200283 arguments are stored in the *args* array, after the positional arguments.
284
285 This is not part of the :ref:`limited API <stable>`.
286
287 .. versionadded:: 3.7
Georg Brandl54a3faa2008-01-20 09:30:57 +0000288
289
Petr Viktorine1becf42020-05-07 15:39:59 +0200290.. data:: METH_METHOD | METH_FASTCALL | METH_KEYWORDS
291
292 Extension of :const:`METH_FASTCALL | METH_KEYWORDS` supporting the *defining
293 class*, that is, the class that contains the method in question.
294 The defining class might be a superclass of ``Py_TYPE(self)``.
295
296 The method needs to be of type :c:type:`PyCMethod`, the same as for
297 ``METH_FASTCALL | METH_KEYWORDS`` with ``defining_class`` argument added after
298 ``self``.
299
300 .. versionadded:: 3.9
301
302
Georg Brandl54a3faa2008-01-20 09:30:57 +0000303.. data:: METH_NOARGS
304
305 Methods without parameters don't need to check whether arguments are given if
306 they are listed with the :const:`METH_NOARGS` flag. They need to be of type
Georg Brandl60203b42010-10-06 10:11:56 +0000307 :c:type:`PyCFunction`. The first parameter is typically named *self* and will
Georg Brandl21dc5ba2009-07-11 10:43:08 +0000308 hold a reference to the module or object instance. In all cases the second
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200309 parameter will be ``NULL``.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000310
311
312.. data:: METH_O
313
314 Methods with a single object argument can be listed with the :const:`METH_O`
Georg Brandl60203b42010-10-06 10:11:56 +0000315 flag, instead of invoking :c:func:`PyArg_ParseTuple` with a ``"O"`` argument.
316 They have the type :c:type:`PyCFunction`, with the *self* parameter, and a
Victor Stinner474652f2020-08-13 22:11:50 +0200317 :c:type:`PyObject*` parameter representing the single argument.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000318
319
320These two constants are not used to indicate the calling convention but the
321binding when use with methods of classes. These may not be used for functions
322defined for modules. At most one of these flags may be set for any given
323method.
324
325
326.. data:: METH_CLASS
327
328 .. index:: builtin: classmethod
329
Jeroen Ruigrok van der Wervenf4a9f962009-04-26 20:21:12 +0000330 The method will be passed the type object as the first parameter rather
331 than an instance of the type. This is used to create *class methods*,
332 similar to what is created when using the :func:`classmethod` built-in
333 function.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000334
335
336.. data:: METH_STATIC
337
338 .. index:: builtin: staticmethod
339
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200340 The method will be passed ``NULL`` as the first parameter rather than an
Jeroen Ruigrok van der Wervenf4a9f962009-04-26 20:21:12 +0000341 instance of the type. This is used to create *static methods*, similar to
342 what is created when using the :func:`staticmethod` built-in function.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000343
344One other constant controls whether a method is loaded in place of another
345definition with the same method name.
346
347
348.. data:: METH_COEXIST
349
350 The method will be loaded in place of existing definitions. Without
351 *METH_COEXIST*, the default is to skip repeated definitions. Since slot
Jeroen Ruigrok van der Wervenf4a9f962009-04-26 20:21:12 +0000352 wrappers are loaded before the method table, the existence of a
353 *sq_contains* slot, for example, would generate a wrapped method named
354 :meth:`__contains__` and preclude the loading of a corresponding
355 PyCFunction with the same name. With the flag defined, the PyCFunction
356 will be loaded in place of the wrapper object and will co-exist with the
357 slot. This is helpful because calls to PyCFunctions are optimized more
358 than wrapper object calls.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000359
Georg Brandl1f01deb2009-01-03 22:47:39 +0000360
Jeroen Demeyer96699312019-09-10 12:41:59 +0200361Accessing attributes of extension types
362---------------------------------------
363
Georg Brandl60203b42010-10-06 10:11:56 +0000364.. c:type:: PyMemberDef
Georg Brandl1f01deb2009-01-03 22:47:39 +0000365
366 Structure which describes an attribute of a type which corresponds to a C
367 struct member. Its fields are:
368
Serhiy Storchaka84b8e922017-03-30 10:01:03 +0300369 +------------------+---------------+-------------------------------+
370 | Field | C Type | Meaning |
371 +==================+===============+===============================+
372 | :attr:`name` | const char \* | name of the member |
373 +------------------+---------------+-------------------------------+
374 | :attr:`!type` | int | the type of the member in the |
375 | | | C struct |
376 +------------------+---------------+-------------------------------+
377 | :attr:`offset` | Py_ssize_t | the offset in bytes that the |
378 | | | member is located on the |
379 | | | type's object struct |
380 +------------------+---------------+-------------------------------+
381 | :attr:`flags` | int | flag bits indicating if the |
382 | | | field should be read-only or |
383 | | | writable |
384 +------------------+---------------+-------------------------------+
385 | :attr:`doc` | const char \* | points to the contents of the |
386 | | | docstring |
387 +------------------+---------------+-------------------------------+
Georg Brandl1f01deb2009-01-03 22:47:39 +0000388
csabellac3c7ef02017-03-29 20:27:50 -0400389 :attr:`!type` can be one of many ``T_`` macros corresponding to various C
Georg Brandl1f01deb2009-01-03 22:47:39 +0000390 types. When the member is accessed in Python, it will be converted to the
391 equivalent Python type.
392
393 =============== ==================
394 Macro name C type
395 =============== ==================
396 T_SHORT short
397 T_INT int
398 T_LONG long
399 T_FLOAT float
400 T_DOUBLE double
Serhiy Storchaka84b8e922017-03-30 10:01:03 +0300401 T_STRING const char \*
Georg Brandl1f01deb2009-01-03 22:47:39 +0000402 T_OBJECT PyObject \*
403 T_OBJECT_EX PyObject \*
404 T_CHAR char
405 T_BYTE char
Benjamin Petersond23f8222009-04-05 19:13:16 +0000406 T_UBYTE unsigned char
Georg Brandl1f01deb2009-01-03 22:47:39 +0000407 T_UINT unsigned int
408 T_USHORT unsigned short
409 T_ULONG unsigned long
410 T_BOOL char
411 T_LONGLONG long long
412 T_ULONGLONG unsigned long long
413 T_PYSSIZET Py_ssize_t
414 =============== ==================
415
Georg Brandl60203b42010-10-06 10:11:56 +0000416 :c:macro:`T_OBJECT` and :c:macro:`T_OBJECT_EX` differ in that
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200417 :c:macro:`T_OBJECT` returns ``None`` if the member is ``NULL`` and
Georg Brandl60203b42010-10-06 10:11:56 +0000418 :c:macro:`T_OBJECT_EX` raises an :exc:`AttributeError`. Try to use
419 :c:macro:`T_OBJECT_EX` over :c:macro:`T_OBJECT` because :c:macro:`T_OBJECT_EX`
Ezio Melotti479def32010-01-03 09:11:59 +0000420 handles use of the :keyword:`del` statement on that attribute more correctly
Georg Brandl60203b42010-10-06 10:11:56 +0000421 than :c:macro:`T_OBJECT`.
Georg Brandl1f01deb2009-01-03 22:47:39 +0000422
Serhiy Storchaka1ecf7d22016-10-27 21:41:19 +0300423 :attr:`flags` can be ``0`` for write and read access or :c:macro:`READONLY` for
Georg Brandl60203b42010-10-06 10:11:56 +0000424 read-only access. Using :c:macro:`T_STRING` for :attr:`type` implies
Windson yang689d5552018-11-18 03:16:51 +0800425 :c:macro:`READONLY`. :c:macro:`T_STRING` data is interpreted as UTF-8.
426 Only :c:macro:`T_OBJECT` and :c:macro:`T_OBJECT_EX`
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200427 members can be deleted. (They are set to ``NULL``).
Michael Seifertda67e0d2017-09-15 18:25:27 +0200428
Petr Viktorin468f8a62019-09-25 13:06:16 +0200429 .. _pymemberdef-offsets:
430
431 Heap allocated types (created using :c:func:`PyType_FromSpec` or similar),
Gurupad Hegde6c7bb382019-12-28 17:16:02 -0500432 ``PyMemberDef`` may contain definitions for the special members
Hai Shi86d69442020-05-12 05:38:55 +0800433 ``__dictoffset__``, ``__weaklistoffset__`` and ``__vectorcalloffset__``,
434 corresponding to
435 :c:member:`~PyTypeObject.tp_dictoffset`,
436 :c:member:`~PyTypeObject.tp_weaklistoffset` and
437 :c:member:`~PyTypeObject.tp_vectorcall_offset` in type objects.
Petr Viktorin468f8a62019-09-25 13:06:16 +0200438 These must be defined with ``T_PYSSIZET`` and ``READONLY``, for example::
439
440 static PyMemberDef spam_type_members[] = {
441 {"__dictoffset__", T_PYSSIZET, offsetof(Spam_object, dict), READONLY},
442 {NULL} /* Sentinel */
443 };
Michael Seifertda67e0d2017-09-15 18:25:27 +0200444
445.. c:type:: PyGetSetDef
446
447 Structure to define property-like access for a type. See also description of
448 the :c:member:`PyTypeObject.tp_getset` slot.
449
450 +-------------+------------------+-----------------------------------+
451 | Field | C Type | Meaning |
452 +=============+==================+===================================+
453 | name | const char \* | attribute name |
454 +-------------+------------------+-----------------------------------+
455 | get | getter | C Function to get the attribute |
456 +-------------+------------------+-----------------------------------+
457 | set | setter | optional C function to set or |
458 | | | delete the attribute, if omitted |
459 | | | the attribute is readonly |
460 +-------------+------------------+-----------------------------------+
461 | doc | const char \* | optional docstring |
462 +-------------+------------------+-----------------------------------+
463 | closure | void \* | optional function pointer, |
464 | | | providing additional data for |
465 | | | getter and setter |
466 +-------------+------------------+-----------------------------------+
467
Victor Stinner474652f2020-08-13 22:11:50 +0200468 The ``get`` function takes one :c:type:`PyObject*` parameter (the
Michael Seifertda67e0d2017-09-15 18:25:27 +0200469 instance) and a function pointer (the associated ``closure``)::
470
471 typedef PyObject *(*getter)(PyObject *, void *);
472
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200473 It should return a new reference on success or ``NULL`` with a set exception
Michael Seifertda67e0d2017-09-15 18:25:27 +0200474 on failure.
475
Victor Stinner474652f2020-08-13 22:11:50 +0200476 ``set`` functions take two :c:type:`PyObject*` parameters (the instance and
Michael Seifertda67e0d2017-09-15 18:25:27 +0200477 the value to be set) and a function pointer (the associated ``closure``)::
478
479 typedef int (*setter)(PyObject *, PyObject *, void *);
480
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200481 In case the attribute should be deleted the second parameter is ``NULL``.
Michael Seifertda67e0d2017-09-15 18:25:27 +0200482 Should return ``0`` on success or ``-1`` with a set exception on failure.