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