blob: d4e65afef14d53546bf57ce18b19448aebe832a3 [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 Ruigrok van der Wervenf4a9f962009-04-26 20:21:12 +000012All Python objects ultimately share a small number of fields at the beginning
13of the object's representation in memory. These are represented by the
Georg Brandl60203b42010-10-06 10:11:56 +000014:c:type:`PyObject` and :c:type:`PyVarObject` types, which are defined, in turn,
Jeroen Ruigrok van der Wervenf4a9f962009-04-26 20:21:12 +000015by the expansions of some macros also used, whether directly or indirectly, in
16the definition of all other Python objects.
Georg Brandl54a3faa2008-01-20 09:30:57 +000017
18
Georg Brandl60203b42010-10-06 10:11:56 +000019.. c:type:: PyObject
Georg Brandl54a3faa2008-01-20 09:30:57 +000020
Jeroen Ruigrok van der Wervenf4a9f962009-04-26 20:21:12 +000021 All object types are extensions of this type. This is a type which
22 contains the information Python needs to treat a pointer to an object as an
23 object. In a normal "release" build, it contains only the object's
Gregory P. Smith1b244652015-04-14 11:12:53 -070024 reference count and a pointer to the corresponding type object.
Gregory P. Smith0f2f3bc2015-04-14 11:21:05 -070025 Nothing is actually declared to be a :c:type:`PyObject`, but every pointer
26 to a Python object can be cast to a :c:type:`PyObject*`. Access to the
27 members must be done by using the macros :c:macro:`Py_REFCNT` and
Gregory P. Smith1b244652015-04-14 11:12:53 -070028 :c:macro:`Py_TYPE`.
Georg Brandl54a3faa2008-01-20 09:30:57 +000029
30
Georg Brandl60203b42010-10-06 10:11:56 +000031.. c:type:: PyVarObject
Georg Brandl54a3faa2008-01-20 09:30:57 +000032
Georg Brandl60203b42010-10-06 10:11:56 +000033 This is an extension of :c:type:`PyObject` that adds the :attr:`ob_size`
Jeroen Ruigrok van der Wervenf4a9f962009-04-26 20:21:12 +000034 field. This is only used for objects that have some notion of *length*.
Gregory P. Smith1b244652015-04-14 11:12:53 -070035 This type does not often appear in the Python/C API.
36 Access to the members must be done by using the macros
37 :c:macro:`Py_REFCNT`, :c:macro:`Py_TYPE`, and :c:macro:`Py_SIZE`.
Georg Brandl54a3faa2008-01-20 09:30:57 +000038
Georg Brandl54a3faa2008-01-20 09:30:57 +000039
Georg Brandl60203b42010-10-06 10:11:56 +000040.. c:macro:: PyObject_HEAD
Georg Brandl54a3faa2008-01-20 09:30:57 +000041
Gregory P. Smith1b244652015-04-14 11:12:53 -070042 This is a macro used when declaring new types which represent objects
43 without a varying length. The PyObject_HEAD macro expands to::
Georg Brandl54a3faa2008-01-20 09:30:57 +000044
Gregory P. Smith1b244652015-04-14 11:12:53 -070045 PyObject ob_base;
Georg Brandl54a3faa2008-01-20 09:30:57 +000046
Zachary Ware5c676f62015-07-06 23:27:15 -050047 See documentation of :c:type:`PyObject` above.
Georg Brandl54a3faa2008-01-20 09:30:57 +000048
49
Georg Brandl60203b42010-10-06 10:11:56 +000050.. c:macro:: PyObject_VAR_HEAD
Georg Brandl54a3faa2008-01-20 09:30:57 +000051
Gregory P. Smith1b244652015-04-14 11:12:53 -070052 This is a macro used when declaring new types which represent objects
53 with a length that varies from instance to instance.
54 The PyObject_VAR_HEAD macro expands to::
Georg Brandl54a3faa2008-01-20 09:30:57 +000055
Gregory P. Smith1b244652015-04-14 11:12:53 -070056 PyVarObject ob_base;
Georg Brandl54a3faa2008-01-20 09:30:57 +000057
Gregory P. Smith1b244652015-04-14 11:12:53 -070058 See documentation of :c:type:`PyVarObject` above.
59
60
61.. c:macro:: Py_TYPE(o)
62
Zachary Waree36402a2015-07-06 23:58:12 -050063 This macro is used to access the :attr:`ob_type` member of a Python object.
Gregory P. Smith1b244652015-04-14 11:12:53 -070064 It expands to::
65
66 (((PyObject*)(o))->ob_type)
67
68
69.. c:macro:: Py_REFCNT(o)
70
Zachary Waree36402a2015-07-06 23:58:12 -050071 This macro is used to access the :attr:`ob_refcnt` member of a Python
72 object.
Gregory P. Smith1b244652015-04-14 11:12:53 -070073 It expands to::
74
75 (((PyObject*)(o))->ob_refcnt)
76
77
78.. c:macro:: Py_SIZE(o)
79
Zachary Waree36402a2015-07-06 23:58:12 -050080 This macro is used to access the :attr:`ob_size` member of a Python object.
Gregory P. Smith1b244652015-04-14 11:12:53 -070081 It expands to::
82
83 (((PyVarObject*)(o))->ob_size)
Georg Brandl54a3faa2008-01-20 09:30:57 +000084
Jeroen Ruigrok van der Werven939c1782009-04-26 20:25:45 +000085
Georg Brandl60203b42010-10-06 10:11:56 +000086.. c:macro:: PyObject_HEAD_INIT(type)
Jeroen Ruigrok van der Werven939c1782009-04-26 20:25:45 +000087
88 This is a macro which expands to initialization values for a new
Georg Brandl60203b42010-10-06 10:11:56 +000089 :c:type:`PyObject` type. This macro expands to::
Jeroen Ruigrok van der Werven939c1782009-04-26 20:25:45 +000090
91 _PyObject_EXTRA_INIT
92 1, type,
93
94
Georg Brandl60203b42010-10-06 10:11:56 +000095.. c:macro:: PyVarObject_HEAD_INIT(type, size)
Jeroen Ruigrok van der Werven939c1782009-04-26 20:25:45 +000096
97 This is a macro which expands to initialization values for a new
Georg Brandl60203b42010-10-06 10:11:56 +000098 :c:type:`PyVarObject` type, including the :attr:`ob_size` field.
Jeroen Ruigrok van der Werven939c1782009-04-26 20:25:45 +000099 This macro expands to::
100
101 _PyObject_EXTRA_INIT
102 1, type, size,
Georg Brandl54a3faa2008-01-20 09:30:57 +0000103
104
Georg Brandl60203b42010-10-06 10:11:56 +0000105.. c:type:: PyCFunction
Georg Brandl54a3faa2008-01-20 09:30:57 +0000106
Jeroen Ruigrok van der Wervenf4a9f962009-04-26 20:21:12 +0000107 Type of the functions used to implement most Python callables in C.
Georg Brandl60203b42010-10-06 10:11:56 +0000108 Functions of this type take two :c:type:`PyObject\*` parameters and return
Jeroen Ruigrok van der Wervenf4a9f962009-04-26 20:21:12 +0000109 one such value. If the return value is *NULL*, an exception shall have
110 been set. If not *NULL*, the return value is interpreted as the return
111 value of the function as exposed in Python. The function must return a new
112 reference.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000113
114
Georg Brandl60203b42010-10-06 10:11:56 +0000115.. c:type:: PyCFunctionWithKeywords
Georg Brandl54a3faa2008-01-20 09:30:57 +0000116
Jeroen Demeyer5600b5e2019-06-16 19:03:23 +0200117 Type of the functions used to implement Python callables in C
118 with signature :const:`METH_VARARGS | METH_KEYWORDS`.
119
120
121.. c:type:: _PyCFunctionFast
122
123 Type of the functions used to implement Python callables in C
124 with signature :const:`METH_FASTCALL`.
125
126
127.. c:type:: _PyCFunctionFastWithKeywords
128
129 Type of the functions used to implement Python callables in C
130 with signature :const:`METH_FASTCALL | METH_KEYWORDS`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000131
132
Georg Brandl60203b42010-10-06 10:11:56 +0000133.. c:type:: PyMethodDef
Georg Brandl54a3faa2008-01-20 09:30:57 +0000134
135 Structure used to describe a method of an extension type. This structure has
136 four fields:
137
Serhiy Storchaka84b8e922017-03-30 10:01:03 +0300138 +------------------+---------------+-------------------------------+
139 | Field | C Type | Meaning |
140 +==================+===============+===============================+
141 | :attr:`ml_name` | const char \* | name of the method |
142 +------------------+---------------+-------------------------------+
143 | :attr:`ml_meth` | PyCFunction | pointer to the C |
144 | | | implementation |
145 +------------------+---------------+-------------------------------+
146 | :attr:`ml_flags` | int | flag bits indicating how the |
147 | | | call should be constructed |
148 +------------------+---------------+-------------------------------+
149 | :attr:`ml_doc` | const char \* | points to the contents of the |
150 | | | docstring |
151 +------------------+---------------+-------------------------------+
Georg Brandl54a3faa2008-01-20 09:30:57 +0000152
153The :attr:`ml_meth` is a C function pointer. The functions may be of different
Georg Brandl60203b42010-10-06 10:11:56 +0000154types, but they always return :c:type:`PyObject\*`. If the function is not of
155the :c:type:`PyCFunction`, the compiler will require a cast in the method table.
156Even though :c:type:`PyCFunction` defines the first parameter as
Benjamin Peterson82f34ad2015-01-13 09:17:24 -0500157:c:type:`PyObject\*`, it is common that the method implementation uses the
Georg Brandl54a3faa2008-01-20 09:30:57 +0000158specific C type of the *self* object.
159
160The :attr:`ml_flags` field is a bitfield which can include the following flags.
161The individual flags indicate either a calling convention or a binding
Jeroen Demeyer5600b5e2019-06-16 19:03:23 +0200162convention.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000163
Jeroen Demeyer5600b5e2019-06-16 19:03:23 +0200164There are four basic calling conventions for positional arguments
165and two of them can be combined with :const:`METH_KEYWORDS` to support
166also keyword arguments. So there are a total of 6 calling conventions:
Georg Brandl54a3faa2008-01-20 09:30:57 +0000167
168.. data:: METH_VARARGS
169
170 This is the typical calling convention, where the methods have the type
Georg Brandl60203b42010-10-06 10:11:56 +0000171 :c:type:`PyCFunction`. The function expects two :c:type:`PyObject\*` values.
Georg Brandl21dc5ba2009-07-11 10:43:08 +0000172 The first one is the *self* object for methods; for module functions, it is
173 the module object. The second parameter (often called *args*) is a tuple
174 object representing all arguments. This parameter is typically processed
Georg Brandl60203b42010-10-06 10:11:56 +0000175 using :c:func:`PyArg_ParseTuple` or :c:func:`PyArg_UnpackTuple`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000176
177
Jeroen Demeyer5600b5e2019-06-16 19:03:23 +0200178.. data:: METH_VARARGS | METH_KEYWORDS
Georg Brandl54a3faa2008-01-20 09:30:57 +0000179
Georg Brandl60203b42010-10-06 10:11:56 +0000180 Methods with these flags must be of type :c:type:`PyCFunctionWithKeywords`.
Jeroen Demeyer5600b5e2019-06-16 19:03:23 +0200181 The function expects three parameters: *self*, *args*, *kwargs* where
182 *kwargs* is a dictionary of all the keyword arguments or possibly *NULL*
183 if there are no keyword arguments. The parameters are typically processed
184 using :c:func:`PyArg_ParseTupleAndKeywords`.
185
186
187.. data:: METH_FASTCALL
188
189 Fast calling convention supporting only positional arguments.
190 The methods have the type :c:type:`_PyCFunctionFast`.
191 The first parameter is *self*, the second parameter is a C array
192 of :c:type:`PyObject\*` values indicating the arguments and the third
193 parameter is the number of arguments (the length of the array).
194
195 This is not part of the :ref:`limited API <stable>`.
196
197 .. versionadded:: 3.7
198
199
200.. data:: METH_FASTCALL | METH_KEYWORDS
201
202 Extension of :const:`METH_FASTCALL` supporting also keyword arguments,
203 with methods of type :c:type:`_PyCFunctionFastWithKeywords`.
204 Keyword arguments are passed the same way as in the vectorcall protocol:
205 there is an additional fourth :c:type:`PyObject\*` parameter
206 which is a tuple representing the names of the keyword arguments
Jeroen Demeyer05677862019-08-16 12:41:27 +0200207 (which are guaranteed to be strings)
Jeroen Demeyer5600b5e2019-06-16 19:03:23 +0200208 or possibly *NULL* if there are no keywords. The values of the keyword
209 arguments are stored in the *args* array, after the positional arguments.
210
211 This is not part of the :ref:`limited API <stable>`.
212
213 .. versionadded:: 3.7
Georg Brandl54a3faa2008-01-20 09:30:57 +0000214
215
216.. data:: METH_NOARGS
217
218 Methods without parameters don't need to check whether arguments are given if
219 they are listed with the :const:`METH_NOARGS` flag. They need to be of type
Georg Brandl60203b42010-10-06 10:11:56 +0000220 :c:type:`PyCFunction`. The first parameter is typically named *self* and will
Georg Brandl21dc5ba2009-07-11 10:43:08 +0000221 hold a reference to the module or object instance. In all cases the second
222 parameter will be *NULL*.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000223
224
225.. data:: METH_O
226
227 Methods with a single object argument can be listed with the :const:`METH_O`
Georg Brandl60203b42010-10-06 10:11:56 +0000228 flag, instead of invoking :c:func:`PyArg_ParseTuple` with a ``"O"`` argument.
229 They have the type :c:type:`PyCFunction`, with the *self* parameter, and a
230 :c:type:`PyObject\*` parameter representing the single argument.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000231
232
233These two constants are not used to indicate the calling convention but the
234binding when use with methods of classes. These may not be used for functions
235defined for modules. At most one of these flags may be set for any given
236method.
237
238
239.. data:: METH_CLASS
240
241 .. index:: builtin: classmethod
242
Jeroen Ruigrok van der Wervenf4a9f962009-04-26 20:21:12 +0000243 The method will be passed the type object as the first parameter rather
244 than an instance of the type. This is used to create *class methods*,
245 similar to what is created when using the :func:`classmethod` built-in
246 function.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000247
248
249.. data:: METH_STATIC
250
251 .. index:: builtin: staticmethod
252
Jeroen Ruigrok van der Wervenf4a9f962009-04-26 20:21:12 +0000253 The method will be passed *NULL* as the first parameter rather than an
254 instance of the type. This is used to create *static methods*, similar to
255 what is created when using the :func:`staticmethod` built-in function.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000256
257One other constant controls whether a method is loaded in place of another
258definition with the same method name.
259
260
261.. data:: METH_COEXIST
262
263 The method will be loaded in place of existing definitions. Without
264 *METH_COEXIST*, the default is to skip repeated definitions. Since slot
Jeroen Ruigrok van der Wervenf4a9f962009-04-26 20:21:12 +0000265 wrappers are loaded before the method table, the existence of a
266 *sq_contains* slot, for example, would generate a wrapped method named
267 :meth:`__contains__` and preclude the loading of a corresponding
268 PyCFunction with the same name. With the flag defined, the PyCFunction
269 will be loaded in place of the wrapper object and will co-exist with the
270 slot. This is helpful because calls to PyCFunctions are optimized more
271 than wrapper object calls.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000272
Georg Brandl1f01deb2009-01-03 22:47:39 +0000273
Georg Brandl60203b42010-10-06 10:11:56 +0000274.. c:type:: PyMemberDef
Georg Brandl1f01deb2009-01-03 22:47:39 +0000275
276 Structure which describes an attribute of a type which corresponds to a C
277 struct member. Its fields are:
278
Serhiy Storchaka84b8e922017-03-30 10:01:03 +0300279 +------------------+---------------+-------------------------------+
280 | Field | C Type | Meaning |
281 +==================+===============+===============================+
282 | :attr:`name` | const char \* | name of the member |
283 +------------------+---------------+-------------------------------+
284 | :attr:`!type` | int | the type of the member in the |
285 | | | C struct |
286 +------------------+---------------+-------------------------------+
287 | :attr:`offset` | Py_ssize_t | the offset in bytes that the |
288 | | | member is located on the |
289 | | | type's object struct |
290 +------------------+---------------+-------------------------------+
291 | :attr:`flags` | int | flag bits indicating if the |
292 | | | field should be read-only or |
293 | | | writable |
294 +------------------+---------------+-------------------------------+
295 | :attr:`doc` | const char \* | points to the contents of the |
296 | | | docstring |
297 +------------------+---------------+-------------------------------+
Georg Brandl1f01deb2009-01-03 22:47:39 +0000298
csabellac3c7ef02017-03-29 20:27:50 -0400299 :attr:`!type` can be one of many ``T_`` macros corresponding to various C
Georg Brandl1f01deb2009-01-03 22:47:39 +0000300 types. When the member is accessed in Python, it will be converted to the
301 equivalent Python type.
302
303 =============== ==================
304 Macro name C type
305 =============== ==================
306 T_SHORT short
307 T_INT int
308 T_LONG long
309 T_FLOAT float
310 T_DOUBLE double
Serhiy Storchaka84b8e922017-03-30 10:01:03 +0300311 T_STRING const char \*
Georg Brandl1f01deb2009-01-03 22:47:39 +0000312 T_OBJECT PyObject \*
313 T_OBJECT_EX PyObject \*
314 T_CHAR char
315 T_BYTE char
Benjamin Petersond23f8222009-04-05 19:13:16 +0000316 T_UBYTE unsigned char
Georg Brandl1f01deb2009-01-03 22:47:39 +0000317 T_UINT unsigned int
318 T_USHORT unsigned short
319 T_ULONG unsigned long
320 T_BOOL char
321 T_LONGLONG long long
322 T_ULONGLONG unsigned long long
323 T_PYSSIZET Py_ssize_t
324 =============== ==================
325
Georg Brandl60203b42010-10-06 10:11:56 +0000326 :c:macro:`T_OBJECT` and :c:macro:`T_OBJECT_EX` differ in that
327 :c:macro:`T_OBJECT` returns ``None`` if the member is *NULL* and
328 :c:macro:`T_OBJECT_EX` raises an :exc:`AttributeError`. Try to use
329 :c:macro:`T_OBJECT_EX` over :c:macro:`T_OBJECT` because :c:macro:`T_OBJECT_EX`
Ezio Melotti479def32010-01-03 09:11:59 +0000330 handles use of the :keyword:`del` statement on that attribute more correctly
Georg Brandl60203b42010-10-06 10:11:56 +0000331 than :c:macro:`T_OBJECT`.
Georg Brandl1f01deb2009-01-03 22:47:39 +0000332
Serhiy Storchaka1ecf7d22016-10-27 21:41:19 +0300333 :attr:`flags` can be ``0`` for write and read access or :c:macro:`READONLY` for
Georg Brandl60203b42010-10-06 10:11:56 +0000334 read-only access. Using :c:macro:`T_STRING` for :attr:`type` implies
Windson yang689d5552018-11-18 03:16:51 +0800335 :c:macro:`READONLY`. :c:macro:`T_STRING` data is interpreted as UTF-8.
336 Only :c:macro:`T_OBJECT` and :c:macro:`T_OBJECT_EX`
Georg Brandl1f01deb2009-01-03 22:47:39 +0000337 members can be deleted. (They are set to *NULL*).
Michael Seifertda67e0d2017-09-15 18:25:27 +0200338
339
340.. c:type:: PyGetSetDef
341
342 Structure to define property-like access for a type. See also description of
343 the :c:member:`PyTypeObject.tp_getset` slot.
344
345 +-------------+------------------+-----------------------------------+
346 | Field | C Type | Meaning |
347 +=============+==================+===================================+
348 | name | const char \* | attribute name |
349 +-------------+------------------+-----------------------------------+
350 | get | getter | C Function to get the attribute |
351 +-------------+------------------+-----------------------------------+
352 | set | setter | optional C function to set or |
353 | | | delete the attribute, if omitted |
354 | | | the attribute is readonly |
355 +-------------+------------------+-----------------------------------+
356 | doc | const char \* | optional docstring |
357 +-------------+------------------+-----------------------------------+
358 | closure | void \* | optional function pointer, |
359 | | | providing additional data for |
360 | | | getter and setter |
361 +-------------+------------------+-----------------------------------+
362
363 The ``get`` function takes one :c:type:`PyObject\*` parameter (the
364 instance) and a function pointer (the associated ``closure``)::
365
366 typedef PyObject *(*getter)(PyObject *, void *);
367
368 It should return a new reference on success or *NULL* with a set exception
369 on failure.
370
371 ``set`` functions take two :c:type:`PyObject\*` parameters (the instance and
372 the value to be set) and a function pointer (the associated ``closure``)::
373
374 typedef int (*setter)(PyObject *, PyObject *, void *);
375
376 In case the attribute should be deleted the second parameter is *NULL*.
377 Should return ``0`` on success or ``-1`` with a set exception on failure.