~dkuhlman/python-training-materials/Materials

« back to all changes in this revision

Viewing changes to python-3.5.1-docs-html/_sources/c-api/structures.txt

  • Committer: Dave Kuhlman
  • Date: 2017-04-15 16:24:56 UTC
  • Revision ID: dkuhlman@davekuhlman.org-20170415162456-iav9vozzg4iwqwv3
Updated docs

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
.. highlightlang:: c
2
 
 
3
 
.. _common-structs:
4
 
 
5
 
Common Object Structures
6
 
========================
7
 
 
8
 
There are a large number of structures which are used in the definition of
9
 
object types for Python.  This section describes these structures and how they
10
 
are used.
11
 
 
12
 
All Python objects ultimately share a small number of fields at the beginning
13
 
of the object's representation in memory.  These are represented by the
14
 
:c:type:`PyObject` and :c:type:`PyVarObject` types, which are defined, in turn,
15
 
by the expansions of some macros also used, whether directly or indirectly, in
16
 
the definition of all other Python objects.
17
 
 
18
 
 
19
 
.. c:type:: PyObject
20
 
 
21
 
   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.
25
 
   Nothing is actually declared to be a :c:type:`PyObject`, but every pointer
26
 
   to a Python object can be cast to a :c:type:`PyObject*`.  Access to the
27
 
   members must be done by using the macros :c:macro:`Py_REFCNT` and
28
 
   :c:macro:`Py_TYPE`.
29
 
 
30
 
 
31
 
.. c:type:: PyVarObject
32
 
 
33
 
   This is an extension of :c:type:`PyObject` that adds the :attr:`ob_size`
34
 
   field.  This is only used for objects that have some notion of *length*.
35
 
   This type does not often appear in the Python/C API.
36
 
   Access to the members must be done by using the macros
37
 
   :c:macro:`Py_REFCNT`, :c:macro:`Py_TYPE`, and :c:macro:`Py_SIZE`.
38
 
 
39
 
 
40
 
.. c:macro:: PyObject_HEAD
41
 
 
42
 
   This is a macro used when declaring new types which represent objects
43
 
   without a varying length.  The PyObject_HEAD macro expands to::
44
 
 
45
 
      PyObject ob_base;
46
 
 
47
 
   See documentation of :c:type:`PyObject` above.
48
 
 
49
 
 
50
 
.. c:macro:: PyObject_VAR_HEAD
51
 
 
52
 
   This is a macro used when declaring new types which represent objects
53
 
   with a length that varies from instance to instance.
54
 
   The PyObject_VAR_HEAD macro expands to::
55
 
 
56
 
      PyVarObject ob_base;
57
 
 
58
 
   See documentation of :c:type:`PyVarObject` above.
59
 
 
60
 
 
61
 
.. c:macro:: Py_TYPE(o)
62
 
 
63
 
   This macro is used to access the :attr:`ob_type` member of a Python object.
64
 
   It expands to::
65
 
 
66
 
      (((PyObject*)(o))->ob_type)
67
 
 
68
 
 
69
 
.. c:macro:: Py_REFCNT(o)
70
 
 
71
 
   This macro is used to access the :attr:`ob_refcnt` member of a Python
72
 
   object.
73
 
   It expands to::
74
 
 
75
 
      (((PyObject*)(o))->ob_refcnt)
76
 
 
77
 
 
78
 
.. c:macro:: Py_SIZE(o)
79
 
 
80
 
   This macro is used to access the :attr:`ob_size` member of a Python object.
81
 
   It expands to::
82
 
 
83
 
      (((PyVarObject*)(o))->ob_size)
84
 
 
85
 
 
86
 
.. c:macro:: PyObject_HEAD_INIT(type)
87
 
 
88
 
   This is a macro which expands to initialization values for a new
89
 
   :c:type:`PyObject` type.  This macro expands to::
90
 
 
91
 
      _PyObject_EXTRA_INIT
92
 
      1, type,
93
 
 
94
 
 
95
 
.. c:macro:: PyVarObject_HEAD_INIT(type, size)
96
 
 
97
 
   This is a macro which expands to initialization values for a new
98
 
   :c:type:`PyVarObject` type, including the :attr:`ob_size` field.
99
 
   This macro expands to::
100
 
 
101
 
      _PyObject_EXTRA_INIT
102
 
      1, type, size,
103
 
 
104
 
 
105
 
.. c:type:: PyCFunction
106
 
 
107
 
   Type of the functions used to implement most Python callables in C.
108
 
   Functions of this type take two :c:type:`PyObject\*` parameters and return
109
 
   one such value.  If the return value is *NULL*, an exception shall have
110
 
   been set.  If not *NULL*, the return value is interpreted as the return
111
 
   value of the function as exposed in Python.  The function must return a new
112
 
   reference.
113
 
 
114
 
 
115
 
.. c:type:: PyCFunctionWithKeywords
116
 
 
117
 
   Type of the functions used to implement Python callables in C that take
118
 
   keyword arguments: they take three :c:type:`PyObject\*` parameters and return
119
 
   one such value.  See :c:type:`PyCFunction` above for the meaning of the return
120
 
   value.
121
 
 
122
 
 
123
 
.. c:type:: PyMethodDef
124
 
 
125
 
   Structure used to describe a method of an extension type.  This structure has
126
 
   four fields:
127
 
 
128
 
   +------------------+-------------+-------------------------------+
129
 
   | Field            | C Type      | Meaning                       |
130
 
   +==================+=============+===============================+
131
 
   | :attr:`ml_name`  | char \*     | name of the method            |
132
 
   +------------------+-------------+-------------------------------+
133
 
   | :attr:`ml_meth`  | PyCFunction | pointer to the C              |
134
 
   |                  |             | implementation                |
135
 
   +------------------+-------------+-------------------------------+
136
 
   | :attr:`ml_flags` | int         | flag bits indicating how the  |
137
 
   |                  |             | call should be constructed    |
138
 
   +------------------+-------------+-------------------------------+
139
 
   | :attr:`ml_doc`   | char \*     | points to the contents of the |
140
 
   |                  |             | docstring                     |
141
 
   +------------------+-------------+-------------------------------+
142
 
 
143
 
The :attr:`ml_meth` is a C function pointer.  The functions may be of different
144
 
types, but they always return :c:type:`PyObject\*`.  If the function is not of
145
 
the :c:type:`PyCFunction`, the compiler will require a cast in the method table.
146
 
Even though :c:type:`PyCFunction` defines the first parameter as
147
 
:c:type:`PyObject\*`, it is common that the method implementation uses the
148
 
specific C type of the *self* object.
149
 
 
150
 
The :attr:`ml_flags` field is a bitfield which can include the following flags.
151
 
The individual flags indicate either a calling convention or a binding
152
 
convention.  Of the calling convention flags, only :const:`METH_VARARGS` and
153
 
:const:`METH_KEYWORDS` can be combined (but note that :const:`METH_KEYWORDS`
154
 
alone is equivalent to ``METH_VARARGS | METH_KEYWORDS``). Any of the calling
155
 
convention flags can be combined with a binding flag.
156
 
 
157
 
 
158
 
.. data:: METH_VARARGS
159
 
 
160
 
   This is the typical calling convention, where the methods have the type
161
 
   :c:type:`PyCFunction`. The function expects two :c:type:`PyObject\*` values.
162
 
   The first one is the *self* object for methods; for module functions, it is
163
 
   the module object.  The second parameter (often called *args*) is a tuple
164
 
   object representing all arguments. This parameter is typically processed
165
 
   using :c:func:`PyArg_ParseTuple` or :c:func:`PyArg_UnpackTuple`.
166
 
 
167
 
 
168
 
.. data:: METH_KEYWORDS
169
 
 
170
 
   Methods with these flags must be of type :c:type:`PyCFunctionWithKeywords`.
171
 
   The function expects three parameters: *self*, *args*, and a dictionary of
172
 
   all the keyword arguments.  The flag is typically combined with
173
 
   :const:`METH_VARARGS`, and the parameters are typically processed using
174
 
   :c:func:`PyArg_ParseTupleAndKeywords`.
175
 
 
176
 
 
177
 
.. data:: METH_NOARGS
178
 
 
179
 
   Methods without parameters don't need to check whether arguments are given if
180
 
   they are listed with the :const:`METH_NOARGS` flag.  They need to be of type
181
 
   :c:type:`PyCFunction`.  The first parameter is typically named *self* and will
182
 
   hold a reference to the module or object instance.  In all cases the second
183
 
   parameter will be *NULL*.
184
 
 
185
 
 
186
 
.. data:: METH_O
187
 
 
188
 
   Methods with a single object argument can be listed with the :const:`METH_O`
189
 
   flag, instead of invoking :c:func:`PyArg_ParseTuple` with a ``"O"`` argument.
190
 
   They have the type :c:type:`PyCFunction`, with the *self* parameter, and a
191
 
   :c:type:`PyObject\*` parameter representing the single argument.
192
 
 
193
 
 
194
 
These two constants are not used to indicate the calling convention but the
195
 
binding when use with methods of classes.  These may not be used for functions
196
 
defined for modules.  At most one of these flags may be set for any given
197
 
method.
198
 
 
199
 
 
200
 
.. data:: METH_CLASS
201
 
 
202
 
   .. index:: builtin: classmethod
203
 
 
204
 
   The method will be passed the type object as the first parameter rather
205
 
   than an instance of the type.  This is used to create *class methods*,
206
 
   similar to what is created when using the :func:`classmethod` built-in
207
 
   function.
208
 
 
209
 
 
210
 
.. data:: METH_STATIC
211
 
 
212
 
   .. index:: builtin: staticmethod
213
 
 
214
 
   The method will be passed *NULL* as the first parameter rather than an
215
 
   instance of the type.  This is used to create *static methods*, similar to
216
 
   what is created when using the :func:`staticmethod` built-in function.
217
 
 
218
 
One other constant controls whether a method is loaded in place of another
219
 
definition with the same method name.
220
 
 
221
 
 
222
 
.. data:: METH_COEXIST
223
 
 
224
 
   The method will be loaded in place of existing definitions.  Without
225
 
   *METH_COEXIST*, the default is to skip repeated definitions.  Since slot
226
 
   wrappers are loaded before the method table, the existence of a
227
 
   *sq_contains* slot, for example, would generate a wrapped method named
228
 
   :meth:`__contains__` and preclude the loading of a corresponding
229
 
   PyCFunction with the same name.  With the flag defined, the PyCFunction
230
 
   will be loaded in place of the wrapper object and will co-exist with the
231
 
   slot.  This is helpful because calls to PyCFunctions are optimized more
232
 
   than wrapper object calls.
233
 
 
234
 
 
235
 
.. c:type:: PyMemberDef
236
 
 
237
 
   Structure which describes an attribute of a type which corresponds to a C
238
 
   struct member.  Its fields are:
239
 
 
240
 
   +------------------+-------------+-------------------------------+
241
 
   | Field            | C Type      | Meaning                       |
242
 
   +==================+=============+===============================+
243
 
   | :attr:`name`     | char \*     | name of the member            |
244
 
   +------------------+-------------+-------------------------------+
245
 
   | :attr:`type`     | int         | the type of the member in the |
246
 
   |                  |             | C struct                      |
247
 
   +------------------+-------------+-------------------------------+
248
 
   | :attr:`offset`   | Py_ssize_t  | the offset in bytes that the  |
249
 
   |                  |             | member is located on the      |
250
 
   |                  |             | type's object struct          |
251
 
   +------------------+-------------+-------------------------------+
252
 
   | :attr:`flags`    | int         | flag bits indicating if the   |
253
 
   |                  |             | field should be read-only or  |
254
 
   |                  |             | writable                      |
255
 
   +------------------+-------------+-------------------------------+
256
 
   | :attr:`doc`      | char \*     | points to the contents of the |
257
 
   |                  |             | docstring                     |
258
 
   +------------------+-------------+-------------------------------+
259
 
 
260
 
   :attr:`type` can be one of many ``T_`` macros corresponding to various C
261
 
   types.  When the member is accessed in Python, it will be converted to the
262
 
   equivalent Python type.
263
 
 
264
 
   =============== ==================
265
 
   Macro name      C type
266
 
   =============== ==================
267
 
   T_SHORT         short
268
 
   T_INT           int
269
 
   T_LONG          long
270
 
   T_FLOAT         float
271
 
   T_DOUBLE        double
272
 
   T_STRING        char \*
273
 
   T_OBJECT        PyObject \*
274
 
   T_OBJECT_EX     PyObject \*
275
 
   T_CHAR          char
276
 
   T_BYTE          char
277
 
   T_UBYTE         unsigned char
278
 
   T_UINT          unsigned int
279
 
   T_USHORT        unsigned short
280
 
   T_ULONG         unsigned long
281
 
   T_BOOL          char
282
 
   T_LONGLONG      long long
283
 
   T_ULONGLONG     unsigned long long
284
 
   T_PYSSIZET      Py_ssize_t
285
 
   =============== ==================
286
 
 
287
 
   :c:macro:`T_OBJECT` and :c:macro:`T_OBJECT_EX` differ in that
288
 
   :c:macro:`T_OBJECT` returns ``None`` if the member is *NULL* and
289
 
   :c:macro:`T_OBJECT_EX` raises an :exc:`AttributeError`.  Try to use
290
 
   :c:macro:`T_OBJECT_EX` over :c:macro:`T_OBJECT` because :c:macro:`T_OBJECT_EX`
291
 
   handles use of the :keyword:`del` statement on that attribute more correctly
292
 
   than :c:macro:`T_OBJECT`.
293
 
 
294
 
   :attr:`flags` can be 0 for write and read access or :c:macro:`READONLY` for
295
 
   read-only access.  Using :c:macro:`T_STRING` for :attr:`type` implies
296
 
   :c:macro:`READONLY`.  Only :c:macro:`T_OBJECT` and :c:macro:`T_OBJECT_EX`
297
 
   members can be deleted.  (They are set to *NULL*).