blob: 704896883bcc568329fdc539fa6b263c4b1f4af2 [file] [log] [blame]
Georg Brandl54a3faa2008-01-20 09:30:57 +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 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
14:ctype:`PyObject` and :ctype:`PyVarObject` types, which are defined, in turn,
15by 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
19.. ctype:: PyObject
20
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
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 Brandl54a3faa2008-01-20 09:30:57 +000027
28
29.. ctype:: PyVarObject
30
Jeroen Ruigrok van der Wervenf4a9f962009-04-26 20:21:12 +000031 This is an extension of :ctype:`PyObject` that adds the :attr:`ob_size`
32 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 Brandl54a3faa2008-01-20 09:30:57 +000035
36These macros are used in the definition of :ctype:`PyObject` and
37:ctype:`PyVarObject`:
38
39.. XXX need to document PEP 3123 changes here
40
41.. cmacro:: PyObject_HEAD
42
43 This is a macro which expands to the declarations of the fields of the
44 :ctype:`PyObject` type; it is used when declaring new types which represent
Jeroen Ruigrok van der Wervenf4a9f962009-04-26 20:21:12 +000045 objects without a varying length. The specific fields it expands to depend
46 on the definition of :cmacro:`Py_TRACE_REFS`. By default, that macro is
47 not defined, and :cmacro:`PyObject_HEAD` expands to::
Georg Brandl54a3faa2008-01-20 09:30:57 +000048
49 Py_ssize_t ob_refcnt;
50 PyTypeObject *ob_type;
51
52 When :cmacro:`Py_TRACE_REFS` is defined, it expands to::
53
54 PyObject *_ob_next, *_ob_prev;
55 Py_ssize_t ob_refcnt;
56 PyTypeObject *ob_type;
57
58
59.. cmacro:: PyObject_VAR_HEAD
60
61 This is a macro which expands to the declarations of the fields of the
Jeroen Ruigrok van der Wervenf4a9f962009-04-26 20:21:12 +000062 :ctype:`PyVarObject` type; it is used when declaring new types which
63 represent objects with a length that varies from instance to instance.
64 This macro always expands to::
Georg Brandl54a3faa2008-01-20 09:30:57 +000065
66 PyObject_HEAD
67 Py_ssize_t ob_size;
68
69 Note that :cmacro:`PyObject_HEAD` is part of the expansion, and that its own
70 expansion varies depending on the definition of :cmacro:`Py_TRACE_REFS`.
71
Jeroen Ruigrok van der Werven939c1782009-04-26 20:25:45 +000072
73.. cmacro:: PyObject_HEAD_INIT(type)
74
75 This is a macro which expands to initialization values for a new
76 :ctype:`PyObject` type. This macro expands to::
77
78 _PyObject_EXTRA_INIT
79 1, type,
80
81
82.. cmacro:: PyVarObject_HEAD_INIT(type, size)
83
84 This is a macro which expands to initialization values for a new
85 :ctype:`PyVarObject` type, including the :attr:`ob_size` field.
86 This macro expands to::
87
88 _PyObject_EXTRA_INIT
89 1, type, size,
Georg Brandl54a3faa2008-01-20 09:30:57 +000090
91
92.. ctype:: PyCFunction
93
Jeroen Ruigrok van der Wervenf4a9f962009-04-26 20:21:12 +000094 Type of the functions used to implement most Python callables in C.
95 Functions of this type take two :ctype:`PyObject\*` parameters and return
96 one such value. If the return value is *NULL*, an exception shall have
97 been set. If not *NULL*, the return value is interpreted as the return
98 value of the function as exposed in Python. The function must return a new
99 reference.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000100
101
102.. ctype:: PyCFunctionWithKeywords
103
104 Type of the functions used to implement Python callables in C that take
105 keyword arguments: they take three :ctype:`PyObject\*` parameters and return
106 one such value. See :ctype:`PyCFunction` above for the meaning of the return
107 value.
108
109
110.. ctype:: PyMethodDef
111
112 Structure used to describe a method of an extension type. This structure has
113 four fields:
114
115 +------------------+-------------+-------------------------------+
116 | Field | C Type | Meaning |
117 +==================+=============+===============================+
118 | :attr:`ml_name` | char \* | name of the method |
119 +------------------+-------------+-------------------------------+
120 | :attr:`ml_meth` | PyCFunction | pointer to the C |
121 | | | implementation |
122 +------------------+-------------+-------------------------------+
123 | :attr:`ml_flags` | int | flag bits indicating how the |
124 | | | call should be constructed |
125 +------------------+-------------+-------------------------------+
126 | :attr:`ml_doc` | char \* | points to the contents of the |
127 | | | docstring |
128 +------------------+-------------+-------------------------------+
129
130The :attr:`ml_meth` is a C function pointer. The functions may be of different
131types, but they always return :ctype:`PyObject\*`. If the function is not of
132the :ctype:`PyCFunction`, the compiler will require a cast in the method table.
133Even though :ctype:`PyCFunction` defines the first parameter as
134:ctype:`PyObject\*`, it is common that the method implementation uses a the
135specific C type of the *self* object.
136
137The :attr:`ml_flags` field is a bitfield which can include the following flags.
138The individual flags indicate either a calling convention or a binding
139convention. Of the calling convention flags, only :const:`METH_VARARGS` and
140:const:`METH_KEYWORDS` can be combined (but note that :const:`METH_KEYWORDS`
141alone is equivalent to ``METH_VARARGS | METH_KEYWORDS``). Any of the calling
142convention flags can be combined with a binding flag.
143
144
145.. data:: METH_VARARGS
146
147 This is the typical calling convention, where the methods have the type
Jeroen Ruigrok van der Wervenf4a9f962009-04-26 20:21:12 +0000148 :ctype:`PyCFunction`. The function expects two :ctype:`PyObject\*` values.
149 The first one is the *self* object for methods; for module functions, it
150 has the value given to :cfunc:`Py_InitModule4` (or *NULL* if
151 :cfunc:`Py_InitModule` was used). The second parameter (often called
152 *args*) is a tuple object representing all arguments. This parameter is
153 typically processed using :cfunc:`PyArg_ParseTuple` or
154 :cfunc:`PyArg_UnpackTuple`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000155
156
157.. data:: METH_KEYWORDS
158
Jeroen Ruigrok van der Wervenf4a9f962009-04-26 20:21:12 +0000159 Methods with these flags must be of type :ctype:`PyCFunctionWithKeywords`.
160 The function expects three parameters: *self*, *args*, and a dictionary of
161 all the keyword arguments. The flag is typically combined with
162 :const:`METH_VARARGS`, and the parameters are typically processed using
Georg Brandl54a3faa2008-01-20 09:30:57 +0000163 :cfunc:`PyArg_ParseTupleAndKeywords`.
164
165
166.. data:: METH_NOARGS
167
168 Methods without parameters don't need to check whether arguments are given if
169 they are listed with the :const:`METH_NOARGS` flag. They need to be of type
170 :ctype:`PyCFunction`. When used with object methods, the first parameter is
Jeroen Ruigrok van der Wervenf4a9f962009-04-26 20:21:12 +0000171 typically named ``self`` and will hold a reference to the object instance.
172 In all cases the second parameter will be *NULL*.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000173
174
175.. data:: METH_O
176
177 Methods with a single object argument can be listed with the :const:`METH_O`
178 flag, instead of invoking :cfunc:`PyArg_ParseTuple` with a ``"O"`` argument.
179 They have the type :ctype:`PyCFunction`, with the *self* parameter, and a
180 :ctype:`PyObject\*` parameter representing the single argument.
181
182
183These two constants are not used to indicate the calling convention but the
184binding when use with methods of classes. These may not be used for functions
185defined for modules. At most one of these flags may be set for any given
186method.
187
188
189.. data:: METH_CLASS
190
191 .. index:: builtin: classmethod
192
Jeroen Ruigrok van der Wervenf4a9f962009-04-26 20:21:12 +0000193 The method will be passed the type object as the first parameter rather
194 than an instance of the type. This is used to create *class methods*,
195 similar to what is created when using the :func:`classmethod` built-in
196 function.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000197
198
199.. data:: METH_STATIC
200
201 .. index:: builtin: staticmethod
202
Jeroen Ruigrok van der Wervenf4a9f962009-04-26 20:21:12 +0000203 The method will be passed *NULL* as the first parameter rather than an
204 instance of the type. This is used to create *static methods*, similar to
205 what is created when using the :func:`staticmethod` built-in function.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000206
207One other constant controls whether a method is loaded in place of another
208definition with the same method name.
209
210
211.. data:: METH_COEXIST
212
213 The method will be loaded in place of existing definitions. Without
214 *METH_COEXIST*, the default is to skip repeated definitions. Since slot
Jeroen Ruigrok van der Wervenf4a9f962009-04-26 20:21:12 +0000215 wrappers are loaded before the method table, the existence of a
216 *sq_contains* slot, for example, would generate a wrapped method named
217 :meth:`__contains__` and preclude the loading of a corresponding
218 PyCFunction with the same name. With the flag defined, the PyCFunction
219 will be loaded in place of the wrapper object and will co-exist with the
220 slot. This is helpful because calls to PyCFunctions are optimized more
221 than wrapper object calls.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000222
Georg Brandl1f01deb2009-01-03 22:47:39 +0000223
224.. ctype:: PyMemberDef
225
226 Structure which describes an attribute of a type which corresponds to a C
227 struct member. Its fields are:
228
229 +------------------+-------------+-------------------------------+
230 | Field | C Type | Meaning |
231 +==================+=============+===============================+
232 | :attr:`name` | char \* | name of the member |
233 +------------------+-------------+-------------------------------+
234 | :attr:`type` | int | the type of the member in the |
235 | | | C struct |
236 +------------------+-------------+-------------------------------+
237 | :attr:`offset` | Py_ssize_t | the offset in bytes that the |
238 | | | member is located on the |
239 | | | type's object struct |
240 +------------------+-------------+-------------------------------+
241 | :attr:`flags` | int | flag bits indicating if the |
242 | | | field should be read-only or |
243 | | | writable |
244 +------------------+-------------+-------------------------------+
245 | :attr:`doc` | char \* | points to the contents of the |
246 | | | docstring |
247 +------------------+-------------+-------------------------------+
248
249 :attr:`type` can be one of many ``T_`` macros corresponding to various C
250 types. When the member is accessed in Python, it will be converted to the
251 equivalent Python type.
252
253 =============== ==================
254 Macro name C type
255 =============== ==================
256 T_SHORT short
257 T_INT int
258 T_LONG long
259 T_FLOAT float
260 T_DOUBLE double
261 T_STRING char \*
262 T_OBJECT PyObject \*
263 T_OBJECT_EX PyObject \*
264 T_CHAR char
265 T_BYTE char
Benjamin Petersond23f8222009-04-05 19:13:16 +0000266 T_UBYTE unsigned char
Georg Brandl1f01deb2009-01-03 22:47:39 +0000267 T_UINT unsigned int
268 T_USHORT unsigned short
269 T_ULONG unsigned long
270 T_BOOL char
271 T_LONGLONG long long
272 T_ULONGLONG unsigned long long
273 T_PYSSIZET Py_ssize_t
274 =============== ==================
275
276 :cmacro:`T_OBJECT` and :cmacro:`T_OBJECT_EX` differ in that
277 :cmacro:`T_OBJECT` returns ``None`` if the member is *NULL* and
278 :cmacro:`T_OBJECT_EX` raises an :exc:`AttributeError`.
279
280 :attr:`flags` can be 0 for write and read access or :cmacro:`READONLY` for
281 read-only access. Using :cmacro:`T_STRING` for :attr:`type` implies
282 :cmacro:`READONLY`. Only :cmacro:`T_OBJECT` and :cmacro:`T_OBJECT_EX`
283 members can be deleted. (They are set to *NULL*).