blob: c6ec685026a1a6ef0422340bad8985e1067400a0 [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
162:const:`METH_KEYWORDS` can be combined (but note that :const:`METH_KEYWORDS`
163alone is equivalent to ``METH_VARARGS | METH_KEYWORDS``). Any of the calling
164convention flags can be combined with a binding flag.
165
166
167.. data:: METH_VARARGS
168
169 This is the typical calling convention, where the methods have the type
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100170 :c:type:`PyCFunction`. The function expects two :c:type:`PyObject\*` values.
Georg Brandl749930f2010-11-12 19:45:46 +0000171 The first one is the *self* object for methods; for module functions, it is
172 the module object. The second parameter (often called *args*) is a tuple
173 object representing all arguments. This parameter is typically processed
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100174 using :c:func:`PyArg_ParseTuple` or :c:func:`PyArg_UnpackTuple`.
Georg Brandlf6842722008-01-19 22:08:21 +0000175
176
177.. data:: METH_KEYWORDS
178
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100179 Methods with these flags must be of type :c:type:`PyCFunctionWithKeywords`.
Jeroen Ruigrok van der Wervenbc25bf92009-04-25 11:15:06 +0000180 The function expects three parameters: *self*, *args*, and a dictionary of
181 all the keyword arguments. The flag is typically combined with
182 :const:`METH_VARARGS`, and the parameters are typically processed using
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100183 :c:func:`PyArg_ParseTupleAndKeywords`.
Georg Brandlf6842722008-01-19 22:08:21 +0000184
185
186.. data:: METH_NOARGS
187
188 Methods without parameters don't need to check whether arguments are given if
189 they are listed with the :const:`METH_NOARGS` flag. They need to be of type
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100190 :c:type:`PyCFunction`. The first parameter is typically named ``self`` and
Georg Brandl749930f2010-11-12 19:45:46 +0000191 will hold a reference to the module or object instance. In all cases the
192 second parameter will be *NULL*.
Georg Brandlf6842722008-01-19 22:08:21 +0000193
194
195.. data:: METH_O
196
197 Methods with a single object argument can be listed with the :const:`METH_O`
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100198 flag, instead of invoking :c:func:`PyArg_ParseTuple` with a ``"O"`` argument.
199 They have the type :c:type:`PyCFunction`, with the *self* parameter, and a
200 :c:type:`PyObject\*` parameter representing the single argument.
Georg Brandlf6842722008-01-19 22:08:21 +0000201
202
203.. data:: METH_OLDARGS
204
205 This calling convention is deprecated. The method must be of type
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100206 :c:type:`PyCFunction`. The second argument is *NULL* if no arguments are
Jeroen Ruigrok van der Wervenbc25bf92009-04-25 11:15:06 +0000207 given, a single object if exactly one argument is given, and a tuple of
208 objects if more than one argument is given. There is no way for a function
209 using this convention to distinguish between a call with multiple arguments
210 and a call with a tuple as the only argument.
Georg Brandlf6842722008-01-19 22:08:21 +0000211
212These two constants are not used to indicate the calling convention but the
213binding when use with methods of classes. These may not be used for functions
214defined for modules. At most one of these flags may be set for any given
215method.
216
217
218.. data:: METH_CLASS
219
220 .. index:: builtin: classmethod
221
Jeroen Ruigrok van der Wervenbc25bf92009-04-25 11:15:06 +0000222 The method will be passed the type object as the first parameter rather
223 than an instance of the type. This is used to create *class methods*,
224 similar to what is created when using the :func:`classmethod` built-in
225 function.
Georg Brandlf6842722008-01-19 22:08:21 +0000226
227 .. versionadded:: 2.3
228
229
230.. data:: METH_STATIC
231
232 .. index:: builtin: staticmethod
233
Jeroen Ruigrok van der Wervenbc25bf92009-04-25 11:15:06 +0000234 The method will be passed *NULL* as the first parameter rather than an
235 instance of the type. This is used to create *static methods*, similar to
236 what is created when using the :func:`staticmethod` built-in function.
Georg Brandlf6842722008-01-19 22:08:21 +0000237
238 .. versionadded:: 2.3
239
240One other constant controls whether a method is loaded in place of another
241definition with the same method name.
242
243
244.. data:: METH_COEXIST
245
246 The method will be loaded in place of existing definitions. Without
247 *METH_COEXIST*, the default is to skip repeated definitions. Since slot
Jeroen Ruigrok van der Wervenbc25bf92009-04-25 11:15:06 +0000248 wrappers are loaded before the method table, the existence of a
249 *sq_contains* slot, for example, would generate a wrapped method named
250 :meth:`__contains__` and preclude the loading of a corresponding
251 PyCFunction with the same name. With the flag defined, the PyCFunction
252 will be loaded in place of the wrapper object and will co-exist with the
253 slot. This is helpful because calls to PyCFunctions are optimized more
254 than wrapper object calls.
Georg Brandlf6842722008-01-19 22:08:21 +0000255
256 .. versionadded:: 2.4
257
258
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100259.. c:type:: PyMemberDef
Benjamin Peterson0132ee342009-01-02 18:26:23 +0000260
261 Structure which describes an attribute of a type which corresponds to a C
Georg Brandlc63785d2009-01-03 21:52:16 +0000262 struct member. Its fields are:
Benjamin Peterson0132ee342009-01-02 18:26:23 +0000263
264 +------------------+-------------+-------------------------------+
265 | Field | C Type | Meaning |
266 +==================+=============+===============================+
267 | :attr:`name` | char \* | name of the member |
268 +------------------+-------------+-------------------------------+
269 | :attr:`type` | int | the type of the member in the |
270 | | | C struct |
271 +------------------+-------------+-------------------------------+
272 | :attr:`offset` | Py_ssize_t | the offset in bytes that the |
273 | | | member is located on the |
274 | | | type's object struct |
275 +------------------+-------------+-------------------------------+
276 | :attr:`flags` | int | flag bits indicating if the |
277 | | | field should be read-only or |
278 | | | writable |
279 +------------------+-------------+-------------------------------+
280 | :attr:`doc` | char \* | points to the contents of the |
281 | | | docstring |
282 +------------------+-------------+-------------------------------+
283
284 :attr:`type` can be one of many ``T_`` macros corresponding to various C
285 types. When the member is accessed in Python, it will be converted to the
286 equivalent Python type.
287
288 =============== ==================
289 Macro name C type
290 =============== ==================
291 T_SHORT short
292 T_INT int
293 T_LONG long
294 T_FLOAT float
295 T_DOUBLE double
296 T_STRING char \*
297 T_OBJECT PyObject \*
298 T_OBJECT_EX PyObject \*
299 T_CHAR char
300 T_BYTE char
Georg Brandlbdaa6a72009-03-31 19:30:56 +0000301 T_UBYTE unsigned char
Benjamin Peterson0132ee342009-01-02 18:26:23 +0000302 T_UINT unsigned int
303 T_USHORT unsigned short
304 T_ULONG unsigned long
305 T_BOOL char
306 T_LONGLONG long long
307 T_ULONGLONG unsigned long long
308 T_PYSSIZET Py_ssize_t
309 =============== ==================
310
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100311 :c:macro:`T_OBJECT` and :c:macro:`T_OBJECT_EX` differ in that
312 :c:macro:`T_OBJECT` returns ``None`` if the member is *NULL* and
313 :c:macro:`T_OBJECT_EX` raises an :exc:`AttributeError`. Try to use
314 :c:macro:`T_OBJECT_EX` over :c:macro:`T_OBJECT` because :c:macro:`T_OBJECT_EX`
Ezio Melotti722a8a92010-01-03 09:09:55 +0000315 handles use of the :keyword:`del` statement on that attribute more correctly
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100316 than :c:macro:`T_OBJECT`.
Benjamin Peterson0132ee342009-01-02 18:26:23 +0000317
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100318 :attr:`flags` can be 0 for write and read access or :c:macro:`READONLY` for
319 read-only access. Using :c:macro:`T_STRING` for :attr:`type` implies
320 :c:macro:`READONLY`. Only :c:macro:`T_OBJECT` and :c:macro:`T_OBJECT_EX`
Georg Brandlc63785d2009-01-03 21:52:16 +0000321 members can be deleted. (They are set to *NULL*).
Benjamin Peterson0132ee342009-01-02 18:26:23 +0000322
323
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100324.. c:function:: PyObject* Py_FindMethod(PyMethodDef table[], PyObject *ob, char *name)
Georg Brandlf6842722008-01-19 22:08:21 +0000325
Jeroen Ruigrok van der Wervenbc25bf92009-04-25 11:15:06 +0000326 Return a bound method object for an extension type implemented in C. This
Antoine Pitrou92fae552013-08-01 21:17:24 +0200327 can be useful in the implementation of a :c:member:`~PyTypeObject.tp_getattro` or
328 :c:member:`~PyTypeObject.tp_getattr` handler that does not use the
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100329 :c:func:`PyObject_GenericGetAttr` function.