blob: b654eb71d7ba80da9a888c6808fa0f1b8cf33f7f [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
72.. cmacro:: PyObject_HEAD_INIT
73
74
75.. ctype:: PyCFunction
76
Jeroen Ruigrok van der Wervenf4a9f962009-04-26 20:21:12 +000077 Type of the functions used to implement most Python callables in C.
78 Functions of this type take two :ctype:`PyObject\*` parameters and return
79 one such value. If the return value is *NULL*, an exception shall have
80 been set. If not *NULL*, the return value is interpreted as the return
81 value of the function as exposed in Python. The function must return a new
82 reference.
Georg Brandl54a3faa2008-01-20 09:30:57 +000083
84
85.. ctype:: PyCFunctionWithKeywords
86
87 Type of the functions used to implement Python callables in C that take
88 keyword arguments: they take three :ctype:`PyObject\*` parameters and return
89 one such value. See :ctype:`PyCFunction` above for the meaning of the return
90 value.
91
92
93.. ctype:: PyMethodDef
94
95 Structure used to describe a method of an extension type. This structure has
96 four fields:
97
98 +------------------+-------------+-------------------------------+
99 | Field | C Type | Meaning |
100 +==================+=============+===============================+
101 | :attr:`ml_name` | char \* | name of the method |
102 +------------------+-------------+-------------------------------+
103 | :attr:`ml_meth` | PyCFunction | pointer to the C |
104 | | | implementation |
105 +------------------+-------------+-------------------------------+
106 | :attr:`ml_flags` | int | flag bits indicating how the |
107 | | | call should be constructed |
108 +------------------+-------------+-------------------------------+
109 | :attr:`ml_doc` | char \* | points to the contents of the |
110 | | | docstring |
111 +------------------+-------------+-------------------------------+
112
113The :attr:`ml_meth` is a C function pointer. The functions may be of different
114types, but they always return :ctype:`PyObject\*`. If the function is not of
115the :ctype:`PyCFunction`, the compiler will require a cast in the method table.
116Even though :ctype:`PyCFunction` defines the first parameter as
117:ctype:`PyObject\*`, it is common that the method implementation uses a the
118specific C type of the *self* object.
119
120The :attr:`ml_flags` field is a bitfield which can include the following flags.
121The individual flags indicate either a calling convention or a binding
122convention. Of the calling convention flags, only :const:`METH_VARARGS` and
123:const:`METH_KEYWORDS` can be combined (but note that :const:`METH_KEYWORDS`
124alone is equivalent to ``METH_VARARGS | METH_KEYWORDS``). Any of the calling
125convention flags can be combined with a binding flag.
126
127
128.. data:: METH_VARARGS
129
130 This is the typical calling convention, where the methods have the type
Jeroen Ruigrok van der Wervenf4a9f962009-04-26 20:21:12 +0000131 :ctype:`PyCFunction`. The function expects two :ctype:`PyObject\*` values.
132 The first one is the *self* object for methods; for module functions, it
133 has the value given to :cfunc:`Py_InitModule4` (or *NULL* if
134 :cfunc:`Py_InitModule` was used). The second parameter (often called
135 *args*) is a tuple object representing all arguments. This parameter is
136 typically processed using :cfunc:`PyArg_ParseTuple` or
137 :cfunc:`PyArg_UnpackTuple`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000138
139
140.. data:: METH_KEYWORDS
141
Jeroen Ruigrok van der Wervenf4a9f962009-04-26 20:21:12 +0000142 Methods with these flags must be of type :ctype:`PyCFunctionWithKeywords`.
143 The function expects three parameters: *self*, *args*, and a dictionary of
144 all the keyword arguments. The flag is typically combined with
145 :const:`METH_VARARGS`, and the parameters are typically processed using
Georg Brandl54a3faa2008-01-20 09:30:57 +0000146 :cfunc:`PyArg_ParseTupleAndKeywords`.
147
148
149.. data:: METH_NOARGS
150
151 Methods without parameters don't need to check whether arguments are given if
152 they are listed with the :const:`METH_NOARGS` flag. They need to be of type
153 :ctype:`PyCFunction`. When used with object methods, the first parameter is
Jeroen Ruigrok van der Wervenf4a9f962009-04-26 20:21:12 +0000154 typically named ``self`` and will hold a reference to the object instance.
155 In all cases the second parameter will be *NULL*.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000156
157
158.. data:: METH_O
159
160 Methods with a single object argument can be listed with the :const:`METH_O`
161 flag, instead of invoking :cfunc:`PyArg_ParseTuple` with a ``"O"`` argument.
162 They have the type :ctype:`PyCFunction`, with the *self* parameter, and a
163 :ctype:`PyObject\*` parameter representing the single argument.
164
165
166These two constants are not used to indicate the calling convention but the
167binding when use with methods of classes. These may not be used for functions
168defined for modules. At most one of these flags may be set for any given
169method.
170
171
172.. data:: METH_CLASS
173
174 .. index:: builtin: classmethod
175
Jeroen Ruigrok van der Wervenf4a9f962009-04-26 20:21:12 +0000176 The method will be passed the type object as the first parameter rather
177 than an instance of the type. This is used to create *class methods*,
178 similar to what is created when using the :func:`classmethod` built-in
179 function.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000180
181
182.. data:: METH_STATIC
183
184 .. index:: builtin: staticmethod
185
Jeroen Ruigrok van der Wervenf4a9f962009-04-26 20:21:12 +0000186 The method will be passed *NULL* as the first parameter rather than an
187 instance of the type. This is used to create *static methods*, similar to
188 what is created when using the :func:`staticmethod` built-in function.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000189
190One other constant controls whether a method is loaded in place of another
191definition with the same method name.
192
193
194.. data:: METH_COEXIST
195
196 The method will be loaded in place of existing definitions. Without
197 *METH_COEXIST*, the default is to skip repeated definitions. Since slot
Jeroen Ruigrok van der Wervenf4a9f962009-04-26 20:21:12 +0000198 wrappers are loaded before the method table, the existence of a
199 *sq_contains* slot, for example, would generate a wrapped method named
200 :meth:`__contains__` and preclude the loading of a corresponding
201 PyCFunction with the same name. With the flag defined, the PyCFunction
202 will be loaded in place of the wrapper object and will co-exist with the
203 slot. This is helpful because calls to PyCFunctions are optimized more
204 than wrapper object calls.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000205
Georg Brandl1f01deb2009-01-03 22:47:39 +0000206
207.. ctype:: PyMemberDef
208
209 Structure which describes an attribute of a type which corresponds to a C
210 struct member. Its fields are:
211
212 +------------------+-------------+-------------------------------+
213 | Field | C Type | Meaning |
214 +==================+=============+===============================+
215 | :attr:`name` | char \* | name of the member |
216 +------------------+-------------+-------------------------------+
217 | :attr:`type` | int | the type of the member in the |
218 | | | C struct |
219 +------------------+-------------+-------------------------------+
220 | :attr:`offset` | Py_ssize_t | the offset in bytes that the |
221 | | | member is located on the |
222 | | | type's object struct |
223 +------------------+-------------+-------------------------------+
224 | :attr:`flags` | int | flag bits indicating if the |
225 | | | field should be read-only or |
226 | | | writable |
227 +------------------+-------------+-------------------------------+
228 | :attr:`doc` | char \* | points to the contents of the |
229 | | | docstring |
230 +------------------+-------------+-------------------------------+
231
232 :attr:`type` can be one of many ``T_`` macros corresponding to various C
233 types. When the member is accessed in Python, it will be converted to the
234 equivalent Python type.
235
236 =============== ==================
237 Macro name C type
238 =============== ==================
239 T_SHORT short
240 T_INT int
241 T_LONG long
242 T_FLOAT float
243 T_DOUBLE double
244 T_STRING char \*
245 T_OBJECT PyObject \*
246 T_OBJECT_EX PyObject \*
247 T_CHAR char
248 T_BYTE char
Benjamin Petersond23f8222009-04-05 19:13:16 +0000249 T_UBYTE unsigned char
Georg Brandl1f01deb2009-01-03 22:47:39 +0000250 T_UINT unsigned int
251 T_USHORT unsigned short
252 T_ULONG unsigned long
253 T_BOOL char
254 T_LONGLONG long long
255 T_ULONGLONG unsigned long long
256 T_PYSSIZET Py_ssize_t
257 =============== ==================
258
259 :cmacro:`T_OBJECT` and :cmacro:`T_OBJECT_EX` differ in that
260 :cmacro:`T_OBJECT` returns ``None`` if the member is *NULL* and
261 :cmacro:`T_OBJECT_EX` raises an :exc:`AttributeError`.
262
263 :attr:`flags` can be 0 for write and read access or :cmacro:`READONLY` for
264 read-only access. Using :cmacro:`T_STRING` for :attr:`type` implies
265 :cmacro:`READONLY`. Only :cmacro:`T_OBJECT` and :cmacro:`T_OBJECT_EX`
266 members can be deleted. (They are set to *NULL*).