blob: d9f29d15ff7a918c6968913bae252b2c5671e30e [file] [log] [blame]
Georg Brandl54a3faa2008-01-20 09:30:57 +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
45.. cfunction:: int PyObject_SetAttr(PyObject *o, PyObject *attr_name, PyObject *v)
46
47 Set the value of the attribute named *attr_name*, for object *o*, to the value
48 *v*. Returns ``-1`` on failure. This is the equivalent of the Python statement
49 ``o.attr_name = v``.
50
51
52.. cfunction:: int PyObject_SetAttrString(PyObject *o, const char *attr_name, PyObject *v)
53
54 Set the value of the attribute named *attr_name*, for object *o*, to the value
55 *v*. Returns ``-1`` on failure. This is the equivalent of the Python statement
56 ``o.attr_name = v``.
57
58
59.. cfunction:: int PyObject_DelAttr(PyObject *o, PyObject *attr_name)
60
61 Delete attribute named *attr_name*, for object *o*. Returns ``-1`` on failure.
62 This is the equivalent of the Python statement ``del o.attr_name``.
63
64
65.. cfunction:: int PyObject_DelAttrString(PyObject *o, const char *attr_name)
66
67 Delete attribute named *attr_name*, for object *o*. Returns ``-1`` on failure.
68 This is the equivalent of the Python statement ``del o.attr_name``.
69
70
71.. cfunction:: PyObject* PyObject_RichCompare(PyObject *o1, PyObject *o2, int opid)
72
73 Compare the values of *o1* and *o2* using the operation specified by *opid*,
74 which must be one of :const:`Py_LT`, :const:`Py_LE`, :const:`Py_EQ`,
75 :const:`Py_NE`, :const:`Py_GT`, or :const:`Py_GE`, corresponding to ``<``,
76 ``<=``, ``==``, ``!=``, ``>``, or ``>=`` respectively. This is the equivalent of
77 the Python expression ``o1 op o2``, where ``op`` is the operator corresponding
78 to *opid*. Returns the value of the comparison on success, or *NULL* on failure.
79
80
81.. cfunction:: int PyObject_RichCompareBool(PyObject *o1, PyObject *o2, int opid)
82
83 Compare the values of *o1* and *o2* using the operation specified by *opid*,
84 which must be one of :const:`Py_LT`, :const:`Py_LE`, :const:`Py_EQ`,
85 :const:`Py_NE`, :const:`Py_GT`, or :const:`Py_GE`, corresponding to ``<``,
86 ``<=``, ``==``, ``!=``, ``>``, or ``>=`` respectively. Returns ``-1`` on error,
87 ``0`` if the result is false, ``1`` otherwise. This is the equivalent of the
88 Python expression ``o1 op o2``, where ``op`` is the operator corresponding to
89 *opid*.
90
91
Georg Brandl54a3faa2008-01-20 09:30:57 +000092.. cfunction:: PyObject* PyObject_Repr(PyObject *o)
93
94 .. index:: builtin: repr
95
96 Compute a string representation of object *o*. Returns the string
97 representation on success, *NULL* on failure. This is the equivalent of the
Georg Brandl559e5d72008-06-11 18:37:52 +000098 Python expression ``repr(o)``. Called by the :func:`repr` built-in function.
99
100
101.. cfunction:: PyObject* PyObject_ASCII(PyObject *o)
102
103 .. index:: builtin: ascii
104
105 As :cfunc:`PyObject_Repr`, compute a string representation of object *o*, but
106 escape the non-ASCII characters in the string returned by
107 :cfunc:`PyObject_Repr` with ``\x``, ``\u`` or ``\U`` escapes. This generates
108 a string similar to that returned by :cfunc:`PyObject_Repr` in Python 2.
109 Called by the :func:`ascii` built-in function.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000110
111
112.. cfunction:: PyObject* PyObject_Str(PyObject *o)
113
114 .. index:: builtin: str
115
116 Compute a string representation of object *o*. Returns the string
117 representation on success, *NULL* on failure. This is the equivalent of the
118 Python expression ``str(o)``. Called by the :func:`str` built-in function
119 and, therefore, by the :func:`print` function.
120
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000121.. cfunction:: PyObject* PyObject_Bytes(PyObject *o)
122
123 .. index:: builtin: bytes
124
125 Compute a bytes representation of object *o*. *NULL* is returned on failure
126 and a bytes object on success. This is equivalent to the Python expression
127 ``bytes(o)``.
128
Georg Brandl54a3faa2008-01-20 09:30:57 +0000129
Georg Brandl54a3faa2008-01-20 09:30:57 +0000130.. cfunction:: int PyObject_IsInstance(PyObject *inst, PyObject *cls)
131
132 Returns ``1`` if *inst* is an instance of the class *cls* or a subclass of
133 *cls*, or ``0`` if not. On error, returns ``-1`` and sets an exception. If
134 *cls* is a type object rather than a class object, :cfunc:`PyObject_IsInstance`
135 returns ``1`` if *inst* is of type *cls*. If *cls* is a tuple, the check will
136 be done against every entry in *cls*. The result will be ``1`` when at least one
137 of the checks returns ``1``, otherwise it will be ``0``. If *inst* is not a
138 class instance and *cls* is neither a type object, nor a class object, nor a
139 tuple, *inst* must have a :attr:`__class__` attribute --- the class relationship
140 of the value of that attribute with *cls* will be used to determine the result
141 of this function.
142
143
144Subclass determination is done in a fairly straightforward way, but includes a
145wrinkle that implementors of extensions to the class system may want to be aware
146of. If :class:`A` and :class:`B` are class objects, :class:`B` is a subclass of
147:class:`A` if it inherits from :class:`A` either directly or indirectly. If
148either is not a class object, a more general mechanism is used to determine the
149class relationship of the two objects. When testing if *B* is a subclass of
150*A*, if *A* is *B*, :cfunc:`PyObject_IsSubclass` returns true. If *A* and *B*
151are different objects, *B*'s :attr:`__bases__` attribute is searched in a
152depth-first fashion for *A* --- the presence of the :attr:`__bases__` attribute
153is considered sufficient for this determination.
154
155
156.. cfunction:: int PyObject_IsSubclass(PyObject *derived, PyObject *cls)
157
158 Returns ``1`` if the class *derived* is identical to or derived from the class
159 *cls*, otherwise returns ``0``. In case of an error, returns ``-1``. If *cls*
160 is a tuple, the check will be done against every entry in *cls*. The result will
161 be ``1`` when at least one of the checks returns ``1``, otherwise it will be
162 ``0``. If either *derived* or *cls* is not an actual class object (or tuple),
163 this function uses the generic algorithm described above.
164
165
166.. cfunction:: int PyCallable_Check(PyObject *o)
167
168 Determine if the object *o* is callable. Return ``1`` if the object is callable
169 and ``0`` otherwise. This function always succeeds.
170
171
172.. cfunction:: PyObject* PyObject_Call(PyObject *callable_object, PyObject *args, PyObject *kw)
173
174 Call a callable Python object *callable_object*, with arguments given by the
175 tuple *args*, and named arguments given by the dictionary *kw*. If no named
176 arguments are needed, *kw* may be *NULL*. *args* must not be *NULL*, use an
177 empty tuple if no arguments are needed. Returns the result of the call on
178 success, or *NULL* on failure. This is the equivalent of the Python expression
179 ``callable_object(*args, **kw)``.
180
181
182.. cfunction:: PyObject* PyObject_CallObject(PyObject *callable_object, PyObject *args)
183
184 Call a callable Python object *callable_object*, with arguments given by the
185 tuple *args*. If no arguments are needed, then *args* may be *NULL*. Returns
186 the result of the call on success, or *NULL* on failure. This is the equivalent
187 of the Python expression ``callable_object(*args)``.
188
189
190.. cfunction:: PyObject* PyObject_CallFunction(PyObject *callable, char *format, ...)
191
192 Call a callable Python object *callable*, with a variable number of C arguments.
193 The C arguments are described using a :cfunc:`Py_BuildValue` style format
194 string. The format may be *NULL*, indicating that no arguments are provided.
195 Returns the result of the call on success, or *NULL* on failure. This is the
196 equivalent of the Python expression ``callable(*args)``. Note that if you only
197 pass :ctype:`PyObject \*` args, :cfunc:`PyObject_CallFunctionObjArgs` is a
198 faster alternative.
199
200
201.. cfunction:: PyObject* PyObject_CallMethod(PyObject *o, char *method, char *format, ...)
202
203 Call the method named *method* of object *o* with a variable number of C
204 arguments. The C arguments are described by a :cfunc:`Py_BuildValue` format
205 string that should produce a tuple. The format may be *NULL*, indicating that
206 no arguments are provided. Returns the result of the call on success, or *NULL*
207 on failure. This is the equivalent of the Python expression ``o.method(args)``.
208 Note that if you only pass :ctype:`PyObject \*` args,
209 :cfunc:`PyObject_CallMethodObjArgs` is a faster alternative.
210
211
212.. cfunction:: PyObject* PyObject_CallFunctionObjArgs(PyObject *callable, ..., NULL)
213
214 Call a callable Python object *callable*, with a variable number of
215 :ctype:`PyObject\*` arguments. The arguments are provided as a variable number
216 of parameters followed by *NULL*. Returns the result of the call on success, or
217 *NULL* on failure.
218
219
220.. cfunction:: PyObject* PyObject_CallMethodObjArgs(PyObject *o, PyObject *name, ..., NULL)
221
222 Calls a method of the object *o*, where the name of the method is given as a
223 Python string object in *name*. It is called with a variable number of
224 :ctype:`PyObject\*` arguments. The arguments are provided as a variable number
225 of parameters followed by *NULL*. Returns the result of the call on success, or
226 *NULL* on failure.
227
228
229.. cfunction:: long PyObject_Hash(PyObject *o)
230
231 .. index:: builtin: hash
232
233 Compute and return the hash value of an object *o*. On failure, return ``-1``.
234 This is the equivalent of the Python expression ``hash(o)``.
235
236
Nick Coghlan7a70a3a2008-08-18 13:18:16 +0000237.. cfunction:: long PyObject_HashNotImplemented(PyObject *o)
238
Benjamin Petersone5384b02008-10-04 22:00:42 +0000239 Set a :exc:`TypeError` indicating that ``type(o)`` is not hashable and return ``-1``.
Nick Coghlan7a70a3a2008-08-18 13:18:16 +0000240 This function receives special treatment when stored in a ``tp_hash`` slot,
Benjamin Petersonc4fe6f32008-08-19 18:57:56 +0000241 allowing a type to explicitly indicate to the interpreter that it is not
Nick Coghlan7a70a3a2008-08-18 13:18:16 +0000242 hashable.
243
Nick Coghlan7a70a3a2008-08-18 13:18:16 +0000244
Georg Brandl54a3faa2008-01-20 09:30:57 +0000245.. cfunction:: int PyObject_IsTrue(PyObject *o)
246
247 Returns ``1`` if the object *o* is considered to be true, and ``0`` otherwise.
248 This is equivalent to the Python expression ``not not o``. On failure, return
249 ``-1``.
250
251
252.. cfunction:: int PyObject_Not(PyObject *o)
253
254 Returns ``0`` if the object *o* is considered to be true, and ``1`` otherwise.
255 This is equivalent to the Python expression ``not o``. On failure, return
256 ``-1``.
257
258
259.. cfunction:: PyObject* PyObject_Type(PyObject *o)
260
261 .. index:: builtin: type
262
263 When *o* is non-*NULL*, returns a type object corresponding to the object type
264 of object *o*. On failure, raises :exc:`SystemError` and returns *NULL*. This
265 is equivalent to the Python expression ``type(o)``. This function increments the
266 reference count of the return value. There's really no reason to use this
267 function instead of the common expression ``o->ob_type``, which returns a
268 pointer of type :ctype:`PyTypeObject\*`, except when the incremented reference
269 count is needed.
270
271
272.. cfunction:: int PyObject_TypeCheck(PyObject *o, PyTypeObject *type)
273
274 Return true if the object *o* is of type *type* or a subtype of *type*. Both
275 parameters must be non-*NULL*.
276
277
278.. cfunction:: Py_ssize_t PyObject_Length(PyObject *o)
279 Py_ssize_t PyObject_Size(PyObject *o)
280
281 .. index:: builtin: len
282
283 Return the length of object *o*. If the object *o* provides either the sequence
284 and mapping protocols, the sequence length is returned. On error, ``-1`` is
285 returned. This is the equivalent to the Python expression ``len(o)``.
286
287
288.. cfunction:: PyObject* PyObject_GetItem(PyObject *o, PyObject *key)
289
290 Return element of *o* corresponding to the object *key* or *NULL* on failure.
291 This is the equivalent of the Python expression ``o[key]``.
292
293
294.. cfunction:: int PyObject_SetItem(PyObject *o, PyObject *key, PyObject *v)
295
296 Map the object *key* to the value *v*. Returns ``-1`` on failure. This is the
297 equivalent of the Python statement ``o[key] = v``.
298
299
300.. cfunction:: int PyObject_DelItem(PyObject *o, PyObject *key)
301
302 Delete the mapping for *key* from *o*. Returns ``-1`` on failure. This is the
303 equivalent of the Python statement ``del o[key]``.
304
305
306.. cfunction:: PyObject* PyObject_Dir(PyObject *o)
307
308 This is equivalent to the Python expression ``dir(o)``, returning a (possibly
309 empty) list of strings appropriate for the object argument, or *NULL* if there
310 was an error. If the argument is *NULL*, this is like the Python ``dir()``,
311 returning the names of the current locals; in this case, if no execution frame
312 is active then *NULL* is returned but :cfunc:`PyErr_Occurred` will return false.
313
314
315.. cfunction:: PyObject* PyObject_GetIter(PyObject *o)
316
317 This is equivalent to the Python expression ``iter(o)``. It returns a new
318 iterator for the object argument, or the object itself if the object is already
319 an iterator. Raises :exc:`TypeError` and returns *NULL* if the object cannot be
320 iterated.