blob: 8410934423fae332eee67cec169561ea6d49f732 [file] [log] [blame]
Georg Brandl54a3faa2008-01-20 09:30:57 +00001.. highlightlang:: c
2
3.. _object:
4
5Object Protocol
6===============
7
8
Georg Brandl60203b42010-10-06 10:11:56 +00009.. c:function:: int PyObject_Print(PyObject *o, FILE *fp, int flags)
Georg Brandl54a3faa2008-01-20 09:30:57 +000010
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
Georg Brandl60203b42010-10-06 10:11:56 +000017.. c:function:: int PyObject_HasAttr(PyObject *o, PyObject *attr_name)
Georg Brandl54a3faa2008-01-20 09:30:57 +000018
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
Georg Brandl60203b42010-10-06 10:11:56 +000024.. c:function:: int PyObject_HasAttrString(PyObject *o, const char *attr_name)
Georg Brandl54a3faa2008-01-20 09:30:57 +000025
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
Georg Brandl60203b42010-10-06 10:11:56 +000031.. c:function:: PyObject* PyObject_GetAttr(PyObject *o, PyObject *attr_name)
Georg Brandl54a3faa2008-01-20 09:30:57 +000032
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
Georg Brandl60203b42010-10-06 10:11:56 +000038.. c:function:: PyObject* PyObject_GetAttrString(PyObject *o, const char *attr_name)
Georg Brandl54a3faa2008-01-20 09:30:57 +000039
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 Brandl60203b42010-10-06 10:11:56 +000045.. c:function:: PyObject* PyObject_GenericGetAttr(PyObject *o, PyObject *name)
Benjamin Petersond23f8222009-04-05 19:13:16 +000046
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 Brandl60203b42010-10-06 10:11:56 +000055.. c:function:: int PyObject_SetAttr(PyObject *o, PyObject *attr_name, PyObject *v)
Georg Brandl54a3faa2008-01-20 09:30:57 +000056
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
Georg Brandl60203b42010-10-06 10:11:56 +000062.. c:function:: int PyObject_SetAttrString(PyObject *o, const char *attr_name, PyObject *v)
Georg Brandl54a3faa2008-01-20 09:30:57 +000063
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 Brandl60203b42010-10-06 10:11:56 +000069.. c:function:: int PyObject_GenericSetAttr(PyObject *o, PyObject *name, PyObject *value)
Benjamin Petersond23f8222009-04-05 19:13:16 +000070
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
Georg Brandl60203b42010-10-06 10:11:56 +000079.. c:function:: int PyObject_DelAttr(PyObject *o, PyObject *attr_name)
Georg Brandl54a3faa2008-01-20 09:30:57 +000080
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
Georg Brandl60203b42010-10-06 10:11:56 +000085.. c:function:: int PyObject_DelAttrString(PyObject *o, const char *attr_name)
Georg Brandl54a3faa2008-01-20 09:30:57 +000086
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
Georg Brandl60203b42010-10-06 10:11:56 +000091.. c:function:: PyObject* PyObject_RichCompare(PyObject *o1, PyObject *o2, int opid)
Georg Brandl54a3faa2008-01-20 09:30:57 +000092
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
Georg Brandl60203b42010-10-06 10:11:56 +0000101.. c:function:: int PyObject_RichCompareBool(PyObject *o1, PyObject *o2, int opid)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000102
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
Georg Brandl60203b42010-10-06 10:11:56 +0000112.. c:function:: PyObject* PyObject_Repr(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000113
114 .. index:: builtin: repr
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
Georg Brandl559e5d72008-06-11 18:37:52 +0000118 Python expression ``repr(o)``. Called by the :func:`repr` built-in function.
119
120
Georg Brandl60203b42010-10-06 10:11:56 +0000121.. c:function:: PyObject* PyObject_ASCII(PyObject *o)
Georg Brandl559e5d72008-06-11 18:37:52 +0000122
123 .. index:: builtin: ascii
124
Georg Brandl60203b42010-10-06 10:11:56 +0000125 As :c:func:`PyObject_Repr`, compute a string representation of object *o*, but
Georg Brandl559e5d72008-06-11 18:37:52 +0000126 escape the non-ASCII characters in the string returned by
Georg Brandl60203b42010-10-06 10:11:56 +0000127 :c:func:`PyObject_Repr` with ``\x``, ``\u`` or ``\U`` escapes. This generates
128 a string similar to that returned by :c:func:`PyObject_Repr` in Python 2.
Georg Brandl559e5d72008-06-11 18:37:52 +0000129 Called by the :func:`ascii` built-in function.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000130
131
Georg Brandl60203b42010-10-06 10:11:56 +0000132.. c:function:: PyObject* PyObject_Str(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000133
134 .. index:: builtin: str
135
136 Compute a string representation of object *o*. Returns the string
137 representation on success, *NULL* on failure. This is the equivalent of the
138 Python expression ``str(o)``. Called by the :func:`str` built-in function
139 and, therefore, by the :func:`print` function.
140
Georg Brandl60203b42010-10-06 10:11:56 +0000141.. c:function:: PyObject* PyObject_Bytes(PyObject *o)
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000142
143 .. index:: builtin: bytes
144
Alexandre Vassalottieb6f8de2009-12-31 03:56:09 +0000145 Compute a bytes representation of object *o*. *NULL* is returned on
146 failure and a bytes object on success. This is equivalent to the Python
147 expression ``bytes(o)``, when *o* is not an integer. Unlike ``bytes(o)``,
148 a TypeError is raised when *o* is an integer instead of a zero-initialized
149 bytes object.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000150
Georg Brandl60203b42010-10-06 10:11:56 +0000151.. c:function:: int PyObject_IsInstance(PyObject *inst, PyObject *cls)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000152
153 Returns ``1`` if *inst* is an instance of the class *cls* or a subclass of
154 *cls*, or ``0`` if not. On error, returns ``-1`` and sets an exception. If
Georg Brandl60203b42010-10-06 10:11:56 +0000155 *cls* is a type object rather than a class object, :c:func:`PyObject_IsInstance`
Georg Brandl54a3faa2008-01-20 09:30:57 +0000156 returns ``1`` if *inst* is of type *cls*. If *cls* is a tuple, the check will
157 be done against every entry in *cls*. The result will be ``1`` when at least one
158 of the checks returns ``1``, otherwise it will be ``0``. If *inst* is not a
159 class instance and *cls* is neither a type object, nor a class object, nor a
160 tuple, *inst* must have a :attr:`__class__` attribute --- the class relationship
161 of the value of that attribute with *cls* will be used to determine the result
162 of this function.
163
164
165Subclass determination is done in a fairly straightforward way, but includes a
166wrinkle that implementors of extensions to the class system may want to be aware
167of. If :class:`A` and :class:`B` are class objects, :class:`B` is a subclass of
168:class:`A` if it inherits from :class:`A` either directly or indirectly. If
169either is not a class object, a more general mechanism is used to determine the
170class relationship of the two objects. When testing if *B* is a subclass of
Georg Brandl60203b42010-10-06 10:11:56 +0000171*A*, if *A* is *B*, :c:func:`PyObject_IsSubclass` returns true. If *A* and *B*
Georg Brandl54a3faa2008-01-20 09:30:57 +0000172are different objects, *B*'s :attr:`__bases__` attribute is searched in a
173depth-first fashion for *A* --- the presence of the :attr:`__bases__` attribute
174is considered sufficient for this determination.
175
176
Georg Brandl60203b42010-10-06 10:11:56 +0000177.. c:function:: int PyObject_IsSubclass(PyObject *derived, PyObject *cls)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000178
179 Returns ``1`` if the class *derived* is identical to or derived from the class
180 *cls*, otherwise returns ``0``. In case of an error, returns ``-1``. If *cls*
181 is a tuple, the check will be done against every entry in *cls*. The result will
182 be ``1`` when at least one of the checks returns ``1``, otherwise it will be
183 ``0``. If either *derived* or *cls* is not an actual class object (or tuple),
184 this function uses the generic algorithm described above.
185
186
Georg Brandl60203b42010-10-06 10:11:56 +0000187.. c:function:: int PyCallable_Check(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000188
189 Determine if the object *o* is callable. Return ``1`` if the object is callable
190 and ``0`` otherwise. This function always succeeds.
191
192
Georg Brandl60203b42010-10-06 10:11:56 +0000193.. c:function:: PyObject* PyObject_Call(PyObject *callable_object, PyObject *args, PyObject *kw)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000194
195 Call a callable Python object *callable_object*, with arguments given by the
196 tuple *args*, and named arguments given by the dictionary *kw*. If no named
197 arguments are needed, *kw* may be *NULL*. *args* must not be *NULL*, use an
198 empty tuple if no arguments are needed. Returns the result of the call on
199 success, or *NULL* on failure. This is the equivalent of the Python expression
200 ``callable_object(*args, **kw)``.
201
202
Georg Brandl60203b42010-10-06 10:11:56 +0000203.. c:function:: PyObject* PyObject_CallObject(PyObject *callable_object, PyObject *args)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000204
205 Call a callable Python object *callable_object*, with arguments given by the
206 tuple *args*. If no arguments are needed, then *args* may be *NULL*. Returns
207 the result of the call on success, or *NULL* on failure. This is the equivalent
208 of the Python expression ``callable_object(*args)``.
209
210
Georg Brandl60203b42010-10-06 10:11:56 +0000211.. c:function:: PyObject* PyObject_CallFunction(PyObject *callable, char *format, ...)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000212
213 Call a callable Python object *callable*, with a variable number of C arguments.
Georg Brandl60203b42010-10-06 10:11:56 +0000214 The C arguments are described using a :c:func:`Py_BuildValue` style format
Georg Brandl54a3faa2008-01-20 09:30:57 +0000215 string. The format may be *NULL*, indicating that no arguments are provided.
216 Returns the result of the call on success, or *NULL* on failure. This is the
217 equivalent of the Python expression ``callable(*args)``. Note that if you only
Georg Brandl60203b42010-10-06 10:11:56 +0000218 pass :c:type:`PyObject \*` args, :c:func:`PyObject_CallFunctionObjArgs` is a
Georg Brandl54a3faa2008-01-20 09:30:57 +0000219 faster alternative.
220
221
Georg Brandl60203b42010-10-06 10:11:56 +0000222.. c:function:: PyObject* PyObject_CallMethod(PyObject *o, char *method, char *format, ...)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000223
224 Call the method named *method* of object *o* with a variable number of C
Georg Brandl60203b42010-10-06 10:11:56 +0000225 arguments. The C arguments are described by a :c:func:`Py_BuildValue` format
Georg Brandl54a3faa2008-01-20 09:30:57 +0000226 string that should produce a tuple. The format may be *NULL*, indicating that
227 no arguments are provided. Returns the result of the call on success, or *NULL*
228 on failure. This is the equivalent of the Python expression ``o.method(args)``.
Georg Brandl60203b42010-10-06 10:11:56 +0000229 Note that if you only pass :c:type:`PyObject \*` args,
230 :c:func:`PyObject_CallMethodObjArgs` is a faster alternative.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000231
232
Georg Brandl60203b42010-10-06 10:11:56 +0000233.. c:function:: PyObject* PyObject_CallFunctionObjArgs(PyObject *callable, ..., NULL)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000234
235 Call a callable Python object *callable*, with a variable number of
Georg Brandl60203b42010-10-06 10:11:56 +0000236 :c:type:`PyObject\*` arguments. The arguments are provided as a variable number
Georg Brandl54a3faa2008-01-20 09:30:57 +0000237 of parameters followed by *NULL*. Returns the result of the call on success, or
238 *NULL* on failure.
239
240
Georg Brandl60203b42010-10-06 10:11:56 +0000241.. c:function:: PyObject* PyObject_CallMethodObjArgs(PyObject *o, PyObject *name, ..., NULL)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000242
243 Calls a method of the object *o*, where the name of the method is given as a
244 Python string object in *name*. It is called with a variable number of
Georg Brandl60203b42010-10-06 10:11:56 +0000245 :c:type:`PyObject\*` arguments. The arguments are provided as a variable number
Georg Brandl54a3faa2008-01-20 09:30:57 +0000246 of parameters followed by *NULL*. Returns the result of the call on success, or
247 *NULL* on failure.
248
249
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000250.. c:function:: Py_hash_t PyObject_Hash(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000251
252 .. index:: builtin: hash
253
254 Compute and return the hash value of an object *o*. On failure, return ``-1``.
255 This is the equivalent of the Python expression ``hash(o)``.
256
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000257 .. versionchanged:: 3.2
Georg Brandl54a3faa2008-01-20 09:30:57 +0000258
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000259 The return type is now Py_hash_t. This is a signed integer the same size
260 as Py_ssize_t.
261
262
263.. c:function:: Py_hash_t PyObject_HashNotImplemented(PyObject *o)
Nick Coghlan7a70a3a2008-08-18 13:18:16 +0000264
Benjamin Petersone5384b02008-10-04 22:00:42 +0000265 Set a :exc:`TypeError` indicating that ``type(o)`` is not hashable and return ``-1``.
Nick Coghlan7a70a3a2008-08-18 13:18:16 +0000266 This function receives special treatment when stored in a ``tp_hash`` slot,
Benjamin Petersonc4fe6f32008-08-19 18:57:56 +0000267 allowing a type to explicitly indicate to the interpreter that it is not
Nick Coghlan7a70a3a2008-08-18 13:18:16 +0000268 hashable.
269
Nick Coghlan7a70a3a2008-08-18 13:18:16 +0000270
Georg Brandl60203b42010-10-06 10:11:56 +0000271.. c:function:: int PyObject_IsTrue(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000272
273 Returns ``1`` if the object *o* is considered to be true, and ``0`` otherwise.
274 This is equivalent to the Python expression ``not not o``. On failure, return
275 ``-1``.
276
277
Georg Brandl60203b42010-10-06 10:11:56 +0000278.. c:function:: int PyObject_Not(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000279
280 Returns ``0`` if the object *o* is considered to be true, and ``1`` otherwise.
281 This is equivalent to the Python expression ``not o``. On failure, return
282 ``-1``.
283
284
Georg Brandl60203b42010-10-06 10:11:56 +0000285.. c:function:: PyObject* PyObject_Type(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000286
287 .. index:: builtin: type
288
289 When *o* is non-*NULL*, returns a type object corresponding to the object type
290 of object *o*. On failure, raises :exc:`SystemError` and returns *NULL*. This
291 is equivalent to the Python expression ``type(o)``. This function increments the
292 reference count of the return value. There's really no reason to use this
293 function instead of the common expression ``o->ob_type``, which returns a
Georg Brandl60203b42010-10-06 10:11:56 +0000294 pointer of type :c:type:`PyTypeObject\*`, except when the incremented reference
Georg Brandl54a3faa2008-01-20 09:30:57 +0000295 count is needed.
296
297
Georg Brandl60203b42010-10-06 10:11:56 +0000298.. c:function:: int PyObject_TypeCheck(PyObject *o, PyTypeObject *type)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000299
300 Return true if the object *o* is of type *type* or a subtype of *type*. Both
301 parameters must be non-*NULL*.
302
303
Georg Brandl60203b42010-10-06 10:11:56 +0000304.. c:function:: Py_ssize_t PyObject_Length(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000305 Py_ssize_t PyObject_Size(PyObject *o)
306
307 .. index:: builtin: len
308
309 Return the length of object *o*. If the object *o* provides either the sequence
310 and mapping protocols, the sequence length is returned. On error, ``-1`` is
311 returned. This is the equivalent to the Python expression ``len(o)``.
312
Georg Brandl54a3faa2008-01-20 09:30:57 +0000313
Georg Brandl60203b42010-10-06 10:11:56 +0000314.. c:function:: PyObject* PyObject_GetItem(PyObject *o, PyObject *key)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000315
316 Return element of *o* corresponding to the object *key* or *NULL* on failure.
317 This is the equivalent of the Python expression ``o[key]``.
318
319
Georg Brandl60203b42010-10-06 10:11:56 +0000320.. c:function:: int PyObject_SetItem(PyObject *o, PyObject *key, PyObject *v)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000321
322 Map the object *key* to the value *v*. Returns ``-1`` on failure. This is the
323 equivalent of the Python statement ``o[key] = v``.
324
325
Georg Brandl60203b42010-10-06 10:11:56 +0000326.. c:function:: int PyObject_DelItem(PyObject *o, PyObject *key)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000327
328 Delete the mapping for *key* from *o*. Returns ``-1`` on failure. This is the
329 equivalent of the Python statement ``del o[key]``.
330
331
Georg Brandl60203b42010-10-06 10:11:56 +0000332.. c:function:: PyObject* PyObject_Dir(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000333
334 This is equivalent to the Python expression ``dir(o)``, returning a (possibly
335 empty) list of strings appropriate for the object argument, or *NULL* if there
336 was an error. If the argument is *NULL*, this is like the Python ``dir()``,
337 returning the names of the current locals; in this case, if no execution frame
Georg Brandl60203b42010-10-06 10:11:56 +0000338 is active then *NULL* is returned but :c:func:`PyErr_Occurred` will return false.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000339
340
Georg Brandl60203b42010-10-06 10:11:56 +0000341.. c:function:: PyObject* PyObject_GetIter(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000342
343 This is equivalent to the Python expression ``iter(o)``. It returns a new
344 iterator for the object argument, or the object itself if the object is already
345 an iterator. Raises :exc:`TypeError` and returns *NULL* if the object cannot be
346 iterated.