blob: fc3467bee4d3cf7818f9ac03e2156c3bee5a3012 [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
65.. c:macro:: Py_TYPE(o)
66
Zachary Waree36402a2015-07-06 23:58:12 -050067 This macro is used to access the :attr:`ob_type` member of a Python object.
Gregory P. Smith1b244652015-04-14 11:12:53 -070068 It expands to::
69
70 (((PyObject*)(o))->ob_type)
71
72
Dong-hee Nad905df72020-02-14 02:37:17 +090073.. c:function:: int Py_IS_TYPE(PyObject *o, PyTypeObject *type)
74
75 Return non-zero if the object *o* type is *type*. Return zero otherwise.
76 Equivalent to: ``Py_TYPE(o) == type``.
77
78 .. versionadded:: 3.9
79
80
Victor Stinnerd2ec81a2020-02-07 09:17:07 +010081.. c:function:: void Py_SET_TYPE(PyObject *o, PyTypeObject *type)
82
83 Set the object *o* type to *type*.
84
85 .. versionadded:: 3.9
86
87
Gregory P. Smith1b244652015-04-14 11:12:53 -070088.. c:macro:: Py_REFCNT(o)
89
Zachary Waree36402a2015-07-06 23:58:12 -050090 This macro is used to access the :attr:`ob_refcnt` member of a Python
91 object.
Gregory P. Smith1b244652015-04-14 11:12:53 -070092 It expands to::
93
94 (((PyObject*)(o))->ob_refcnt)
95
96
Victor Stinnerc86a1122020-02-07 01:24:29 +010097.. c:function:: void Py_SET_REFCNT(PyObject *o, Py_ssize_t refcnt)
98
99 Set the object *o* reference counter to *refcnt*.
100
101 .. versionadded:: 3.9
102
103
Gregory P. Smith1b244652015-04-14 11:12:53 -0700104.. c:macro:: Py_SIZE(o)
105
Zachary Waree36402a2015-07-06 23:58:12 -0500106 This macro is used to access the :attr:`ob_size` member of a Python object.
Gregory P. Smith1b244652015-04-14 11:12:53 -0700107 It expands to::
108
109 (((PyVarObject*)(o))->ob_size)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000110
Jeroen Ruigrok van der Werven939c1782009-04-26 20:25:45 +0000111
Victor Stinnerb10dc3e2020-02-07 12:05:12 +0100112.. c:function:: void Py_SET_SIZE(PyVarObject *o, Py_ssize_t size)
113
Brandt Bucher968dcd92020-02-13 09:34:45 -0800114 Set the object *o* size to *size*.
Victor Stinnerb10dc3e2020-02-07 12:05:12 +0100115
116 .. versionadded:: 3.9
117
118
Georg Brandl60203b42010-10-06 10:11:56 +0000119.. c:macro:: PyObject_HEAD_INIT(type)
Jeroen Ruigrok van der Werven939c1782009-04-26 20:25:45 +0000120
121 This is a macro which expands to initialization values for a new
Georg Brandl60203b42010-10-06 10:11:56 +0000122 :c:type:`PyObject` type. This macro expands to::
Jeroen Ruigrok van der Werven939c1782009-04-26 20:25:45 +0000123
124 _PyObject_EXTRA_INIT
125 1, type,
126
127
Georg Brandl60203b42010-10-06 10:11:56 +0000128.. c:macro:: PyVarObject_HEAD_INIT(type, size)
Jeroen Ruigrok van der Werven939c1782009-04-26 20:25:45 +0000129
130 This is a macro which expands to initialization values for a new
Georg Brandl60203b42010-10-06 10:11:56 +0000131 :c:type:`PyVarObject` type, including the :attr:`ob_size` field.
Jeroen Ruigrok van der Werven939c1782009-04-26 20:25:45 +0000132 This macro expands to::
133
134 _PyObject_EXTRA_INIT
135 1, type, size,
Georg Brandl54a3faa2008-01-20 09:30:57 +0000136
137
Jeroen Demeyer96699312019-09-10 12:41:59 +0200138Implementing functions and methods
139----------------------------------
140
Georg Brandl60203b42010-10-06 10:11:56 +0000141.. c:type:: PyCFunction
Georg Brandl54a3faa2008-01-20 09:30:57 +0000142
Jeroen Ruigrok van der Wervenf4a9f962009-04-26 20:21:12 +0000143 Type of the functions used to implement most Python callables in C.
Georg Brandl60203b42010-10-06 10:11:56 +0000144 Functions of this type take two :c:type:`PyObject\*` parameters and return
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200145 one such value. If the return value is ``NULL``, an exception shall have
146 been set. If not ``NULL``, the return value is interpreted as the return
Jeroen Ruigrok van der Wervenf4a9f962009-04-26 20:21:12 +0000147 value of the function as exposed in Python. The function must return a new
148 reference.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000149
150
Georg Brandl60203b42010-10-06 10:11:56 +0000151.. c:type:: PyCFunctionWithKeywords
Georg Brandl54a3faa2008-01-20 09:30:57 +0000152
Jeroen Demeyer5600b5e2019-06-16 19:03:23 +0200153 Type of the functions used to implement Python callables in C
154 with signature :const:`METH_VARARGS | METH_KEYWORDS`.
155
156
157.. c:type:: _PyCFunctionFast
158
159 Type of the functions used to implement Python callables in C
160 with signature :const:`METH_FASTCALL`.
161
162
163.. c:type:: _PyCFunctionFastWithKeywords
164
165 Type of the functions used to implement Python callables in C
166 with signature :const:`METH_FASTCALL | METH_KEYWORDS`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000167
168
Georg Brandl60203b42010-10-06 10:11:56 +0000169.. c:type:: PyMethodDef
Georg Brandl54a3faa2008-01-20 09:30:57 +0000170
171 Structure used to describe a method of an extension type. This structure has
172 four fields:
173
Serhiy Storchaka84b8e922017-03-30 10:01:03 +0300174 +------------------+---------------+-------------------------------+
175 | Field | C Type | Meaning |
176 +==================+===============+===============================+
177 | :attr:`ml_name` | const char \* | name of the method |
178 +------------------+---------------+-------------------------------+
179 | :attr:`ml_meth` | PyCFunction | pointer to the C |
180 | | | implementation |
181 +------------------+---------------+-------------------------------+
182 | :attr:`ml_flags` | int | flag bits indicating how the |
183 | | | call should be constructed |
184 +------------------+---------------+-------------------------------+
185 | :attr:`ml_doc` | const char \* | points to the contents of the |
186 | | | docstring |
187 +------------------+---------------+-------------------------------+
Georg Brandl54a3faa2008-01-20 09:30:57 +0000188
189The :attr:`ml_meth` is a C function pointer. The functions may be of different
Georg Brandl60203b42010-10-06 10:11:56 +0000190types, but they always return :c:type:`PyObject\*`. If the function is not of
191the :c:type:`PyCFunction`, the compiler will require a cast in the method table.
192Even though :c:type:`PyCFunction` defines the first parameter as
Benjamin Peterson82f34ad2015-01-13 09:17:24 -0500193:c:type:`PyObject\*`, it is common that the method implementation uses the
Georg Brandl54a3faa2008-01-20 09:30:57 +0000194specific C type of the *self* object.
195
196The :attr:`ml_flags` field is a bitfield which can include the following flags.
197The individual flags indicate either a calling convention or a binding
Jeroen Demeyer5600b5e2019-06-16 19:03:23 +0200198convention.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000199
Jeroen Demeyer5600b5e2019-06-16 19:03:23 +0200200There are four basic calling conventions for positional arguments
201and two of them can be combined with :const:`METH_KEYWORDS` to support
202also keyword arguments. So there are a total of 6 calling conventions:
Georg Brandl54a3faa2008-01-20 09:30:57 +0000203
204.. data:: METH_VARARGS
205
206 This is the typical calling convention, where the methods have the type
Georg Brandl60203b42010-10-06 10:11:56 +0000207 :c:type:`PyCFunction`. The function expects two :c:type:`PyObject\*` values.
Georg Brandl21dc5ba2009-07-11 10:43:08 +0000208 The first one is the *self* object for methods; for module functions, it is
209 the module object. The second parameter (often called *args*) is a tuple
210 object representing all arguments. This parameter is typically processed
Georg Brandl60203b42010-10-06 10:11:56 +0000211 using :c:func:`PyArg_ParseTuple` or :c:func:`PyArg_UnpackTuple`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000212
213
Jeroen Demeyer5600b5e2019-06-16 19:03:23 +0200214.. data:: METH_VARARGS | METH_KEYWORDS
Georg Brandl54a3faa2008-01-20 09:30:57 +0000215
Georg Brandl60203b42010-10-06 10:11:56 +0000216 Methods with these flags must be of type :c:type:`PyCFunctionWithKeywords`.
Jeroen Demeyer5600b5e2019-06-16 19:03:23 +0200217 The function expects three parameters: *self*, *args*, *kwargs* where
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200218 *kwargs* is a dictionary of all the keyword arguments or possibly ``NULL``
Jeroen Demeyer5600b5e2019-06-16 19:03:23 +0200219 if there are no keyword arguments. The parameters are typically processed
220 using :c:func:`PyArg_ParseTupleAndKeywords`.
221
222
223.. data:: METH_FASTCALL
224
225 Fast calling convention supporting only positional arguments.
226 The methods have the type :c:type:`_PyCFunctionFast`.
227 The first parameter is *self*, the second parameter is a C array
228 of :c:type:`PyObject\*` values indicating the arguments and the third
229 parameter is the number of arguments (the length of the array).
230
231 This is not part of the :ref:`limited API <stable>`.
232
233 .. versionadded:: 3.7
234
235
236.. data:: METH_FASTCALL | METH_KEYWORDS
237
238 Extension of :const:`METH_FASTCALL` supporting also keyword arguments,
239 with methods of type :c:type:`_PyCFunctionFastWithKeywords`.
Jeroen Demeyer9a13a382019-11-12 14:08:00 +0100240 Keyword arguments are passed the same way as in the
241 :ref:`vectorcall protocol <vectorcall>`:
Jeroen Demeyer5600b5e2019-06-16 19:03:23 +0200242 there is an additional fourth :c:type:`PyObject\*` parameter
243 which is a tuple representing the names of the keyword arguments
Jeroen Demeyer05677862019-08-16 12:41:27 +0200244 (which are guaranteed to be strings)
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200245 or possibly ``NULL`` if there are no keywords. The values of the keyword
Jeroen Demeyer5600b5e2019-06-16 19:03:23 +0200246 arguments are stored in the *args* array, after the positional arguments.
247
248 This is not part of the :ref:`limited API <stable>`.
249
250 .. versionadded:: 3.7
Georg Brandl54a3faa2008-01-20 09:30:57 +0000251
252
253.. data:: METH_NOARGS
254
255 Methods without parameters don't need to check whether arguments are given if
256 they are listed with the :const:`METH_NOARGS` flag. They need to be of type
Georg Brandl60203b42010-10-06 10:11:56 +0000257 :c:type:`PyCFunction`. The first parameter is typically named *self* and will
Georg Brandl21dc5ba2009-07-11 10:43:08 +0000258 hold a reference to the module or object instance. In all cases the second
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200259 parameter will be ``NULL``.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000260
261
262.. data:: METH_O
263
264 Methods with a single object argument can be listed with the :const:`METH_O`
Georg Brandl60203b42010-10-06 10:11:56 +0000265 flag, instead of invoking :c:func:`PyArg_ParseTuple` with a ``"O"`` argument.
266 They have the type :c:type:`PyCFunction`, with the *self* parameter, and a
267 :c:type:`PyObject\*` parameter representing the single argument.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000268
269
270These two constants are not used to indicate the calling convention but the
271binding when use with methods of classes. These may not be used for functions
272defined for modules. At most one of these flags may be set for any given
273method.
274
275
276.. data:: METH_CLASS
277
278 .. index:: builtin: classmethod
279
Jeroen Ruigrok van der Wervenf4a9f962009-04-26 20:21:12 +0000280 The method will be passed the type object as the first parameter rather
281 than an instance of the type. This is used to create *class methods*,
282 similar to what is created when using the :func:`classmethod` built-in
283 function.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000284
285
286.. data:: METH_STATIC
287
288 .. index:: builtin: staticmethod
289
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200290 The method will be passed ``NULL`` as the first parameter rather than an
Jeroen Ruigrok van der Wervenf4a9f962009-04-26 20:21:12 +0000291 instance of the type. This is used to create *static methods*, similar to
292 what is created when using the :func:`staticmethod` built-in function.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000293
294One other constant controls whether a method is loaded in place of another
295definition with the same method name.
296
297
298.. data:: METH_COEXIST
299
300 The method will be loaded in place of existing definitions. Without
301 *METH_COEXIST*, the default is to skip repeated definitions. Since slot
Jeroen Ruigrok van der Wervenf4a9f962009-04-26 20:21:12 +0000302 wrappers are loaded before the method table, the existence of a
303 *sq_contains* slot, for example, would generate a wrapped method named
304 :meth:`__contains__` and preclude the loading of a corresponding
305 PyCFunction with the same name. With the flag defined, the PyCFunction
306 will be loaded in place of the wrapper object and will co-exist with the
307 slot. This is helpful because calls to PyCFunctions are optimized more
308 than wrapper object calls.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000309
Georg Brandl1f01deb2009-01-03 22:47:39 +0000310
Jeroen Demeyer96699312019-09-10 12:41:59 +0200311Accessing attributes of extension types
312---------------------------------------
313
Georg Brandl60203b42010-10-06 10:11:56 +0000314.. c:type:: PyMemberDef
Georg Brandl1f01deb2009-01-03 22:47:39 +0000315
316 Structure which describes an attribute of a type which corresponds to a C
317 struct member. Its fields are:
318
Serhiy Storchaka84b8e922017-03-30 10:01:03 +0300319 +------------------+---------------+-------------------------------+
320 | Field | C Type | Meaning |
321 +==================+===============+===============================+
322 | :attr:`name` | const char \* | name of the member |
323 +------------------+---------------+-------------------------------+
324 | :attr:`!type` | int | the type of the member in the |
325 | | | C struct |
326 +------------------+---------------+-------------------------------+
327 | :attr:`offset` | Py_ssize_t | the offset in bytes that the |
328 | | | member is located on the |
329 | | | type's object struct |
330 +------------------+---------------+-------------------------------+
331 | :attr:`flags` | int | flag bits indicating if the |
332 | | | field should be read-only or |
333 | | | writable |
334 +------------------+---------------+-------------------------------+
335 | :attr:`doc` | const char \* | points to the contents of the |
336 | | | docstring |
337 +------------------+---------------+-------------------------------+
Georg Brandl1f01deb2009-01-03 22:47:39 +0000338
csabellac3c7ef02017-03-29 20:27:50 -0400339 :attr:`!type` can be one of many ``T_`` macros corresponding to various C
Georg Brandl1f01deb2009-01-03 22:47:39 +0000340 types. When the member is accessed in Python, it will be converted to the
341 equivalent Python type.
342
343 =============== ==================
344 Macro name C type
345 =============== ==================
346 T_SHORT short
347 T_INT int
348 T_LONG long
349 T_FLOAT float
350 T_DOUBLE double
Serhiy Storchaka84b8e922017-03-30 10:01:03 +0300351 T_STRING const char \*
Georg Brandl1f01deb2009-01-03 22:47:39 +0000352 T_OBJECT PyObject \*
353 T_OBJECT_EX PyObject \*
354 T_CHAR char
355 T_BYTE char
Benjamin Petersond23f8222009-04-05 19:13:16 +0000356 T_UBYTE unsigned char
Georg Brandl1f01deb2009-01-03 22:47:39 +0000357 T_UINT unsigned int
358 T_USHORT unsigned short
359 T_ULONG unsigned long
360 T_BOOL char
361 T_LONGLONG long long
362 T_ULONGLONG unsigned long long
363 T_PYSSIZET Py_ssize_t
364 =============== ==================
365
Georg Brandl60203b42010-10-06 10:11:56 +0000366 :c:macro:`T_OBJECT` and :c:macro:`T_OBJECT_EX` differ in that
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200367 :c:macro:`T_OBJECT` returns ``None`` if the member is ``NULL`` and
Georg Brandl60203b42010-10-06 10:11:56 +0000368 :c:macro:`T_OBJECT_EX` raises an :exc:`AttributeError`. Try to use
369 :c:macro:`T_OBJECT_EX` over :c:macro:`T_OBJECT` because :c:macro:`T_OBJECT_EX`
Ezio Melotti479def32010-01-03 09:11:59 +0000370 handles use of the :keyword:`del` statement on that attribute more correctly
Georg Brandl60203b42010-10-06 10:11:56 +0000371 than :c:macro:`T_OBJECT`.
Georg Brandl1f01deb2009-01-03 22:47:39 +0000372
Serhiy Storchaka1ecf7d22016-10-27 21:41:19 +0300373 :attr:`flags` can be ``0`` for write and read access or :c:macro:`READONLY` for
Georg Brandl60203b42010-10-06 10:11:56 +0000374 read-only access. Using :c:macro:`T_STRING` for :attr:`type` implies
Windson yang689d5552018-11-18 03:16:51 +0800375 :c:macro:`READONLY`. :c:macro:`T_STRING` data is interpreted as UTF-8.
376 Only :c:macro:`T_OBJECT` and :c:macro:`T_OBJECT_EX`
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200377 members can be deleted. (They are set to ``NULL``).
Michael Seifertda67e0d2017-09-15 18:25:27 +0200378
Petr Viktorin468f8a62019-09-25 13:06:16 +0200379 .. _pymemberdef-offsets:
380
381 Heap allocated types (created using :c:func:`PyType_FromSpec` or similar),
Gurupad Hegde6c7bb382019-12-28 17:16:02 -0500382 ``PyMemberDef`` may contain definitions for the special members
Petr Viktorin468f8a62019-09-25 13:06:16 +0200383 ``__dictoffset__`` and ``__weaklistoffset__``, corresponding to
384 :c:member:`~PyTypeObject.tp_dictoffset` and
385 :c:member:`~PyTypeObject.tp_weaklistoffset` in type objects.
386 These must be defined with ``T_PYSSIZET`` and ``READONLY``, for example::
387
388 static PyMemberDef spam_type_members[] = {
389 {"__dictoffset__", T_PYSSIZET, offsetof(Spam_object, dict), READONLY},
390 {NULL} /* Sentinel */
391 };
Michael Seifertda67e0d2017-09-15 18:25:27 +0200392
393.. c:type:: PyGetSetDef
394
395 Structure to define property-like access for a type. See also description of
396 the :c:member:`PyTypeObject.tp_getset` slot.
397
398 +-------------+------------------+-----------------------------------+
399 | Field | C Type | Meaning |
400 +=============+==================+===================================+
401 | name | const char \* | attribute name |
402 +-------------+------------------+-----------------------------------+
403 | get | getter | C Function to get the attribute |
404 +-------------+------------------+-----------------------------------+
405 | set | setter | optional C function to set or |
406 | | | delete the attribute, if omitted |
407 | | | the attribute is readonly |
408 +-------------+------------------+-----------------------------------+
409 | doc | const char \* | optional docstring |
410 +-------------+------------------+-----------------------------------+
411 | closure | void \* | optional function pointer, |
412 | | | providing additional data for |
413 | | | getter and setter |
414 +-------------+------------------+-----------------------------------+
415
416 The ``get`` function takes one :c:type:`PyObject\*` parameter (the
417 instance) and a function pointer (the associated ``closure``)::
418
419 typedef PyObject *(*getter)(PyObject *, void *);
420
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200421 It should return a new reference on success or ``NULL`` with a set exception
Michael Seifertda67e0d2017-09-15 18:25:27 +0200422 on failure.
423
424 ``set`` functions take two :c:type:`PyObject\*` parameters (the instance and
425 the value to be set) and a function pointer (the associated ``closure``)::
426
427 typedef int (*setter)(PyObject *, PyObject *, void *);
428
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200429 In case the attribute should be deleted the second parameter is ``NULL``.
Michael Seifertda67e0d2017-09-15 18:25:27 +0200430 Should return ``0`` on success or ``-1`` with a set exception on failure.