]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/python/contrib/Doc/c-api/object.rst
Inital import
[l4.git] / l4 / pkg / python / contrib / Doc / c-api / object.rst
1 .. highlightlang:: c
2
3 .. _object:
4
5 Object Protocol
6 ===============
7
8
9 .. cfunction:: int PyObject_Print(PyObject *o, FILE *fp, int flags)
10
11    Print an object *o*, on file *fp*.  Returns ``-1`` on error.  The flags argument
12    is used to enable certain printing options.  The only option currently supported
13    is :const:`Py_PRINT_RAW`; if given, the :func:`str` of the object is written
14    instead of the :func:`repr`.
15
16
17 .. cfunction:: int PyObject_HasAttr(PyObject *o, PyObject *attr_name)
18
19    Returns ``1`` if *o* has the attribute *attr_name*, and ``0`` otherwise.  This
20    is equivalent to the Python expression ``hasattr(o, attr_name)``.  This function
21    always succeeds.
22
23
24 .. cfunction:: int PyObject_HasAttrString(PyObject *o, const char *attr_name)
25
26    Returns ``1`` if *o* has the attribute *attr_name*, and ``0`` otherwise.  This
27    is equivalent to the Python expression ``hasattr(o, attr_name)``.  This function
28    always succeeds.
29
30
31 .. cfunction:: PyObject* PyObject_GetAttr(PyObject *o, PyObject *attr_name)
32
33    Retrieve an attribute named *attr_name* from object *o*. Returns the attribute
34    value on success, or *NULL* on failure.  This is the equivalent of the Python
35    expression ``o.attr_name``.
36
37
38 .. cfunction:: PyObject* PyObject_GetAttrString(PyObject *o, const char *attr_name)
39
40    Retrieve an attribute named *attr_name* from object *o*. Returns the attribute
41    value on success, or *NULL* on failure. This is the equivalent of the Python
42    expression ``o.attr_name``.
43
44
45 .. cfunction:: PyObject* PyObject_GenericGetAttr(PyObject *o, PyObject *name)
46
47    Generic attribute getter function that is meant to be put into a type
48    object's ``tp_getattro`` slot.  It looks for a descriptor in the dictionary
49    of classes in the object's MRO as well as an attribute in the object's
50    :attr:`__dict__` (if present).  As outlined in :ref:`descriptors`, data
51    descriptors take preference over instance attributes, while non-data
52    descriptors don't.  Otherwise, an :exc:`AttributeError` is raised.
53
54
55 .. cfunction:: int PyObject_SetAttr(PyObject *o, PyObject *attr_name, PyObject *v)
56
57    Set the value of the attribute named *attr_name*, for object *o*, to the value
58    *v*. Returns ``-1`` on failure.  This is the equivalent of the Python statement
59    ``o.attr_name = v``.
60
61
62 .. cfunction:: int PyObject_SetAttrString(PyObject *o, const char *attr_name, PyObject *v)
63
64    Set the value of the attribute named *attr_name*, for object *o*, to the value
65    *v*. Returns ``-1`` on failure.  This is the equivalent of the Python statement
66    ``o.attr_name = v``.
67
68
69 .. cfunction:: int PyObject_GenericSetAttr(PyObject *o, PyObject *name, PyObject *value)
70
71    Generic attribute setter function that is meant to be put into a type
72    object's ``tp_setattro`` slot.  It looks for a data descriptor in the
73    dictionary of classes in the object's MRO, and if found it takes preference
74    over setting the attribute in the instance dictionary. Otherwise, the
75    attribute is set in the object's :attr:`__dict__` (if present).  Otherwise,
76    an :exc:`AttributeError` is raised and ``-1`` is returned.
77
78
79 .. cfunction:: int PyObject_DelAttr(PyObject *o, PyObject *attr_name)
80
81    Delete attribute named *attr_name*, for object *o*. Returns ``-1`` on failure.
82    This is the equivalent of the Python statement ``del o.attr_name``.
83
84
85 .. cfunction:: int PyObject_DelAttrString(PyObject *o, const char *attr_name)
86
87    Delete attribute named *attr_name*, for object *o*. Returns ``-1`` on failure.
88    This is the equivalent of the Python statement ``del o.attr_name``.
89
90
91 .. cfunction:: PyObject* PyObject_RichCompare(PyObject *o1, PyObject *o2, int opid)
92
93    Compare the values of *o1* and *o2* using the operation specified by *opid*,
94    which must be one of :const:`Py_LT`, :const:`Py_LE`, :const:`Py_EQ`,
95    :const:`Py_NE`, :const:`Py_GT`, or :const:`Py_GE`, corresponding to ``<``,
96    ``<=``, ``==``, ``!=``, ``>``, or ``>=`` respectively. This is the equivalent of
97    the Python expression ``o1 op o2``, where ``op`` is the operator corresponding
98    to *opid*. Returns the value of the comparison on success, or *NULL* on failure.
99
100
101 .. cfunction:: int PyObject_RichCompareBool(PyObject *o1, PyObject *o2, int opid)
102
103    Compare the values of *o1* and *o2* using the operation specified by *opid*,
104    which must be one of :const:`Py_LT`, :const:`Py_LE`, :const:`Py_EQ`,
105    :const:`Py_NE`, :const:`Py_GT`, or :const:`Py_GE`, corresponding to ``<``,
106    ``<=``, ``==``, ``!=``, ``>``, or ``>=`` respectively. Returns ``-1`` on error,
107    ``0`` if the result is false, ``1`` otherwise. This is the equivalent of the
108    Python expression ``o1 op o2``, where ``op`` is the operator corresponding to
109    *opid*.
110
111
112 .. cfunction:: int PyObject_Cmp(PyObject *o1, PyObject *o2, int *result)
113
114    .. index:: builtin: cmp
115
116    Compare the values of *o1* and *o2* using a routine provided by *o1*, if one
117    exists, otherwise with a routine provided by *o2*.  The result of the comparison
118    is returned in *result*.  Returns ``-1`` on failure.  This is the equivalent of
119    the Python statement ``result = cmp(o1, o2)``.
120
121
122 .. cfunction:: int PyObject_Compare(PyObject *o1, PyObject *o2)
123
124    .. index:: builtin: cmp
125
126    Compare the values of *o1* and *o2* using a routine provided by *o1*, if one
127    exists, otherwise with a routine provided by *o2*.  Returns the result of the
128    comparison on success.  On error, the value returned is undefined; use
129    :cfunc:`PyErr_Occurred` to detect an error.  This is equivalent to the Python
130    expression ``cmp(o1, o2)``.
131
132
133 .. cfunction:: PyObject* PyObject_Repr(PyObject *o)
134
135    .. index:: builtin: repr
136
137    Compute a string representation of object *o*.  Returns the string
138    representation on success, *NULL* on failure.  This is the equivalent of the
139    Python expression ``repr(o)``.  Called by the :func:`repr` built-in function and
140    by reverse quotes.
141
142
143 .. cfunction:: PyObject* PyObject_Str(PyObject *o)
144
145    .. index:: builtin: str
146
147    Compute a string representation of object *o*.  Returns the string
148    representation on success, *NULL* on failure.  This is the equivalent of the
149    Python expression ``str(o)``.  Called by the :func:`str` built-in function and
150    by the :keyword:`print` statement.
151
152
153 .. cfunction:: PyObject* PyObject_Bytes(PyObject *o)
154
155    .. index:: builtin: bytes
156
157    Compute a bytes representation of object *o*.  In 2.x, this is just a alias
158    for :cfunc:`PyObject_Str`.
159
160
161 .. cfunction:: PyObject* PyObject_Unicode(PyObject *o)
162
163    .. index:: builtin: unicode
164
165    Compute a Unicode string representation of object *o*.  Returns the Unicode
166    string representation on success, *NULL* on failure. This is the equivalent of
167    the Python expression ``unicode(o)``.  Called by the :func:`unicode` built-in
168    function.
169
170
171 .. cfunction:: int PyObject_IsInstance(PyObject *inst, PyObject *cls)
172
173    Returns ``1`` if *inst* is an instance of the class *cls* or a subclass of
174    *cls*, or ``0`` if not.  On error, returns ``-1`` and sets an exception.  If
175    *cls* is a type object rather than a class object, :cfunc:`PyObject_IsInstance`
176    returns ``1`` if *inst* is of type *cls*.  If *cls* is a tuple, the check will
177    be done against every entry in *cls*. The result will be ``1`` when at least one
178    of the checks returns ``1``, otherwise it will be ``0``. If *inst* is not a
179    class instance and *cls* is neither a type object, nor a class object, nor a
180    tuple, *inst* must have a :attr:`__class__` attribute --- the class relationship
181    of the value of that attribute with *cls* will be used to determine the result
182    of this function.
183
184    .. versionadded:: 2.1
185
186    .. versionchanged:: 2.2
187       Support for a tuple as the second argument added.
188
189 Subclass determination is done in a fairly straightforward way, but includes a
190 wrinkle that implementors of extensions to the class system may want to be aware
191 of.  If :class:`A` and :class:`B` are class objects, :class:`B` is a subclass of
192 :class:`A` if it inherits from :class:`A` either directly or indirectly.  If
193 either is not a class object, a more general mechanism is used to determine the
194 class relationship of the two objects.  When testing if *B* is a subclass of
195 *A*, if *A* is *B*, :cfunc:`PyObject_IsSubclass` returns true.  If *A* and *B*
196 are different objects, *B*'s :attr:`__bases__` attribute is searched in a
197 depth-first fashion for *A* --- the presence of the :attr:`__bases__` attribute
198 is considered sufficient for this determination.
199
200
201 .. cfunction:: int PyObject_IsSubclass(PyObject *derived, PyObject *cls)
202
203    Returns ``1`` if the class *derived* is identical to or derived from the class
204    *cls*, otherwise returns ``0``.  In case of an error, returns ``-1``. If *cls*
205    is a tuple, the check will be done against every entry in *cls*. The result will
206    be ``1`` when at least one of the checks returns ``1``, otherwise it will be
207    ``0``. If either *derived* or *cls* is not an actual class object (or tuple),
208    this function uses the generic algorithm described above.
209
210    .. versionadded:: 2.1
211
212    .. versionchanged:: 2.3
213       Older versions of Python did not support a tuple as the second argument.
214
215
216 .. cfunction:: int PyCallable_Check(PyObject *o)
217
218    Determine if the object *o* is callable.  Return ``1`` if the object is callable
219    and ``0`` otherwise.  This function always succeeds.
220
221
222 .. cfunction:: PyObject* PyObject_Call(PyObject *callable_object, PyObject *args, PyObject *kw)
223
224    .. index:: builtin: apply
225
226    Call a callable Python object *callable_object*, with arguments given by the
227    tuple *args*, and named arguments given by the dictionary *kw*. If no named
228    arguments are needed, *kw* may be *NULL*. *args* must not be *NULL*, use an
229    empty tuple if no arguments are needed. Returns the result of the call on
230    success, or *NULL* on failure.  This is the equivalent of the Python expression
231    ``apply(callable_object, args, kw)`` or ``callable_object(*args, **kw)``.
232
233    .. versionadded:: 2.2
234
235
236 .. cfunction:: PyObject* PyObject_CallObject(PyObject *callable_object, PyObject *args)
237
238    .. index:: builtin: apply
239
240    Call a callable Python object *callable_object*, with arguments given by the
241    tuple *args*.  If no arguments are needed, then *args* may be *NULL*.  Returns
242    the result of the call on success, or *NULL* on failure.  This is the equivalent
243    of the Python expression ``apply(callable_object, args)`` or
244    ``callable_object(*args)``.
245
246
247 .. cfunction:: PyObject* PyObject_CallFunction(PyObject *callable, char *format, ...)
248
249    .. index:: builtin: apply
250
251    Call a callable Python object *callable*, with a variable number of C arguments.
252    The C arguments are described using a :cfunc:`Py_BuildValue` style format
253    string.  The format may be *NULL*, indicating that no arguments are provided.
254    Returns the result of the call on success, or *NULL* on failure.  This is the
255    equivalent of the Python expression ``apply(callable, args)`` or
256    ``callable(*args)``. Note that if you only pass :ctype:`PyObject \*` args,
257    :cfunc:`PyObject_CallFunctionObjArgs` is a faster alternative.
258
259
260 .. cfunction:: PyObject* PyObject_CallMethod(PyObject *o, char *method, char *format, ...)
261
262    Call the method named *method* of object *o* with a variable number of C
263    arguments.  The C arguments are described by a :cfunc:`Py_BuildValue` format
264    string that should  produce a tuple.  The format may be *NULL*, indicating that
265    no arguments are provided. Returns the result of the call on success, or *NULL*
266    on failure.  This is the equivalent of the Python expression ``o.method(args)``.
267    Note that if you only pass :ctype:`PyObject \*` args,
268    :cfunc:`PyObject_CallMethodObjArgs` is a faster alternative.
269
270
271 .. cfunction:: PyObject* PyObject_CallFunctionObjArgs(PyObject *callable, ..., NULL)
272
273    Call a callable Python object *callable*, with a variable number of
274    :ctype:`PyObject\*` arguments.  The arguments are provided as a variable number
275    of parameters followed by *NULL*. Returns the result of the call on success, or
276    *NULL* on failure.
277
278    .. versionadded:: 2.2
279
280
281 .. cfunction:: PyObject* PyObject_CallMethodObjArgs(PyObject *o, PyObject *name, ..., NULL)
282
283    Calls a method of the object *o*, where the name of the method is given as a
284    Python string object in *name*.  It is called with a variable number of
285    :ctype:`PyObject\*` arguments.  The arguments are provided as a variable number
286    of parameters followed by *NULL*. Returns the result of the call on success, or
287    *NULL* on failure.
288
289    .. versionadded:: 2.2
290
291
292 .. cfunction:: long PyObject_Hash(PyObject *o)
293
294    .. index:: builtin: hash
295
296    Compute and return the hash value of an object *o*.  On failure, return ``-1``.
297    This is the equivalent of the Python expression ``hash(o)``.
298
299
300 .. cfunction:: long PyObject_HashNotImplemented(PyObject *o)
301
302    Set a :exc:`TypeError` indicating that ``type(o)`` is not hashable and return ``-1``.
303    This function receives special treatment when stored in a ``tp_hash`` slot,
304    allowing a type to explicitly indicate to the interpreter that it is not
305    hashable.
306
307    .. versionadded:: 2.6
308
309
310 .. cfunction:: int PyObject_IsTrue(PyObject *o)
311
312    Returns ``1`` if the object *o* is considered to be true, and ``0`` otherwise.
313    This is equivalent to the Python expression ``not not o``.  On failure, return
314    ``-1``.
315
316
317 .. cfunction:: int PyObject_Not(PyObject *o)
318
319    Returns ``0`` if the object *o* is considered to be true, and ``1`` otherwise.
320    This is equivalent to the Python expression ``not o``.  On failure, return
321    ``-1``.
322
323
324 .. cfunction:: PyObject* PyObject_Type(PyObject *o)
325
326    .. index:: builtin: type
327
328    When *o* is non-*NULL*, returns a type object corresponding to the object type
329    of object *o*. On failure, raises :exc:`SystemError` and returns *NULL*.  This
330    is equivalent to the Python expression ``type(o)``. This function increments the
331    reference count of the return value. There's really no reason to use this
332    function instead of the common expression ``o->ob_type``, which returns a
333    pointer of type :ctype:`PyTypeObject\*`, except when the incremented reference
334    count is needed.
335
336
337 .. cfunction:: int PyObject_TypeCheck(PyObject *o, PyTypeObject *type)
338
339    Return true if the object *o* is of type *type* or a subtype of *type*.  Both
340    parameters must be non-*NULL*.
341
342    .. versionadded:: 2.2
343
344
345 .. cfunction:: Py_ssize_t PyObject_Length(PyObject *o)
346                Py_ssize_t PyObject_Size(PyObject *o)
347
348    .. index:: builtin: len
349
350    Return the length of object *o*.  If the object *o* provides either the sequence
351    and mapping protocols, the sequence length is returned.  On error, ``-1`` is
352    returned.  This is the equivalent to the Python expression ``len(o)``.
353
354    .. versionchanged:: 2.5
355       These functions returned an :ctype:`int` type. This might require
356       changes in your code for properly supporting 64-bit systems.
357
358
359 .. cfunction:: PyObject* PyObject_GetItem(PyObject *o, PyObject *key)
360
361    Return element of *o* corresponding to the object *key* or *NULL* on failure.
362    This is the equivalent of the Python expression ``o[key]``.
363
364
365 .. cfunction:: int PyObject_SetItem(PyObject *o, PyObject *key, PyObject *v)
366
367    Map the object *key* to the value *v*.  Returns ``-1`` on failure.  This is the
368    equivalent of the Python statement ``o[key] = v``.
369
370
371 .. cfunction:: int PyObject_DelItem(PyObject *o, PyObject *key)
372
373    Delete the mapping for *key* from *o*.  Returns ``-1`` on failure. This is the
374    equivalent of the Python statement ``del o[key]``.
375
376
377 .. cfunction:: int PyObject_AsFileDescriptor(PyObject *o)
378
379    Derives a file descriptor from a Python object.  If the object is an integer or
380    long integer, its value is returned.  If not, the object's :meth:`fileno` method
381    is called if it exists; the method must return an integer or long integer, which
382    is returned as the file descriptor value.  Returns ``-1`` on failure.
383
384
385 .. cfunction:: PyObject* PyObject_Dir(PyObject *o)
386
387    This is equivalent to the Python expression ``dir(o)``, returning a (possibly
388    empty) list of strings appropriate for the object argument, or *NULL* if there
389    was an error.  If the argument is *NULL*, this is like the Python ``dir()``,
390    returning the names of the current locals; in this case, if no execution frame
391    is active then *NULL* is returned but :cfunc:`PyErr_Occurred` will return false.
392
393
394 .. cfunction:: PyObject* PyObject_GetIter(PyObject *o)
395
396    This is equivalent to the Python expression ``iter(o)``. It returns a new
397    iterator for the object argument, or the object  itself if the object is already
398    an iterator.  Raises :exc:`TypeError` and returns *NULL* if the object cannot be
399    iterated.