blob: af6a8c38cd031a4ede93632ac1d5d83597c6b424 [file] [log] [blame]
Georg Brandlf6842722008-01-19 22:08:21 +00001.. highlightlang:: c
2
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 Wervenbc25bf92009-04-25 11:15:06 +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
Sandro Tosi98ed08f2012-01-14 16:42:02 +010014:c:type:`PyObject` and :c:type:`PyVarObject` types, which are defined, in turn,
Jeroen Ruigrok van der Wervenbc25bf92009-04-25 11:15:06 +000015by the expansions of some macros also used, whether directly or indirectly, in
16the definition of all other Python objects.
Georg Brandlf6842722008-01-19 22:08:21 +000017
18
Sandro Tosi98ed08f2012-01-14 16:42:02 +010019.. c:type:: PyObject
Georg Brandlf6842722008-01-19 22:08:21 +000020
Jeroen Ruigrok van der Wervenbc25bf92009-04-25 11:15:06 +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
24 reference count and a pointer to the corresponding type object. It
25 corresponds to the fields defined by the expansion of the ``PyObject_HEAD``
26 macro.
Georg Brandlf6842722008-01-19 22:08:21 +000027
28
Sandro Tosi98ed08f2012-01-14 16:42:02 +010029.. c:type:: PyVarObject
Georg Brandlf6842722008-01-19 22:08:21 +000030
Sandro Tosi98ed08f2012-01-14 16:42:02 +010031 This is an extension of :c:type:`PyObject` that adds the :attr:`ob_size`
Jeroen Ruigrok van der Wervenbc25bf92009-04-25 11:15:06 +000032 field. This is only used for objects that have some notion of *length*.
33 This type does not often appear in the Python/C API. It corresponds to the
34 fields defined by the expansion of the ``PyObject_VAR_HEAD`` macro.
Georg Brandlf6842722008-01-19 22:08:21 +000035
Sandro Tosi98ed08f2012-01-14 16:42:02 +010036These macros are used in the definition of :c:type:`PyObject` and
37:c:type:`PyVarObject`:
Georg Brandlf6842722008-01-19 22:08:21 +000038
39
Sandro Tosi98ed08f2012-01-14 16:42:02 +010040.. c:macro:: PyObject_HEAD
Georg Brandlf6842722008-01-19 22:08:21 +000041
42 This is a macro which expands to the declarations of the fields of the
Sandro Tosi98ed08f2012-01-14 16:42:02 +010043 :c:type:`PyObject` type; it is used when declaring new types which represent
Jeroen Ruigrok van der Wervenbc25bf92009-04-25 11:15:06 +000044 objects without a varying length. The specific fields it expands to depend
Sandro Tosi98ed08f2012-01-14 16:42:02 +010045 on the definition of :c:macro:`Py_TRACE_REFS`. By default, that macro is
46 not defined, and :c:macro:`PyObject_HEAD` expands to::
Georg Brandlf6842722008-01-19 22:08:21 +000047
48 Py_ssize_t ob_refcnt;
49 PyTypeObject *ob_type;
50
Sandro Tosi98ed08f2012-01-14 16:42:02 +010051 When :c:macro:`Py_TRACE_REFS` is defined, it expands to::
Georg Brandlf6842722008-01-19 22:08:21 +000052
53 PyObject *_ob_next, *_ob_prev;
54 Py_ssize_t ob_refcnt;
55 PyTypeObject *ob_type;
56
57
Sandro Tosi98ed08f2012-01-14 16:42:02 +010058.. c:macro:: PyObject_VAR_HEAD
Georg Brandlf6842722008-01-19 22:08:21 +000059
60 This is a macro which expands to the declarations of the fields of the
Sandro Tosi98ed08f2012-01-14 16:42:02 +010061 :c:type:`PyVarObject` type; it is used when declaring new types which
Jeroen Ruigrok van der Wervenbc25bf92009-04-25 11:15:06 +000062 represent objects with a length that varies from instance to instance.
63 This macro always expands to::
Georg Brandlf6842722008-01-19 22:08:21 +000064
65 PyObject_HEAD
66 Py_ssize_t ob_size;
67
Sandro Tosi98ed08f2012-01-14 16:42:02 +010068 Note that :c:macro:`PyObject_HEAD` is part of the expansion, and that its own
69 expansion varies depending on the definition of :c:macro:`Py_TRACE_REFS`.
Georg Brandlf6842722008-01-19 22:08:21 +000070
Jeroen Ruigrok van der Werven162641a2009-04-25 11:59:09 +000071
Gregory P. Smith20d4f1b2016-01-22 13:17:41 -080072.. c:macro:: Py_TYPE(o)
73
74 This macro is used to access the :attr:`ob_type` member of a Python object.
75 It expands to::
76
77 (((PyObject*)(o))->ob_type)
78
79 .. versionadded:: 2.6
80
81
82.. c:macro:: Py_REFCNT(o)
83
84 This macro is used to access the :attr:`ob_refcnt` member of a Python
85 object.
86 It expands to::
87
88 (((PyObject*)(o))->ob_refcnt)
89
90 .. versionadded:: 2.6
91
92
93.. c:macro:: Py_SIZE(o)
94
95 This macro is used to access the :attr:`ob_size` member of a Python object.
96 It expands to::
97
98 (((PyVarObject*)(o))->ob_size)
99
100 .. versionadded:: 2.6
101
102
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100103.. c:macro:: PyObject_HEAD_INIT(type)
Jeroen Ruigrok van der Werven162641a2009-04-25 11:59:09 +0000104
105 This is a macro which expands to initialization values for a new
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100106 :c:type:`PyObject` type. This macro expands to::
Jeroen Ruigrok van der Werven162641a2009-04-25 11:59:09 +0000107
108 _PyObject_EXTRA_INIT
109 1, type,
110
111
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100112.. c:macro:: PyVarObject_HEAD_INIT(type, size)
Jeroen Ruigrok van der Werven162641a2009-04-25 11:59:09 +0000113
114 This is a macro which expands to initialization values for a new
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100115 :c:type:`PyVarObject` type, including the :attr:`ob_size` field.
Jeroen Ruigrok van der Werven162641a2009-04-25 11:59:09 +0000116 This macro expands to::
117
118 _PyObject_EXTRA_INIT
119 1, type, size,
Georg Brandlf6842722008-01-19 22:08:21 +0000120
121
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100122.. c:type:: PyCFunction
Georg Brandlf6842722008-01-19 22:08:21 +0000123
Jeroen Ruigrok van der Wervenbc25bf92009-04-25 11:15:06 +0000124 Type of the functions used to implement most Python callables in C.
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100125 Functions of this type take two :c:type:`PyObject\*` parameters and return
Jeroen Ruigrok van der Wervenbc25bf92009-04-25 11:15:06 +0000126 one such value. If the return value is *NULL*, an exception shall have
127 been set. If not *NULL*, the return value is interpreted as the return
128 value of the function as exposed in Python. The function must return a new
129 reference.
Georg Brandlf6842722008-01-19 22:08:21 +0000130
131
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100132.. c:type:: PyMethodDef
Georg Brandlf6842722008-01-19 22:08:21 +0000133
134 Structure used to describe a method of an extension type. This structure has
135 four fields:
136
137 +------------------+-------------+-------------------------------+
138 | Field | C Type | Meaning |
139 +==================+=============+===============================+
140 | :attr:`ml_name` | char \* | name of the method |
141 +------------------+-------------+-------------------------------+
142 | :attr:`ml_meth` | PyCFunction | pointer to the C |
143 | | | implementation |
144 +------------------+-------------+-------------------------------+
145 | :attr:`ml_flags` | int | flag bits indicating how the |
146 | | | call should be constructed |
147 +------------------+-------------+-------------------------------+
148 | :attr:`ml_doc` | char \* | points to the contents of the |
149 | | | docstring |
150 +------------------+-------------+-------------------------------+
151
152The :attr:`ml_meth` is a C function pointer. The functions may be of different
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100153types, but they always return :c:type:`PyObject\*`. If the function is not of
154the :c:type:`PyCFunction`, the compiler will require a cast in the method table.
155Even though :c:type:`PyCFunction` defines the first parameter as
Benjamin Peterson07f90472015-01-13 09:17:24 -0500156:c:type:`PyObject\*`, it is common that the method implementation uses the
Georg Brandlf6842722008-01-19 22:08:21 +0000157specific C type of the *self* object.
158
159The :attr:`ml_flags` field is a bitfield which can include the following flags.
160The individual flags indicate either a calling convention or a binding
161convention. Of the calling convention flags, only :const:`METH_VARARGS` and
Berker Peksag0d865772016-06-12 16:37:57 +0300162:const:`METH_KEYWORDS` can be combined. Any of the calling convention flags
163can be combined with a binding flag.
Georg Brandlf6842722008-01-19 22:08:21 +0000164
165
166.. data:: METH_VARARGS
167
168 This is the typical calling convention, where the methods have the type
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100169 :c:type:`PyCFunction`. The function expects two :c:type:`PyObject\*` values.
Georg Brandl749930f2010-11-12 19:45:46 +0000170 The first one is the *self* object for methods; for module functions, it is
171 the module object. The second parameter (often called *args*) is a tuple
172 object representing all arguments. This parameter is typically processed
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100173 using :c:func:`PyArg_ParseTuple` or :c:func:`PyArg_UnpackTuple`.
Georg Brandlf6842722008-01-19 22:08:21 +0000174
175
176.. data:: METH_KEYWORDS
177
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100178 Methods with these flags must be of type :c:type:`PyCFunctionWithKeywords`.
Jeroen Ruigrok van der Wervenbc25bf92009-04-25 11:15:06 +0000179 The function expects three parameters: *self*, *args*, and a dictionary of
180 all the keyword arguments. The flag is typically combined with
181 :const:`METH_VARARGS`, and the parameters are typically processed using
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100182 :c:func:`PyArg_ParseTupleAndKeywords`.
Georg Brandlf6842722008-01-19 22:08:21 +0000183
184
185.. data:: METH_NOARGS
186
187 Methods without parameters don't need to check whether arguments are given if
188 they are listed with the :const:`METH_NOARGS` flag. They need to be of type
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100189 :c:type:`PyCFunction`. The first parameter is typically named ``self`` and
Georg Brandl749930f2010-11-12 19:45:46 +0000190 will hold a reference to the module or object instance. In all cases the
191 second parameter will be *NULL*.
Georg Brandlf6842722008-01-19 22:08:21 +0000192
193
194.. data:: METH_O
195
196 Methods with a single object argument can be listed with the :const:`METH_O`
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100197 flag, instead of invoking :c:func:`PyArg_ParseTuple` with a ``"O"`` argument.
198 They have the type :c:type:`PyCFunction`, with the *self* parameter, and a
199 :c:type:`PyObject\*` parameter representing the single argument.
Georg Brandlf6842722008-01-19 22:08:21 +0000200
201
202.. data:: METH_OLDARGS
203
204 This calling convention is deprecated. The method must be of type
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100205 :c:type:`PyCFunction`. The second argument is *NULL* if no arguments are
Jeroen Ruigrok van der Wervenbc25bf92009-04-25 11:15:06 +0000206 given, a single object if exactly one argument is given, and a tuple of
207 objects if more than one argument is given. There is no way for a function
208 using this convention to distinguish between a call with multiple arguments
209 and a call with a tuple as the only argument.
Georg Brandlf6842722008-01-19 22:08:21 +0000210
211These two constants are not used to indicate the calling convention but the
212binding when use with methods of classes. These may not be used for functions
213defined for modules. At most one of these flags may be set for any given
214method.
215
216
217.. data:: METH_CLASS
218
219 .. index:: builtin: classmethod
220
Jeroen Ruigrok van der Wervenbc25bf92009-04-25 11:15:06 +0000221 The method will be passed the type object as the first parameter rather
222 than an instance of the type. This is used to create *class methods*,
223 similar to what is created when using the :func:`classmethod` built-in
224 function.
Georg Brandlf6842722008-01-19 22:08:21 +0000225
226 .. versionadded:: 2.3
227
228
229.. data:: METH_STATIC
230
231 .. index:: builtin: staticmethod
232
Jeroen Ruigrok van der Wervenbc25bf92009-04-25 11:15:06 +0000233 The method will be passed *NULL* as the first parameter rather than an
234 instance of the type. This is used to create *static methods*, similar to
235 what is created when using the :func:`staticmethod` built-in function.
Georg Brandlf6842722008-01-19 22:08:21 +0000236
237 .. versionadded:: 2.3
238
239One other constant controls whether a method is loaded in place of another
240definition with the same method name.
241
242
243.. data:: METH_COEXIST
244
245 The method will be loaded in place of existing definitions. Without
246 *METH_COEXIST*, the default is to skip repeated definitions. Since slot
Jeroen Ruigrok van der Wervenbc25bf92009-04-25 11:15:06 +0000247 wrappers are loaded before the method table, the existence of a
248 *sq_contains* slot, for example, would generate a wrapped method named
249 :meth:`__contains__` and preclude the loading of a corresponding
250 PyCFunction with the same name. With the flag defined, the PyCFunction
251 will be loaded in place of the wrapper object and will co-exist with the
252 slot. This is helpful because calls to PyCFunctions are optimized more
253 than wrapper object calls.
Georg Brandlf6842722008-01-19 22:08:21 +0000254
255 .. versionadded:: 2.4
256
257
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100258.. c:type:: PyMemberDef
Benjamin Peterson0132ee342009-01-02 18:26:23 +0000259
260 Structure which describes an attribute of a type which corresponds to a C
Georg Brandlc63785d2009-01-03 21:52:16 +0000261 struct member. Its fields are:
Benjamin Peterson0132ee342009-01-02 18:26:23 +0000262
263 +------------------+-------------+-------------------------------+
264 | Field | C Type | Meaning |
265 +==================+=============+===============================+
266 | :attr:`name` | char \* | name of the member |
267 +------------------+-------------+-------------------------------+
Senthil Kumaran3ceca682017-03-29 22:29:12 -0700268 | :attr:`!type` | int | the type of the member in the |
Benjamin Peterson0132ee342009-01-02 18:26:23 +0000269 | | | C struct |
270 +------------------+-------------+-------------------------------+
271 | :attr:`offset` | Py_ssize_t | the offset in bytes that the |
272 | | | member is located on the |
273 | | | type's object struct |
274 +------------------+-------------+-------------------------------+
275 | :attr:`flags` | int | flag bits indicating if the |
276 | | | field should be read-only or |
277 | | | writable |
278 +------------------+-------------+-------------------------------+
279 | :attr:`doc` | char \* | points to the contents of the |
280 | | | docstring |
281 +------------------+-------------+-------------------------------+
282
Senthil Kumaran3ceca682017-03-29 22:29:12 -0700283 :attr:`!type` can be one of many ``T_`` macros corresponding to various C
Benjamin Peterson0132ee342009-01-02 18:26:23 +0000284 types. When the member is accessed in Python, it will be converted to the
285 equivalent Python type.
286
287 =============== ==================
288 Macro name C type
289 =============== ==================
290 T_SHORT short
291 T_INT int
292 T_LONG long
293 T_FLOAT float
294 T_DOUBLE double
295 T_STRING char \*
296 T_OBJECT PyObject \*
297 T_OBJECT_EX PyObject \*
298 T_CHAR char
299 T_BYTE char
Georg Brandlbdaa6a72009-03-31 19:30:56 +0000300 T_UBYTE unsigned char
Benjamin Peterson0132ee342009-01-02 18:26:23 +0000301 T_UINT unsigned int
302 T_USHORT unsigned short
303 T_ULONG unsigned long
304 T_BOOL char
305 T_LONGLONG long long
306 T_ULONGLONG unsigned long long
307 T_PYSSIZET Py_ssize_t
308 =============== ==================
309
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100310 :c:macro:`T_OBJECT` and :c:macro:`T_OBJECT_EX` differ in that
311 :c:macro:`T_OBJECT` returns ``None`` if the member is *NULL* and
312 :c:macro:`T_OBJECT_EX` raises an :exc:`AttributeError`. Try to use
313 :c:macro:`T_OBJECT_EX` over :c:macro:`T_OBJECT` because :c:macro:`T_OBJECT_EX`
Ezio Melotti722a8a92010-01-03 09:09:55 +0000314 handles use of the :keyword:`del` statement on that attribute more correctly
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100315 than :c:macro:`T_OBJECT`.
Benjamin Peterson0132ee342009-01-02 18:26:23 +0000316
Serhiy Storchakad585c522016-10-27 21:41:04 +0300317 :attr:`flags` can be ``0`` for write and read access or :c:macro:`READONLY` for
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100318 read-only access. Using :c:macro:`T_STRING` for :attr:`type` implies
319 :c:macro:`READONLY`. Only :c:macro:`T_OBJECT` and :c:macro:`T_OBJECT_EX`
Georg Brandlc63785d2009-01-03 21:52:16 +0000320 members can be deleted. (They are set to *NULL*).
Benjamin Peterson0132ee342009-01-02 18:26:23 +0000321
322
Michael Seifert41376242017-09-16 13:30:07 +0200323.. c:type:: PyGetSetDef
324
325 Structure to define property-like access for a type. See also description of
326 the :c:member:`PyTypeObject.tp_getset` slot.
327
328 +-------------+------------------+-----------------------------------+
329 | Field | C Type | Meaning |
330 +=============+==================+===================================+
331 | name | char \* | attribute name |
332 +-------------+------------------+-----------------------------------+
333 | get | getter | C Function to get the attribute |
334 +-------------+------------------+-----------------------------------+
335 | set | setter | optional C function to set or |
336 | | | delete the attribute, if omitted |
337 | | | the attribute is readonly |
338 +-------------+------------------+-----------------------------------+
339 | doc | char \* | optional docstring |
340 +-------------+------------------+-----------------------------------+
341 | closure | void \* | optional function pointer, |
342 | | | providing additional data for |
343 | | | getter and setter |
344 +-------------+------------------+-----------------------------------+
345
346 The ``get`` function takes one :c:type:`PyObject\*` parameter (the
347 instance) and a function pointer (the associated ``closure``)::
348
349 typedef PyObject *(*getter)(PyObject *, void *);
350
351 It should return a new reference on success or *NULL* with a set exception
352 on failure.
353
354 ``set`` functions take two :c:type:`PyObject\*` parameters (the instance and
355 the value to be set) and a function pointer (the associated ``closure``)::
356
357 typedef int (*setter)(PyObject *, PyObject *, void *);
358
359 In case the attribute should be deleted the second parameter is *NULL*.
360 Should return ``0`` on success or ``-1`` with a set exception on failure.
361
362
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100363.. c:function:: PyObject* Py_FindMethod(PyMethodDef table[], PyObject *ob, char *name)
Georg Brandlf6842722008-01-19 22:08:21 +0000364
Jeroen Ruigrok van der Wervenbc25bf92009-04-25 11:15:06 +0000365 Return a bound method object for an extension type implemented in C. This
Antoine Pitrou92fae552013-08-01 21:17:24 +0200366 can be useful in the implementation of a :c:member:`~PyTypeObject.tp_getattro` or
367 :c:member:`~PyTypeObject.tp_getattr` handler that does not use the
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100368 :c:func:`PyObject_GenericGetAttr` function.