blob: 1bd769f275b351dcea735c9b60f4131e20a2a2b7 [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
73.. c:macro:: Py_REFCNT(o)
74
Zachary Waree36402a2015-07-06 23:58:12 -050075 This macro is used to access the :attr:`ob_refcnt` member of a Python
76 object.
Gregory P. Smith1b244652015-04-14 11:12:53 -070077 It expands to::
78
79 (((PyObject*)(o))->ob_refcnt)
80
81
82.. c:macro:: Py_SIZE(o)
83
Zachary Waree36402a2015-07-06 23:58:12 -050084 This macro is used to access the :attr:`ob_size` member of a Python object.
Gregory P. Smith1b244652015-04-14 11:12:53 -070085 It expands to::
86
87 (((PyVarObject*)(o))->ob_size)
Georg Brandl54a3faa2008-01-20 09:30:57 +000088
Jeroen Ruigrok van der Werven939c1782009-04-26 20:25:45 +000089
Georg Brandl60203b42010-10-06 10:11:56 +000090.. c:macro:: PyObject_HEAD_INIT(type)
Jeroen Ruigrok van der Werven939c1782009-04-26 20:25:45 +000091
92 This is a macro which expands to initialization values for a new
Georg Brandl60203b42010-10-06 10:11:56 +000093 :c:type:`PyObject` type. This macro expands to::
Jeroen Ruigrok van der Werven939c1782009-04-26 20:25:45 +000094
95 _PyObject_EXTRA_INIT
96 1, type,
97
98
Georg Brandl60203b42010-10-06 10:11:56 +000099.. c:macro:: PyVarObject_HEAD_INIT(type, size)
Jeroen Ruigrok van der Werven939c1782009-04-26 20:25:45 +0000100
101 This is a macro which expands to initialization values for a new
Georg Brandl60203b42010-10-06 10:11:56 +0000102 :c:type:`PyVarObject` type, including the :attr:`ob_size` field.
Jeroen Ruigrok van der Werven939c1782009-04-26 20:25:45 +0000103 This macro expands to::
104
105 _PyObject_EXTRA_INIT
106 1, type, size,
Georg Brandl54a3faa2008-01-20 09:30:57 +0000107
108
Jeroen Demeyer96699312019-09-10 12:41:59 +0200109Implementing functions and methods
110----------------------------------
111
Georg Brandl60203b42010-10-06 10:11:56 +0000112.. c:type:: PyCFunction
Georg Brandl54a3faa2008-01-20 09:30:57 +0000113
Jeroen Ruigrok van der Wervenf4a9f962009-04-26 20:21:12 +0000114 Type of the functions used to implement most Python callables in C.
Georg Brandl60203b42010-10-06 10:11:56 +0000115 Functions of this type take two :c:type:`PyObject\*` parameters and return
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200116 one such value. If the return value is ``NULL``, an exception shall have
117 been set. If not ``NULL``, the return value is interpreted as the return
Jeroen Ruigrok van der Wervenf4a9f962009-04-26 20:21:12 +0000118 value of the function as exposed in Python. The function must return a new
119 reference.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000120
121
Georg Brandl60203b42010-10-06 10:11:56 +0000122.. c:type:: PyCFunctionWithKeywords
Georg Brandl54a3faa2008-01-20 09:30:57 +0000123
Jeroen Demeyer5600b5e2019-06-16 19:03:23 +0200124 Type of the functions used to implement Python callables in C
125 with signature :const:`METH_VARARGS | METH_KEYWORDS`.
126
127
128.. c:type:: _PyCFunctionFast
129
130 Type of the functions used to implement Python callables in C
131 with signature :const:`METH_FASTCALL`.
132
133
134.. c:type:: _PyCFunctionFastWithKeywords
135
136 Type of the functions used to implement Python callables in C
137 with signature :const:`METH_FASTCALL | METH_KEYWORDS`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000138
139
Georg Brandl60203b42010-10-06 10:11:56 +0000140.. c:type:: PyMethodDef
Georg Brandl54a3faa2008-01-20 09:30:57 +0000141
142 Structure used to describe a method of an extension type. This structure has
143 four fields:
144
Serhiy Storchaka84b8e922017-03-30 10:01:03 +0300145 +------------------+---------------+-------------------------------+
146 | Field | C Type | Meaning |
147 +==================+===============+===============================+
148 | :attr:`ml_name` | const char \* | name of the method |
149 +------------------+---------------+-------------------------------+
150 | :attr:`ml_meth` | PyCFunction | pointer to the C |
151 | | | implementation |
152 +------------------+---------------+-------------------------------+
153 | :attr:`ml_flags` | int | flag bits indicating how the |
154 | | | call should be constructed |
155 +------------------+---------------+-------------------------------+
156 | :attr:`ml_doc` | const char \* | points to the contents of the |
157 | | | docstring |
158 +------------------+---------------+-------------------------------+
Georg Brandl54a3faa2008-01-20 09:30:57 +0000159
160The :attr:`ml_meth` is a C function pointer. The functions may be of different
Georg Brandl60203b42010-10-06 10:11:56 +0000161types, but they always return :c:type:`PyObject\*`. If the function is not of
162the :c:type:`PyCFunction`, the compiler will require a cast in the method table.
163Even though :c:type:`PyCFunction` defines the first parameter as
Benjamin Peterson82f34ad2015-01-13 09:17:24 -0500164:c:type:`PyObject\*`, it is common that the method implementation uses the
Georg Brandl54a3faa2008-01-20 09:30:57 +0000165specific C type of the *self* object.
166
167The :attr:`ml_flags` field is a bitfield which can include the following flags.
168The individual flags indicate either a calling convention or a binding
Jeroen Demeyer5600b5e2019-06-16 19:03:23 +0200169convention.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000170
Jeroen Demeyer5600b5e2019-06-16 19:03:23 +0200171There are four basic calling conventions for positional arguments
172and two of them can be combined with :const:`METH_KEYWORDS` to support
173also keyword arguments. So there are a total of 6 calling conventions:
Georg Brandl54a3faa2008-01-20 09:30:57 +0000174
175.. data:: METH_VARARGS
176
177 This is the typical calling convention, where the methods have the type
Georg Brandl60203b42010-10-06 10:11:56 +0000178 :c:type:`PyCFunction`. The function expects two :c:type:`PyObject\*` values.
Georg Brandl21dc5ba2009-07-11 10:43:08 +0000179 The first one is the *self* object for methods; for module functions, it is
180 the module object. The second parameter (often called *args*) is a tuple
181 object representing all arguments. This parameter is typically processed
Georg Brandl60203b42010-10-06 10:11:56 +0000182 using :c:func:`PyArg_ParseTuple` or :c:func:`PyArg_UnpackTuple`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000183
184
Jeroen Demeyer5600b5e2019-06-16 19:03:23 +0200185.. data:: METH_VARARGS | METH_KEYWORDS
Georg Brandl54a3faa2008-01-20 09:30:57 +0000186
Georg Brandl60203b42010-10-06 10:11:56 +0000187 Methods with these flags must be of type :c:type:`PyCFunctionWithKeywords`.
Jeroen Demeyer5600b5e2019-06-16 19:03:23 +0200188 The function expects three parameters: *self*, *args*, *kwargs* where
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200189 *kwargs* is a dictionary of all the keyword arguments or possibly ``NULL``
Jeroen Demeyer5600b5e2019-06-16 19:03:23 +0200190 if there are no keyword arguments. The parameters are typically processed
191 using :c:func:`PyArg_ParseTupleAndKeywords`.
192
193
194.. data:: METH_FASTCALL
195
196 Fast calling convention supporting only positional arguments.
197 The methods have the type :c:type:`_PyCFunctionFast`.
198 The first parameter is *self*, the second parameter is a C array
199 of :c:type:`PyObject\*` values indicating the arguments and the third
200 parameter is the number of arguments (the length of the array).
201
202 This is not part of the :ref:`limited API <stable>`.
203
204 .. versionadded:: 3.7
205
206
207.. data:: METH_FASTCALL | METH_KEYWORDS
208
209 Extension of :const:`METH_FASTCALL` supporting also keyword arguments,
210 with methods of type :c:type:`_PyCFunctionFastWithKeywords`.
Jeroen Demeyer9a13a382019-11-12 14:08:00 +0100211 Keyword arguments are passed the same way as in the
212 :ref:`vectorcall protocol <vectorcall>`:
Jeroen Demeyer5600b5e2019-06-16 19:03:23 +0200213 there is an additional fourth :c:type:`PyObject\*` parameter
214 which is a tuple representing the names of the keyword arguments
Jeroen Demeyer05677862019-08-16 12:41:27 +0200215 (which are guaranteed to be strings)
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200216 or possibly ``NULL`` if there are no keywords. The values of the keyword
Jeroen Demeyer5600b5e2019-06-16 19:03:23 +0200217 arguments are stored in the *args* array, after the positional arguments.
218
219 This is not part of the :ref:`limited API <stable>`.
220
221 .. versionadded:: 3.7
Georg Brandl54a3faa2008-01-20 09:30:57 +0000222
223
224.. data:: METH_NOARGS
225
226 Methods without parameters don't need to check whether arguments are given if
227 they are listed with the :const:`METH_NOARGS` flag. They need to be of type
Georg Brandl60203b42010-10-06 10:11:56 +0000228 :c:type:`PyCFunction`. The first parameter is typically named *self* and will
Georg Brandl21dc5ba2009-07-11 10:43:08 +0000229 hold a reference to the module or object instance. In all cases the second
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200230 parameter will be ``NULL``.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000231
232
233.. data:: METH_O
234
235 Methods with a single object argument can be listed with the :const:`METH_O`
Georg Brandl60203b42010-10-06 10:11:56 +0000236 flag, instead of invoking :c:func:`PyArg_ParseTuple` with a ``"O"`` argument.
237 They have the type :c:type:`PyCFunction`, with the *self* parameter, and a
238 :c:type:`PyObject\*` parameter representing the single argument.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000239
240
241These two constants are not used to indicate the calling convention but the
242binding when use with methods of classes. These may not be used for functions
243defined for modules. At most one of these flags may be set for any given
244method.
245
246
247.. data:: METH_CLASS
248
249 .. index:: builtin: classmethod
250
Jeroen Ruigrok van der Wervenf4a9f962009-04-26 20:21:12 +0000251 The method will be passed the type object as the first parameter rather
252 than an instance of the type. This is used to create *class methods*,
253 similar to what is created when using the :func:`classmethod` built-in
254 function.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000255
256
257.. data:: METH_STATIC
258
259 .. index:: builtin: staticmethod
260
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200261 The method will be passed ``NULL`` as the first parameter rather than an
Jeroen Ruigrok van der Wervenf4a9f962009-04-26 20:21:12 +0000262 instance of the type. This is used to create *static methods*, similar to
263 what is created when using the :func:`staticmethod` built-in function.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000264
265One other constant controls whether a method is loaded in place of another
266definition with the same method name.
267
268
269.. data:: METH_COEXIST
270
271 The method will be loaded in place of existing definitions. Without
272 *METH_COEXIST*, the default is to skip repeated definitions. Since slot
Jeroen Ruigrok van der Wervenf4a9f962009-04-26 20:21:12 +0000273 wrappers are loaded before the method table, the existence of a
274 *sq_contains* slot, for example, would generate a wrapped method named
275 :meth:`__contains__` and preclude the loading of a corresponding
276 PyCFunction with the same name. With the flag defined, the PyCFunction
277 will be loaded in place of the wrapper object and will co-exist with the
278 slot. This is helpful because calls to PyCFunctions are optimized more
279 than wrapper object calls.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000280
Georg Brandl1f01deb2009-01-03 22:47:39 +0000281
Jeroen Demeyer96699312019-09-10 12:41:59 +0200282Accessing attributes of extension types
283---------------------------------------
284
Georg Brandl60203b42010-10-06 10:11:56 +0000285.. c:type:: PyMemberDef
Georg Brandl1f01deb2009-01-03 22:47:39 +0000286
287 Structure which describes an attribute of a type which corresponds to a C
288 struct member. Its fields are:
289
Serhiy Storchaka84b8e922017-03-30 10:01:03 +0300290 +------------------+---------------+-------------------------------+
291 | Field | C Type | Meaning |
292 +==================+===============+===============================+
293 | :attr:`name` | const char \* | name of the member |
294 +------------------+---------------+-------------------------------+
295 | :attr:`!type` | int | the type of the member in the |
296 | | | C struct |
297 +------------------+---------------+-------------------------------+
298 | :attr:`offset` | Py_ssize_t | the offset in bytes that the |
299 | | | member is located on the |
300 | | | type's object struct |
301 +------------------+---------------+-------------------------------+
302 | :attr:`flags` | int | flag bits indicating if the |
303 | | | field should be read-only or |
304 | | | writable |
305 +------------------+---------------+-------------------------------+
306 | :attr:`doc` | const char \* | points to the contents of the |
307 | | | docstring |
308 +------------------+---------------+-------------------------------+
Georg Brandl1f01deb2009-01-03 22:47:39 +0000309
csabellac3c7ef02017-03-29 20:27:50 -0400310 :attr:`!type` can be one of many ``T_`` macros corresponding to various C
Georg Brandl1f01deb2009-01-03 22:47:39 +0000311 types. When the member is accessed in Python, it will be converted to the
312 equivalent Python type.
313
314 =============== ==================
315 Macro name C type
316 =============== ==================
317 T_SHORT short
318 T_INT int
319 T_LONG long
320 T_FLOAT float
321 T_DOUBLE double
Serhiy Storchaka84b8e922017-03-30 10:01:03 +0300322 T_STRING const char \*
Georg Brandl1f01deb2009-01-03 22:47:39 +0000323 T_OBJECT PyObject \*
324 T_OBJECT_EX PyObject \*
325 T_CHAR char
326 T_BYTE char
Benjamin Petersond23f8222009-04-05 19:13:16 +0000327 T_UBYTE unsigned char
Georg Brandl1f01deb2009-01-03 22:47:39 +0000328 T_UINT unsigned int
329 T_USHORT unsigned short
330 T_ULONG unsigned long
331 T_BOOL char
332 T_LONGLONG long long
333 T_ULONGLONG unsigned long long
334 T_PYSSIZET Py_ssize_t
335 =============== ==================
336
Georg Brandl60203b42010-10-06 10:11:56 +0000337 :c:macro:`T_OBJECT` and :c:macro:`T_OBJECT_EX` differ in that
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200338 :c:macro:`T_OBJECT` returns ``None`` if the member is ``NULL`` and
Georg Brandl60203b42010-10-06 10:11:56 +0000339 :c:macro:`T_OBJECT_EX` raises an :exc:`AttributeError`. Try to use
340 :c:macro:`T_OBJECT_EX` over :c:macro:`T_OBJECT` because :c:macro:`T_OBJECT_EX`
Ezio Melotti479def32010-01-03 09:11:59 +0000341 handles use of the :keyword:`del` statement on that attribute more correctly
Georg Brandl60203b42010-10-06 10:11:56 +0000342 than :c:macro:`T_OBJECT`.
Georg Brandl1f01deb2009-01-03 22:47:39 +0000343
Serhiy Storchaka1ecf7d22016-10-27 21:41:19 +0300344 :attr:`flags` can be ``0`` for write and read access or :c:macro:`READONLY` for
Georg Brandl60203b42010-10-06 10:11:56 +0000345 read-only access. Using :c:macro:`T_STRING` for :attr:`type` implies
Windson yang689d5552018-11-18 03:16:51 +0800346 :c:macro:`READONLY`. :c:macro:`T_STRING` data is interpreted as UTF-8.
347 Only :c:macro:`T_OBJECT` and :c:macro:`T_OBJECT_EX`
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200348 members can be deleted. (They are set to ``NULL``).
Michael Seifertda67e0d2017-09-15 18:25:27 +0200349
Petr Viktorin468f8a62019-09-25 13:06:16 +0200350 .. _pymemberdef-offsets:
351
352 Heap allocated types (created using :c:func:`PyType_FromSpec` or similar),
353 ``PyMemberDef`` may contain defintitions for the special members
354 ``__dictoffset__`` and ``__weaklistoffset__``, corresponding to
355 :c:member:`~PyTypeObject.tp_dictoffset` and
356 :c:member:`~PyTypeObject.tp_weaklistoffset` in type objects.
357 These must be defined with ``T_PYSSIZET`` and ``READONLY``, for example::
358
359 static PyMemberDef spam_type_members[] = {
360 {"__dictoffset__", T_PYSSIZET, offsetof(Spam_object, dict), READONLY},
361 {NULL} /* Sentinel */
362 };
Michael Seifertda67e0d2017-09-15 18:25:27 +0200363
364.. c:type:: PyGetSetDef
365
366 Structure to define property-like access for a type. See also description of
367 the :c:member:`PyTypeObject.tp_getset` slot.
368
369 +-------------+------------------+-----------------------------------+
370 | Field | C Type | Meaning |
371 +=============+==================+===================================+
372 | name | const char \* | attribute name |
373 +-------------+------------------+-----------------------------------+
374 | get | getter | C Function to get the attribute |
375 +-------------+------------------+-----------------------------------+
376 | set | setter | optional C function to set or |
377 | | | delete the attribute, if omitted |
378 | | | the attribute is readonly |
379 +-------------+------------------+-----------------------------------+
380 | doc | const char \* | optional docstring |
381 +-------------+------------------+-----------------------------------+
382 | closure | void \* | optional function pointer, |
383 | | | providing additional data for |
384 | | | getter and setter |
385 +-------------+------------------+-----------------------------------+
386
387 The ``get`` function takes one :c:type:`PyObject\*` parameter (the
388 instance) and a function pointer (the associated ``closure``)::
389
390 typedef PyObject *(*getter)(PyObject *, void *);
391
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200392 It should return a new reference on success or ``NULL`` with a set exception
Michael Seifertda67e0d2017-09-15 18:25:27 +0200393 on failure.
394
395 ``set`` functions take two :c:type:`PyObject\*` parameters (the instance and
396 the value to be set) and a function pointer (the associated ``closure``)::
397
398 typedef int (*setter)(PyObject *, PyObject *, void *);
399
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200400 In case the attribute should be deleted the second parameter is ``NULL``.
Michael Seifertda67e0d2017-09-15 18:25:27 +0200401 Should return ``0`` on success or ``-1`` with a set exception on failure.