blob: 88ba5ac0c4efcb8491daa41fc210d6e3c9f30c90 [file] [log] [blame]
Georg Brandl54a3faa2008-01-20 09:30:57 +00001.. highlightlang:: c
2
3.. _object:
4
5Object Protocol
6===============
7
8
Brian Curtin49281072011-08-11 09:41:31 -05009.. c:var:: PyObject* Py_NotImplemented
10
11 The ``NotImplemented`` singleton, used to signal that an operation is
12 not implemented for the given type combination.
13
14
15.. c:macro:: Py_RETURN_NOTIMPLEMENTED
16
17 Properly handle returning :c:data:`Py_NotImplemented` from within a C
18 function (that is, increment the reference count of NotImplemented and
19 return it).
20
21
Georg Brandl60203b42010-10-06 10:11:56 +000022.. c:function:: int PyObject_Print(PyObject *o, FILE *fp, int flags)
Georg Brandl54a3faa2008-01-20 09:30:57 +000023
24 Print an object *o*, on file *fp*. Returns ``-1`` on error. The flags argument
25 is used to enable certain printing options. The only option currently supported
26 is :const:`Py_PRINT_RAW`; if given, the :func:`str` of the object is written
27 instead of the :func:`repr`.
28
29
Georg Brandl60203b42010-10-06 10:11:56 +000030.. c:function:: int PyObject_HasAttr(PyObject *o, PyObject *attr_name)
Georg Brandl54a3faa2008-01-20 09:30:57 +000031
32 Returns ``1`` if *o* has the attribute *attr_name*, and ``0`` otherwise. This
33 is equivalent to the Python expression ``hasattr(o, attr_name)``. This function
34 always succeeds.
35
36
Georg Brandl60203b42010-10-06 10:11:56 +000037.. c:function:: int PyObject_HasAttrString(PyObject *o, const char *attr_name)
Georg Brandl54a3faa2008-01-20 09:30:57 +000038
39 Returns ``1`` if *o* has the attribute *attr_name*, and ``0`` otherwise. This
40 is equivalent to the Python expression ``hasattr(o, attr_name)``. This function
41 always succeeds.
42
43
Georg Brandl60203b42010-10-06 10:11:56 +000044.. c:function:: PyObject* PyObject_GetAttr(PyObject *o, PyObject *attr_name)
Georg Brandl54a3faa2008-01-20 09:30:57 +000045
46 Retrieve an attribute named *attr_name* from object *o*. Returns the attribute
47 value on success, or *NULL* on failure. This is the equivalent of the Python
48 expression ``o.attr_name``.
49
50
Georg Brandl60203b42010-10-06 10:11:56 +000051.. c:function:: PyObject* PyObject_GetAttrString(PyObject *o, const char *attr_name)
Georg Brandl54a3faa2008-01-20 09:30:57 +000052
53 Retrieve an attribute named *attr_name* from object *o*. Returns the attribute
54 value on success, or *NULL* on failure. This is the equivalent of the Python
55 expression ``o.attr_name``.
56
57
Georg Brandl60203b42010-10-06 10:11:56 +000058.. c:function:: PyObject* PyObject_GenericGetAttr(PyObject *o, PyObject *name)
Benjamin Petersond23f8222009-04-05 19:13:16 +000059
60 Generic attribute getter function that is meant to be put into a type
61 object's ``tp_getattro`` slot. It looks for a descriptor in the dictionary
62 of classes in the object's MRO as well as an attribute in the object's
63 :attr:`__dict__` (if present). As outlined in :ref:`descriptors`, data
64 descriptors take preference over instance attributes, while non-data
65 descriptors don't. Otherwise, an :exc:`AttributeError` is raised.
66
67
Georg Brandl60203b42010-10-06 10:11:56 +000068.. c:function:: int PyObject_SetAttr(PyObject *o, PyObject *attr_name, PyObject *v)
Georg Brandl54a3faa2008-01-20 09:30:57 +000069
70 Set the value of the attribute named *attr_name*, for object *o*, to the value
71 *v*. Returns ``-1`` on failure. This is the equivalent of the Python statement
72 ``o.attr_name = v``.
73
74
Georg Brandl60203b42010-10-06 10:11:56 +000075.. c:function:: int PyObject_SetAttrString(PyObject *o, const char *attr_name, PyObject *v)
Georg Brandl54a3faa2008-01-20 09:30:57 +000076
77 Set the value of the attribute named *attr_name*, for object *o*, to the value
78 *v*. Returns ``-1`` on failure. This is the equivalent of the Python statement
79 ``o.attr_name = v``.
80
81
Georg Brandl60203b42010-10-06 10:11:56 +000082.. c:function:: int PyObject_GenericSetAttr(PyObject *o, PyObject *name, PyObject *value)
Benjamin Petersond23f8222009-04-05 19:13:16 +000083
84 Generic attribute setter function that is meant to be put into a type
85 object's ``tp_setattro`` slot. It looks for a data descriptor in the
86 dictionary of classes in the object's MRO, and if found it takes preference
87 over setting the attribute in the instance dictionary. Otherwise, the
88 attribute is set in the object's :attr:`__dict__` (if present). Otherwise,
89 an :exc:`AttributeError` is raised and ``-1`` is returned.
90
91
Georg Brandl60203b42010-10-06 10:11:56 +000092.. c:function:: int PyObject_DelAttr(PyObject *o, PyObject *attr_name)
Georg Brandl54a3faa2008-01-20 09:30:57 +000093
94 Delete attribute named *attr_name*, for object *o*. Returns ``-1`` on failure.
95 This is the equivalent of the Python statement ``del o.attr_name``.
96
97
Georg Brandl60203b42010-10-06 10:11:56 +000098.. c:function:: int PyObject_DelAttrString(PyObject *o, const char *attr_name)
Georg Brandl54a3faa2008-01-20 09:30:57 +000099
100 Delete attribute named *attr_name*, for object *o*. Returns ``-1`` on failure.
101 This is the equivalent of the Python statement ``del o.attr_name``.
102
103
Georg Brandl60203b42010-10-06 10:11:56 +0000104.. c:function:: PyObject* PyObject_RichCompare(PyObject *o1, PyObject *o2, int opid)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000105
106 Compare the values of *o1* and *o2* using the operation specified by *opid*,
107 which must be one of :const:`Py_LT`, :const:`Py_LE`, :const:`Py_EQ`,
108 :const:`Py_NE`, :const:`Py_GT`, or :const:`Py_GE`, corresponding to ``<``,
109 ``<=``, ``==``, ``!=``, ``>``, or ``>=`` respectively. This is the equivalent of
110 the Python expression ``o1 op o2``, where ``op`` is the operator corresponding
111 to *opid*. Returns the value of the comparison on success, or *NULL* on failure.
112
113
Georg Brandl60203b42010-10-06 10:11:56 +0000114.. c:function:: int PyObject_RichCompareBool(PyObject *o1, PyObject *o2, int opid)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000115
116 Compare the values of *o1* and *o2* using the operation specified by *opid*,
117 which must be one of :const:`Py_LT`, :const:`Py_LE`, :const:`Py_EQ`,
118 :const:`Py_NE`, :const:`Py_GT`, or :const:`Py_GE`, corresponding to ``<``,
119 ``<=``, ``==``, ``!=``, ``>``, or ``>=`` respectively. Returns ``-1`` on error,
120 ``0`` if the result is false, ``1`` otherwise. This is the equivalent of the
121 Python expression ``o1 op o2``, where ``op`` is the operator corresponding to
122 *opid*.
123
Eli Benderskyad30c422011-01-15 10:23:34 +0000124.. note::
125 If *o1* and *o2* are the same object, :c:func:`PyObject_RichCompareBool`
126 will always return ``1`` for :const:`Py_EQ` and ``0`` for :const:`Py_NE`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000127
Georg Brandl60203b42010-10-06 10:11:56 +0000128.. c:function:: PyObject* PyObject_Repr(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000129
130 .. index:: builtin: repr
131
132 Compute a string representation of object *o*. Returns the string
133 representation on success, *NULL* on failure. This is the equivalent of the
Georg Brandl559e5d72008-06-11 18:37:52 +0000134 Python expression ``repr(o)``. Called by the :func:`repr` built-in function.
135
136
Georg Brandl60203b42010-10-06 10:11:56 +0000137.. c:function:: PyObject* PyObject_ASCII(PyObject *o)
Georg Brandl559e5d72008-06-11 18:37:52 +0000138
139 .. index:: builtin: ascii
140
Georg Brandl60203b42010-10-06 10:11:56 +0000141 As :c:func:`PyObject_Repr`, compute a string representation of object *o*, but
Georg Brandl559e5d72008-06-11 18:37:52 +0000142 escape the non-ASCII characters in the string returned by
Georg Brandl60203b42010-10-06 10:11:56 +0000143 :c:func:`PyObject_Repr` with ``\x``, ``\u`` or ``\U`` escapes. This generates
144 a string similar to that returned by :c:func:`PyObject_Repr` in Python 2.
Georg Brandl559e5d72008-06-11 18:37:52 +0000145 Called by the :func:`ascii` built-in function.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000146
147
Georg Brandl60203b42010-10-06 10:11:56 +0000148.. c:function:: PyObject* PyObject_Str(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000149
150 .. index:: builtin: str
151
152 Compute a string representation of object *o*. Returns the string
153 representation on success, *NULL* on failure. This is the equivalent of the
154 Python expression ``str(o)``. Called by the :func:`str` built-in function
155 and, therefore, by the :func:`print` function.
156
Georg Brandl60203b42010-10-06 10:11:56 +0000157.. c:function:: PyObject* PyObject_Bytes(PyObject *o)
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000158
159 .. index:: builtin: bytes
160
Alexandre Vassalottieb6f8de2009-12-31 03:56:09 +0000161 Compute a bytes representation of object *o*. *NULL* is returned on
162 failure and a bytes object on success. This is equivalent to the Python
163 expression ``bytes(o)``, when *o* is not an integer. Unlike ``bytes(o)``,
164 a TypeError is raised when *o* is an integer instead of a zero-initialized
165 bytes object.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000166
Georg Brandl60203b42010-10-06 10:11:56 +0000167.. c:function:: int PyObject_IsInstance(PyObject *inst, PyObject *cls)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000168
169 Returns ``1`` if *inst* is an instance of the class *cls* or a subclass of
170 *cls*, or ``0`` if not. On error, returns ``-1`` and sets an exception. If
Georg Brandl60203b42010-10-06 10:11:56 +0000171 *cls* is a type object rather than a class object, :c:func:`PyObject_IsInstance`
Georg Brandl54a3faa2008-01-20 09:30:57 +0000172 returns ``1`` if *inst* is of type *cls*. If *cls* is a tuple, the check will
173 be done against every entry in *cls*. The result will be ``1`` when at least one
174 of the checks returns ``1``, otherwise it will be ``0``. If *inst* is not a
175 class instance and *cls* is neither a type object, nor a class object, nor a
176 tuple, *inst* must have a :attr:`__class__` attribute --- the class relationship
177 of the value of that attribute with *cls* will be used to determine the result
178 of this function.
179
180
181Subclass determination is done in a fairly straightforward way, but includes a
182wrinkle that implementors of extensions to the class system may want to be aware
183of. If :class:`A` and :class:`B` are class objects, :class:`B` is a subclass of
184:class:`A` if it inherits from :class:`A` either directly or indirectly. If
185either is not a class object, a more general mechanism is used to determine the
186class relationship of the two objects. When testing if *B* is a subclass of
Georg Brandl60203b42010-10-06 10:11:56 +0000187*A*, if *A* is *B*, :c:func:`PyObject_IsSubclass` returns true. If *A* and *B*
Georg Brandl54a3faa2008-01-20 09:30:57 +0000188are different objects, *B*'s :attr:`__bases__` attribute is searched in a
189depth-first fashion for *A* --- the presence of the :attr:`__bases__` attribute
190is considered sufficient for this determination.
191
192
Georg Brandl60203b42010-10-06 10:11:56 +0000193.. c:function:: int PyObject_IsSubclass(PyObject *derived, PyObject *cls)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000194
195 Returns ``1`` if the class *derived* is identical to or derived from the class
196 *cls*, otherwise returns ``0``. In case of an error, returns ``-1``. If *cls*
197 is a tuple, the check will be done against every entry in *cls*. The result will
198 be ``1`` when at least one of the checks returns ``1``, otherwise it will be
199 ``0``. If either *derived* or *cls* is not an actual class object (or tuple),
200 this function uses the generic algorithm described above.
201
202
Georg Brandl60203b42010-10-06 10:11:56 +0000203.. c:function:: int PyCallable_Check(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000204
205 Determine if the object *o* is callable. Return ``1`` if the object is callable
206 and ``0`` otherwise. This function always succeeds.
207
208
Georg Brandl60203b42010-10-06 10:11:56 +0000209.. c:function:: PyObject* PyObject_Call(PyObject *callable_object, PyObject *args, PyObject *kw)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000210
211 Call a callable Python object *callable_object*, with arguments given by the
212 tuple *args*, and named arguments given by the dictionary *kw*. If no named
213 arguments are needed, *kw* may be *NULL*. *args* must not be *NULL*, use an
214 empty tuple if no arguments are needed. Returns the result of the call on
215 success, or *NULL* on failure. This is the equivalent of the Python expression
216 ``callable_object(*args, **kw)``.
217
218
Georg Brandl60203b42010-10-06 10:11:56 +0000219.. c:function:: PyObject* PyObject_CallObject(PyObject *callable_object, PyObject *args)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000220
221 Call a callable Python object *callable_object*, with arguments given by the
222 tuple *args*. If no arguments are needed, then *args* may be *NULL*. Returns
223 the result of the call on success, or *NULL* on failure. This is the equivalent
224 of the Python expression ``callable_object(*args)``.
225
226
Georg Brandl60203b42010-10-06 10:11:56 +0000227.. c:function:: PyObject* PyObject_CallFunction(PyObject *callable, char *format, ...)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000228
229 Call a callable Python object *callable*, with a variable number of C arguments.
Georg Brandl60203b42010-10-06 10:11:56 +0000230 The C arguments are described using a :c:func:`Py_BuildValue` style format
Georg Brandl54a3faa2008-01-20 09:30:57 +0000231 string. The format may be *NULL*, indicating that no arguments are provided.
232 Returns the result of the call on success, or *NULL* on failure. This is the
233 equivalent of the Python expression ``callable(*args)``. Note that if you only
Georg Brandl60203b42010-10-06 10:11:56 +0000234 pass :c:type:`PyObject \*` args, :c:func:`PyObject_CallFunctionObjArgs` is a
Georg Brandl54a3faa2008-01-20 09:30:57 +0000235 faster alternative.
236
237
Georg Brandl60203b42010-10-06 10:11:56 +0000238.. c:function:: PyObject* PyObject_CallMethod(PyObject *o, char *method, char *format, ...)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000239
240 Call the method named *method* of object *o* with a variable number of C
Georg Brandl60203b42010-10-06 10:11:56 +0000241 arguments. The C arguments are described by a :c:func:`Py_BuildValue` format
Georg Brandl54a3faa2008-01-20 09:30:57 +0000242 string that should produce a tuple. The format may be *NULL*, indicating that
243 no arguments are provided. Returns the result of the call on success, or *NULL*
244 on failure. This is the equivalent of the Python expression ``o.method(args)``.
Georg Brandl60203b42010-10-06 10:11:56 +0000245 Note that if you only pass :c:type:`PyObject \*` args,
246 :c:func:`PyObject_CallMethodObjArgs` is a faster alternative.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000247
248
Georg Brandl60203b42010-10-06 10:11:56 +0000249.. c:function:: PyObject* PyObject_CallFunctionObjArgs(PyObject *callable, ..., NULL)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000250
251 Call a callable Python object *callable*, with a variable number of
Georg Brandl60203b42010-10-06 10:11:56 +0000252 :c:type:`PyObject\*` arguments. The arguments are provided as a variable number
Georg Brandl54a3faa2008-01-20 09:30:57 +0000253 of parameters followed by *NULL*. Returns the result of the call on success, or
254 *NULL* on failure.
255
256
Georg Brandl60203b42010-10-06 10:11:56 +0000257.. c:function:: PyObject* PyObject_CallMethodObjArgs(PyObject *o, PyObject *name, ..., NULL)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000258
259 Calls a method of the object *o*, where the name of the method is given as a
260 Python string object in *name*. It is called with a variable number of
Georg Brandl60203b42010-10-06 10:11:56 +0000261 :c:type:`PyObject\*` arguments. The arguments are provided as a variable number
Georg Brandl54a3faa2008-01-20 09:30:57 +0000262 of parameters followed by *NULL*. Returns the result of the call on success, or
263 *NULL* on failure.
264
265
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000266.. c:function:: Py_hash_t PyObject_Hash(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000267
268 .. index:: builtin: hash
269
270 Compute and return the hash value of an object *o*. On failure, return ``-1``.
271 This is the equivalent of the Python expression ``hash(o)``.
272
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000273 .. versionchanged:: 3.2
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000274 The return type is now Py_hash_t. This is a signed integer the same size
275 as Py_ssize_t.
276
277
278.. c:function:: Py_hash_t PyObject_HashNotImplemented(PyObject *o)
Nick Coghlan7a70a3a2008-08-18 13:18:16 +0000279
Benjamin Petersone5384b02008-10-04 22:00:42 +0000280 Set a :exc:`TypeError` indicating that ``type(o)`` is not hashable and return ``-1``.
Nick Coghlan7a70a3a2008-08-18 13:18:16 +0000281 This function receives special treatment when stored in a ``tp_hash`` slot,
Benjamin Petersonc4fe6f32008-08-19 18:57:56 +0000282 allowing a type to explicitly indicate to the interpreter that it is not
Nick Coghlan7a70a3a2008-08-18 13:18:16 +0000283 hashable.
284
Nick Coghlan7a70a3a2008-08-18 13:18:16 +0000285
Georg Brandl60203b42010-10-06 10:11:56 +0000286.. c:function:: int PyObject_IsTrue(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000287
288 Returns ``1`` if the object *o* is considered to be true, and ``0`` otherwise.
289 This is equivalent to the Python expression ``not not o``. On failure, return
290 ``-1``.
291
292
Georg Brandl60203b42010-10-06 10:11:56 +0000293.. c:function:: int PyObject_Not(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000294
295 Returns ``0`` if the object *o* is considered to be true, and ``1`` otherwise.
296 This is equivalent to the Python expression ``not o``. On failure, return
297 ``-1``.
298
299
Georg Brandl60203b42010-10-06 10:11:56 +0000300.. c:function:: PyObject* PyObject_Type(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000301
302 .. index:: builtin: type
303
304 When *o* is non-*NULL*, returns a type object corresponding to the object type
305 of object *o*. On failure, raises :exc:`SystemError` and returns *NULL*. This
306 is equivalent to the Python expression ``type(o)``. This function increments the
307 reference count of the return value. There's really no reason to use this
308 function instead of the common expression ``o->ob_type``, which returns a
Georg Brandl60203b42010-10-06 10:11:56 +0000309 pointer of type :c:type:`PyTypeObject\*`, except when the incremented reference
Georg Brandl54a3faa2008-01-20 09:30:57 +0000310 count is needed.
311
312
Georg Brandl60203b42010-10-06 10:11:56 +0000313.. c:function:: int PyObject_TypeCheck(PyObject *o, PyTypeObject *type)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000314
315 Return true if the object *o* is of type *type* or a subtype of *type*. Both
316 parameters must be non-*NULL*.
317
318
Georg Brandl60203b42010-10-06 10:11:56 +0000319.. c:function:: Py_ssize_t PyObject_Length(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000320 Py_ssize_t PyObject_Size(PyObject *o)
321
322 .. index:: builtin: len
323
324 Return the length of object *o*. If the object *o* provides either the sequence
325 and mapping protocols, the sequence length is returned. On error, ``-1`` is
326 returned. This is the equivalent to the Python expression ``len(o)``.
327
Georg Brandl54a3faa2008-01-20 09:30:57 +0000328
Georg Brandl60203b42010-10-06 10:11:56 +0000329.. c:function:: PyObject* PyObject_GetItem(PyObject *o, PyObject *key)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000330
331 Return element of *o* corresponding to the object *key* or *NULL* on failure.
332 This is the equivalent of the Python expression ``o[key]``.
333
334
Georg Brandl60203b42010-10-06 10:11:56 +0000335.. c:function:: int PyObject_SetItem(PyObject *o, PyObject *key, PyObject *v)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000336
337 Map the object *key* to the value *v*. Returns ``-1`` on failure. This is the
338 equivalent of the Python statement ``o[key] = v``.
339
340
Georg Brandl60203b42010-10-06 10:11:56 +0000341.. c:function:: int PyObject_DelItem(PyObject *o, PyObject *key)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000342
343 Delete the mapping for *key* from *o*. Returns ``-1`` on failure. This is the
344 equivalent of the Python statement ``del o[key]``.
345
346
Georg Brandl60203b42010-10-06 10:11:56 +0000347.. c:function:: PyObject* PyObject_Dir(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000348
349 This is equivalent to the Python expression ``dir(o)``, returning a (possibly
350 empty) list of strings appropriate for the object argument, or *NULL* if there
351 was an error. If the argument is *NULL*, this is like the Python ``dir()``,
352 returning the names of the current locals; in this case, if no execution frame
Georg Brandl60203b42010-10-06 10:11:56 +0000353 is active then *NULL* is returned but :c:func:`PyErr_Occurred` will return false.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000354
355
Georg Brandl60203b42010-10-06 10:11:56 +0000356.. c:function:: PyObject* PyObject_GetIter(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000357
358 This is equivalent to the Python expression ``iter(o)``. It returns a new
359 iterator for the object argument, or the object itself if the object is already
360 an iterator. Raises :exc:`TypeError` and returns *NULL* if the object cannot be
361 iterated.