blob: 16552700057d2a69a7ec3b4870bb53a083f0d1ee [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
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 Brandlf6842722008-01-19 22:08:21 +000017
18
19.. ctype:: PyObject
20
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
29.. ctype:: PyVarObject
30
Jeroen Ruigrok van der Wervenbc25bf92009-04-25 11:15:06 +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 Brandlf6842722008-01-19 22:08:21 +000035
36These macros are used in the definition of :ctype:`PyObject` and
37:ctype:`PyVarObject`:
38
39
40.. cmacro:: PyObject_HEAD
41
42 This is a macro which expands to the declarations of the fields of the
43 :ctype:`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
45 on the definition of :cmacro:`Py_TRACE_REFS`. By default, that macro is
46 not defined, and :cmacro:`PyObject_HEAD` expands to::
Georg Brandlf6842722008-01-19 22:08:21 +000047
48 Py_ssize_t ob_refcnt;
49 PyTypeObject *ob_type;
50
51 When :cmacro:`Py_TRACE_REFS` is defined, it expands to::
52
53 PyObject *_ob_next, *_ob_prev;
54 Py_ssize_t ob_refcnt;
55 PyTypeObject *ob_type;
56
57
58.. cmacro:: PyObject_VAR_HEAD
59
60 This is a macro which expands to the declarations of the fields of the
Jeroen Ruigrok van der Wervenbc25bf92009-04-25 11:15:06 +000061 :ctype:`PyVarObject` type; it is used when declaring new types which
62 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
68 Note that :cmacro:`PyObject_HEAD` is part of the expansion, and that its own
69 expansion varies depending on the definition of :cmacro:`Py_TRACE_REFS`.
70
71PyObject_HEAD_INIT
72
73
74.. ctype:: PyCFunction
75
Jeroen Ruigrok van der Wervenbc25bf92009-04-25 11:15:06 +000076 Type of the functions used to implement most Python callables in C.
77 Functions of this type take two :ctype:`PyObject\*` parameters and return
78 one such value. If the return value is *NULL*, an exception shall have
79 been set. If not *NULL*, the return value is interpreted as the return
80 value of the function as exposed in Python. The function must return a new
81 reference.
Georg Brandlf6842722008-01-19 22:08:21 +000082
83
84.. ctype:: PyMethodDef
85
86 Structure used to describe a method of an extension type. This structure has
87 four fields:
88
89 +------------------+-------------+-------------------------------+
90 | Field | C Type | Meaning |
91 +==================+=============+===============================+
92 | :attr:`ml_name` | char \* | name of the method |
93 +------------------+-------------+-------------------------------+
94 | :attr:`ml_meth` | PyCFunction | pointer to the C |
95 | | | implementation |
96 +------------------+-------------+-------------------------------+
97 | :attr:`ml_flags` | int | flag bits indicating how the |
98 | | | call should be constructed |
99 +------------------+-------------+-------------------------------+
100 | :attr:`ml_doc` | char \* | points to the contents of the |
101 | | | docstring |
102 +------------------+-------------+-------------------------------+
103
104The :attr:`ml_meth` is a C function pointer. The functions may be of different
105types, but they always return :ctype:`PyObject\*`. If the function is not of
106the :ctype:`PyCFunction`, the compiler will require a cast in the method table.
107Even though :ctype:`PyCFunction` defines the first parameter as
108:ctype:`PyObject\*`, it is common that the method implementation uses a the
109specific C type of the *self* object.
110
111The :attr:`ml_flags` field is a bitfield which can include the following flags.
112The individual flags indicate either a calling convention or a binding
113convention. Of the calling convention flags, only :const:`METH_VARARGS` and
114:const:`METH_KEYWORDS` can be combined (but note that :const:`METH_KEYWORDS`
115alone is equivalent to ``METH_VARARGS | METH_KEYWORDS``). Any of the calling
116convention flags can be combined with a binding flag.
117
118
119.. data:: METH_VARARGS
120
121 This is the typical calling convention, where the methods have the type
Jeroen Ruigrok van der Wervenbc25bf92009-04-25 11:15:06 +0000122 :ctype:`PyCFunction`. The function expects two :ctype:`PyObject\*` values.
123 The first one is the *self* object for methods; for module functions, it
124 has the value given to :cfunc:`Py_InitModule4` (or *NULL* if
125 :cfunc:`Py_InitModule` was used). The second parameter (often called
126 *args*) is a tuple object representing all arguments. This parameter is
127 typically processed using :cfunc:`PyArg_ParseTuple` or
128 :cfunc:`PyArg_UnpackTuple`.
Georg Brandlf6842722008-01-19 22:08:21 +0000129
130
131.. data:: METH_KEYWORDS
132
Jeroen Ruigrok van der Wervenbc25bf92009-04-25 11:15:06 +0000133 Methods with these flags must be of type :ctype:`PyCFunctionWithKeywords`.
134 The function expects three parameters: *self*, *args*, and a dictionary of
135 all the keyword arguments. The flag is typically combined with
136 :const:`METH_VARARGS`, and the parameters are typically processed using
Georg Brandlf6842722008-01-19 22:08:21 +0000137 :cfunc:`PyArg_ParseTupleAndKeywords`.
138
139
140.. data:: METH_NOARGS
141
142 Methods without parameters don't need to check whether arguments are given if
143 they are listed with the :const:`METH_NOARGS` flag. They need to be of type
144 :ctype:`PyCFunction`. When used with object methods, the first parameter is
Jeroen Ruigrok van der Wervenbc25bf92009-04-25 11:15:06 +0000145 typically named ``self`` and will hold a reference to the object instance.
146 In all cases the second parameter will be *NULL*.
Georg Brandlf6842722008-01-19 22:08:21 +0000147
148
149.. data:: METH_O
150
151 Methods with a single object argument can be listed with the :const:`METH_O`
152 flag, instead of invoking :cfunc:`PyArg_ParseTuple` with a ``"O"`` argument.
153 They have the type :ctype:`PyCFunction`, with the *self* parameter, and a
154 :ctype:`PyObject\*` parameter representing the single argument.
155
156
157.. data:: METH_OLDARGS
158
159 This calling convention is deprecated. The method must be of type
Jeroen Ruigrok van der Wervenbc25bf92009-04-25 11:15:06 +0000160 :ctype:`PyCFunction`. The second argument is *NULL* if no arguments are
161 given, a single object if exactly one argument is given, and a tuple of
162 objects if more than one argument is given. There is no way for a function
163 using this convention to distinguish between a call with multiple arguments
164 and a call with a tuple as the only argument.
Georg Brandlf6842722008-01-19 22:08:21 +0000165
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 Wervenbc25bf92009-04-25 11:15:06 +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 Brandlf6842722008-01-19 22:08:21 +0000180
181 .. versionadded:: 2.3
182
183
184.. data:: METH_STATIC
185
186 .. index:: builtin: staticmethod
187
Jeroen Ruigrok van der Wervenbc25bf92009-04-25 11:15:06 +0000188 The method will be passed *NULL* as the first parameter rather than an
189 instance of the type. This is used to create *static methods*, similar to
190 what is created when using the :func:`staticmethod` built-in function.
Georg Brandlf6842722008-01-19 22:08:21 +0000191
192 .. versionadded:: 2.3
193
194One other constant controls whether a method is loaded in place of another
195definition with the same method name.
196
197
198.. data:: METH_COEXIST
199
200 The method will be loaded in place of existing definitions. Without
201 *METH_COEXIST*, the default is to skip repeated definitions. Since slot
Jeroen Ruigrok van der Wervenbc25bf92009-04-25 11:15:06 +0000202 wrappers are loaded before the method table, the existence of a
203 *sq_contains* slot, for example, would generate a wrapped method named
204 :meth:`__contains__` and preclude the loading of a corresponding
205 PyCFunction with the same name. With the flag defined, the PyCFunction
206 will be loaded in place of the wrapper object and will co-exist with the
207 slot. This is helpful because calls to PyCFunctions are optimized more
208 than wrapper object calls.
Georg Brandlf6842722008-01-19 22:08:21 +0000209
210 .. versionadded:: 2.4
211
212
Benjamin Peterson0132ee342009-01-02 18:26:23 +0000213.. ctype:: PyMemberDef
214
215 Structure which describes an attribute of a type which corresponds to a C
Georg Brandlc63785d2009-01-03 21:52:16 +0000216 struct member. Its fields are:
Benjamin Peterson0132ee342009-01-02 18:26:23 +0000217
218 +------------------+-------------+-------------------------------+
219 | Field | C Type | Meaning |
220 +==================+=============+===============================+
221 | :attr:`name` | char \* | name of the member |
222 +------------------+-------------+-------------------------------+
223 | :attr:`type` | int | the type of the member in the |
224 | | | C struct |
225 +------------------+-------------+-------------------------------+
226 | :attr:`offset` | Py_ssize_t | the offset in bytes that the |
227 | | | member is located on the |
228 | | | type's object struct |
229 +------------------+-------------+-------------------------------+
230 | :attr:`flags` | int | flag bits indicating if the |
231 | | | field should be read-only or |
232 | | | writable |
233 +------------------+-------------+-------------------------------+
234 | :attr:`doc` | char \* | points to the contents of the |
235 | | | docstring |
236 +------------------+-------------+-------------------------------+
237
238 :attr:`type` can be one of many ``T_`` macros corresponding to various C
239 types. When the member is accessed in Python, it will be converted to the
240 equivalent Python type.
241
242 =============== ==================
243 Macro name C type
244 =============== ==================
245 T_SHORT short
246 T_INT int
247 T_LONG long
248 T_FLOAT float
249 T_DOUBLE double
250 T_STRING char \*
251 T_OBJECT PyObject \*
252 T_OBJECT_EX PyObject \*
253 T_CHAR char
254 T_BYTE char
Georg Brandlbdaa6a72009-03-31 19:30:56 +0000255 T_UBYTE unsigned char
Benjamin Peterson0132ee342009-01-02 18:26:23 +0000256 T_UINT unsigned int
257 T_USHORT unsigned short
258 T_ULONG unsigned long
259 T_BOOL char
260 T_LONGLONG long long
261 T_ULONGLONG unsigned long long
262 T_PYSSIZET Py_ssize_t
263 =============== ==================
264
265 :cmacro:`T_OBJECT` and :cmacro:`T_OBJECT_EX` differ in that
266 :cmacro:`T_OBJECT` returns ``None`` if the member is *NULL* and
267 :cmacro:`T_OBJECT_EX` raises an :exc:`AttributeError`.
268
269 :attr:`flags` can be 0 for write and read access or :cmacro:`READONLY` for
270 read-only access. Using :cmacro:`T_STRING` for :attr:`type` implies
Georg Brandlc63785d2009-01-03 21:52:16 +0000271 :cmacro:`READONLY`. Only :cmacro:`T_OBJECT` and :cmacro:`T_OBJECT_EX`
272 members can be deleted. (They are set to *NULL*).
Benjamin Peterson0132ee342009-01-02 18:26:23 +0000273
274
Georg Brandlf6842722008-01-19 22:08:21 +0000275.. cfunction:: PyObject* Py_FindMethod(PyMethodDef table[], PyObject *ob, char *name)
276
Jeroen Ruigrok van der Wervenbc25bf92009-04-25 11:15:06 +0000277 Return a bound method object for an extension type implemented in C. This
278 can be useful in the implementation of a :attr:`tp_getattro` or
279 :attr:`tp_getattr` handler that does not use the
280 :cfunc:`PyObject_GenericGetAttr` function.