blob: 295a1290da7a3c51b6ee149ec6482f147b2d5de7 [file] [log] [blame]
Georg Brandlf6842722008-01-19 22:08:21 +00001.. highlightlang:: c
2
3.. _object:
4
5Object Protocol
6===============
7
8
Sandro Tosi98ed08f2012-01-14 16:42:02 +01009.. c:function:: int PyObject_Print(PyObject *o, FILE *fp, int flags)
Georg Brandlf6842722008-01-19 22:08:21 +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
Sandro Tosi98ed08f2012-01-14 16:42:02 +010017.. c:function:: int PyObject_HasAttr(PyObject *o, PyObject *attr_name)
Georg Brandlf6842722008-01-19 22:08:21 +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
Sandro Tosi98ed08f2012-01-14 16:42:02 +010024.. c:function:: int PyObject_HasAttrString(PyObject *o, const char *attr_name)
Georg Brandlf6842722008-01-19 22:08:21 +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
Sandro Tosi98ed08f2012-01-14 16:42:02 +010031.. c:function:: PyObject* PyObject_GetAttr(PyObject *o, PyObject *attr_name)
Georg Brandlf6842722008-01-19 22:08:21 +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
Sandro Tosi98ed08f2012-01-14 16:42:02 +010038.. c:function:: PyObject* PyObject_GetAttrString(PyObject *o, const char *attr_name)
Georg Brandlf6842722008-01-19 22:08:21 +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
Sandro Tosi98ed08f2012-01-14 16:42:02 +010045.. c:function:: PyObject* PyObject_GenericGetAttr(PyObject *o, PyObject *name)
Georg Brandlaa118102009-03-31 17:13:06 +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
Serhiy Storchaka99a196f2013-10-09 13:25:21 +030050 :attr:`~object.__dict__` (if present). As outlined in :ref:`descriptors`,
51 data descriptors take preference over instance attributes, while non-data
Georg Brandlaa118102009-03-31 17:13:06 +000052 descriptors don't. Otherwise, an :exc:`AttributeError` is raised.
53
54
Sandro Tosi98ed08f2012-01-14 16:42:02 +010055.. c:function:: int PyObject_SetAttr(PyObject *o, PyObject *attr_name, PyObject *v)
Georg Brandlf6842722008-01-19 22:08:21 +000056
57 Set the value of the attribute named *attr_name*, for object *o*, to the value
Martin Pantered826042016-11-30 10:32:40 +000058 *v*. Raise an exception and return ``-1`` on failure;
59 return ``0`` on success. This is the equivalent of the Python statement
Georg Brandlf6842722008-01-19 22:08:21 +000060 ``o.attr_name = v``.
61
Martin Pantered826042016-11-30 10:32:40 +000062 If *v* is *NULL*, the attribute is deleted, however this feature is
63 deprecated in favour of using :c:func:`PyObject_DelAttr`.
64
Georg Brandlf6842722008-01-19 22:08:21 +000065
Sandro Tosi98ed08f2012-01-14 16:42:02 +010066.. c:function:: int PyObject_SetAttrString(PyObject *o, const char *attr_name, PyObject *v)
Georg Brandlf6842722008-01-19 22:08:21 +000067
68 Set the value of the attribute named *attr_name*, for object *o*, to the value
Martin Pantered826042016-11-30 10:32:40 +000069 *v*. Raise an exception and return ``-1`` on failure;
70 return ``0`` on success. This is the equivalent of the Python statement
Georg Brandlf6842722008-01-19 22:08:21 +000071 ``o.attr_name = v``.
72
Martin Pantered826042016-11-30 10:32:40 +000073 If *v* is *NULL*, the attribute is deleted, however this feature is
74 deprecated in favour of using :c:func:`PyObject_DelAttrString`.
75
Georg Brandlf6842722008-01-19 22:08:21 +000076
Sandro Tosi98ed08f2012-01-14 16:42:02 +010077.. c:function:: int PyObject_GenericSetAttr(PyObject *o, PyObject *name, PyObject *value)
Georg Brandlaa118102009-03-31 17:13:06 +000078
Martin Pantered826042016-11-30 10:32:40 +000079 Generic attribute setter and deleter function that is meant
80 to be put into a type object's :c:member:`~PyTypeObject.tp_setattro`
81 slot. It looks for a data descriptor in the
Georg Brandlaa118102009-03-31 17:13:06 +000082 dictionary of classes in the object's MRO, and if found it takes preference
Martin Pantered826042016-11-30 10:32:40 +000083 over setting or deleting the attribute in the instance dictionary. Otherwise, the
84 attribute is set or deleted in the object's :attr:`~object.__dict__` (if present).
85 On success, ``0`` is returned, otherwise an :exc:`AttributeError`
86 is raised and ``-1`` is returned.
Georg Brandlaa118102009-03-31 17:13:06 +000087
88
Sandro Tosi98ed08f2012-01-14 16:42:02 +010089.. c:function:: int PyObject_DelAttr(PyObject *o, PyObject *attr_name)
Georg Brandlf6842722008-01-19 22:08:21 +000090
91 Delete attribute named *attr_name*, for object *o*. Returns ``-1`` on failure.
92 This is the equivalent of the Python statement ``del o.attr_name``.
93
94
Sandro Tosi98ed08f2012-01-14 16:42:02 +010095.. c:function:: int PyObject_DelAttrString(PyObject *o, const char *attr_name)
Georg Brandlf6842722008-01-19 22:08:21 +000096
97 Delete attribute named *attr_name*, for object *o*. Returns ``-1`` on failure.
98 This is the equivalent of the Python statement ``del o.attr_name``.
99
100
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100101.. c:function:: PyObject* PyObject_RichCompare(PyObject *o1, PyObject *o2, int opid)
Georg Brandlf6842722008-01-19 22:08:21 +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. This is the equivalent of
107 the Python expression ``o1 op o2``, where ``op`` is the operator corresponding
108 to *opid*. Returns the value of the comparison on success, or *NULL* on failure.
109
110
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100111.. c:function:: int PyObject_RichCompareBool(PyObject *o1, PyObject *o2, int opid)
Georg Brandlf6842722008-01-19 22:08:21 +0000112
113 Compare the values of *o1* and *o2* using the operation specified by *opid*,
114 which must be one of :const:`Py_LT`, :const:`Py_LE`, :const:`Py_EQ`,
115 :const:`Py_NE`, :const:`Py_GT`, or :const:`Py_GE`, corresponding to ``<``,
116 ``<=``, ``==``, ``!=``, ``>``, or ``>=`` respectively. Returns ``-1`` on error,
117 ``0`` if the result is false, ``1`` otherwise. This is the equivalent of the
118 Python expression ``o1 op o2``, where ``op`` is the operator corresponding to
119 *opid*.
120
Eli Bendersky11d11712011-04-30 08:51:55 +0300121.. note::
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100122 If *o1* and *o2* are the same object, :c:func:`PyObject_RichCompareBool`
Eli Bendersky11d11712011-04-30 08:51:55 +0300123 will always return ``1`` for :const:`Py_EQ` and ``0`` for :const:`Py_NE`.
Georg Brandlf6842722008-01-19 22:08:21 +0000124
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100125.. c:function:: int PyObject_Cmp(PyObject *o1, PyObject *o2, int *result)
Georg Brandlf6842722008-01-19 22:08:21 +0000126
127 .. index:: builtin: cmp
128
129 Compare the values of *o1* and *o2* using a routine provided by *o1*, if one
130 exists, otherwise with a routine provided by *o2*. The result of the comparison
131 is returned in *result*. Returns ``-1`` on failure. This is the equivalent of
132 the Python statement ``result = cmp(o1, o2)``.
133
134
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100135.. c:function:: int PyObject_Compare(PyObject *o1, PyObject *o2)
Georg Brandlf6842722008-01-19 22:08:21 +0000136
137 .. index:: builtin: cmp
138
139 Compare the values of *o1* and *o2* using a routine provided by *o1*, if one
140 exists, otherwise with a routine provided by *o2*. Returns the result of the
141 comparison on success. On error, the value returned is undefined; use
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100142 :c:func:`PyErr_Occurred` to detect an error. This is equivalent to the Python
Georg Brandlf6842722008-01-19 22:08:21 +0000143 expression ``cmp(o1, o2)``.
144
145
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100146.. c:function:: PyObject* PyObject_Repr(PyObject *o)
Georg Brandlf6842722008-01-19 22:08:21 +0000147
148 .. index:: builtin: repr
149
150 Compute a string representation of object *o*. Returns the string
151 representation on success, *NULL* on failure. This is the equivalent of the
152 Python expression ``repr(o)``. Called by the :func:`repr` built-in function and
153 by reverse quotes.
154
155
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100156.. c:function:: PyObject* PyObject_Str(PyObject *o)
Georg Brandlf6842722008-01-19 22:08:21 +0000157
158 .. index:: builtin: str
159
160 Compute a string representation of object *o*. Returns the string
161 representation on success, *NULL* on failure. This is the equivalent of the
162 Python expression ``str(o)``. Called by the :func:`str` built-in function and
163 by the :keyword:`print` statement.
164
165
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100166.. c:function:: PyObject* PyObject_Bytes(PyObject *o)
Benjamin Peterson14cb6bc2008-08-26 17:08:40 +0000167
168 .. index:: builtin: bytes
169
Serhiy Storchaka9a118f12016-04-17 09:37:36 +0300170 Compute a bytes representation of object *o*. In 2.x, this is just an alias
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100171 for :c:func:`PyObject_Str`.
Benjamin Peterson14cb6bc2008-08-26 17:08:40 +0000172
173
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100174.. c:function:: PyObject* PyObject_Unicode(PyObject *o)
Georg Brandlf6842722008-01-19 22:08:21 +0000175
176 .. index:: builtin: unicode
177
178 Compute a Unicode string representation of object *o*. Returns the Unicode
179 string representation on success, *NULL* on failure. This is the equivalent of
180 the Python expression ``unicode(o)``. Called by the :func:`unicode` built-in
181 function.
182
183
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100184.. c:function:: int PyObject_IsInstance(PyObject *inst, PyObject *cls)
Georg Brandlf6842722008-01-19 22:08:21 +0000185
186 Returns ``1`` if *inst* is an instance of the class *cls* or a subclass of
187 *cls*, or ``0`` if not. On error, returns ``-1`` and sets an exception. If
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100188 *cls* is a type object rather than a class object, :c:func:`PyObject_IsInstance`
Georg Brandlf6842722008-01-19 22:08:21 +0000189 returns ``1`` if *inst* is of type *cls*. If *cls* is a tuple, the check will
190 be done against every entry in *cls*. The result will be ``1`` when at least one
191 of the checks returns ``1``, otherwise it will be ``0``. If *inst* is not a
192 class instance and *cls* is neither a type object, nor a class object, nor a
Serhiy Storchaka99a196f2013-10-09 13:25:21 +0300193 tuple, *inst* must have a :attr:`~instance.__class__` attribute --- the
194 class relationship of the value of that attribute with *cls* will be used
195 to determine the result of this function.
Georg Brandlf6842722008-01-19 22:08:21 +0000196
197 .. versionadded:: 2.1
198
199 .. versionchanged:: 2.2
200 Support for a tuple as the second argument added.
201
202Subclass determination is done in a fairly straightforward way, but includes a
203wrinkle that implementors of extensions to the class system may want to be aware
204of. If :class:`A` and :class:`B` are class objects, :class:`B` is a subclass of
205:class:`A` if it inherits from :class:`A` either directly or indirectly. If
206either is not a class object, a more general mechanism is used to determine the
207class relationship of the two objects. When testing if *B* is a subclass of
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100208*A*, if *A* is *B*, :c:func:`PyObject_IsSubclass` returns true. If *A* and *B*
Serhiy Storchaka99a196f2013-10-09 13:25:21 +0300209are different objects, *B*'s :attr:`~class.__bases__` attribute is searched in
210a depth-first fashion for *A* --- the presence of the :attr:`~class.__bases__`
211attribute is considered sufficient for this determination.
Georg Brandlf6842722008-01-19 22:08:21 +0000212
213
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100214.. c:function:: int PyObject_IsSubclass(PyObject *derived, PyObject *cls)
Georg Brandlf6842722008-01-19 22:08:21 +0000215
216 Returns ``1`` if the class *derived* is identical to or derived from the class
217 *cls*, otherwise returns ``0``. In case of an error, returns ``-1``. If *cls*
218 is a tuple, the check will be done against every entry in *cls*. The result will
219 be ``1`` when at least one of the checks returns ``1``, otherwise it will be
220 ``0``. If either *derived* or *cls* is not an actual class object (or tuple),
221 this function uses the generic algorithm described above.
222
223 .. versionadded:: 2.1
224
225 .. versionchanged:: 2.3
226 Older versions of Python did not support a tuple as the second argument.
227
228
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100229.. c:function:: int PyCallable_Check(PyObject *o)
Georg Brandlf6842722008-01-19 22:08:21 +0000230
231 Determine if the object *o* is callable. Return ``1`` if the object is callable
232 and ``0`` otherwise. This function always succeeds.
233
234
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100235.. c:function:: PyObject* PyObject_Call(PyObject *callable_object, PyObject *args, PyObject *kw)
Georg Brandlf6842722008-01-19 22:08:21 +0000236
237 .. index:: builtin: apply
238
239 Call a callable Python object *callable_object*, with arguments given by the
240 tuple *args*, and named arguments given by the dictionary *kw*. If no named
241 arguments are needed, *kw* may be *NULL*. *args* must not be *NULL*, use an
242 empty tuple if no arguments are needed. Returns the result of the call on
243 success, or *NULL* on failure. This is the equivalent of the Python expression
244 ``apply(callable_object, args, kw)`` or ``callable_object(*args, **kw)``.
245
246 .. versionadded:: 2.2
247
248
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100249.. c:function:: PyObject* PyObject_CallObject(PyObject *callable_object, PyObject *args)
Georg Brandlf6842722008-01-19 22:08:21 +0000250
251 .. index:: builtin: apply
252
253 Call a callable Python object *callable_object*, with arguments given by the
254 tuple *args*. If no arguments are needed, then *args* may be *NULL*. Returns
255 the result of the call on success, or *NULL* on failure. This is the equivalent
256 of the Python expression ``apply(callable_object, args)`` or
257 ``callable_object(*args)``.
258
259
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100260.. c:function:: PyObject* PyObject_CallFunction(PyObject *callable, char *format, ...)
Georg Brandlf6842722008-01-19 22:08:21 +0000261
262 .. index:: builtin: apply
263
264 Call a callable Python object *callable*, with a variable number of C arguments.
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100265 The C arguments are described using a :c:func:`Py_BuildValue` style format
Georg Brandlf6842722008-01-19 22:08:21 +0000266 string. The format may be *NULL*, indicating that no arguments are provided.
267 Returns the result of the call on success, or *NULL* on failure. This is the
268 equivalent of the Python expression ``apply(callable, args)`` or
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100269 ``callable(*args)``. Note that if you only pass :c:type:`PyObject \*` args,
270 :c:func:`PyObject_CallFunctionObjArgs` is a faster alternative.
Georg Brandlf6842722008-01-19 22:08:21 +0000271
272
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100273.. c:function:: PyObject* PyObject_CallMethod(PyObject *o, char *method, char *format, ...)
Georg Brandlf6842722008-01-19 22:08:21 +0000274
275 Call the method named *method* of object *o* with a variable number of C
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100276 arguments. The C arguments are described by a :c:func:`Py_BuildValue` format
Georg Brandlf6842722008-01-19 22:08:21 +0000277 string that should produce a tuple. The format may be *NULL*, indicating that
278 no arguments are provided. Returns the result of the call on success, or *NULL*
279 on failure. This is the equivalent of the Python expression ``o.method(args)``.
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100280 Note that if you only pass :c:type:`PyObject \*` args,
281 :c:func:`PyObject_CallMethodObjArgs` is a faster alternative.
Georg Brandlf6842722008-01-19 22:08:21 +0000282
283
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100284.. c:function:: PyObject* PyObject_CallFunctionObjArgs(PyObject *callable, ..., NULL)
Georg Brandlf6842722008-01-19 22:08:21 +0000285
286 Call a callable Python object *callable*, with a variable number of
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100287 :c:type:`PyObject\*` arguments. The arguments are provided as a variable number
Georg Brandlf6842722008-01-19 22:08:21 +0000288 of parameters followed by *NULL*. Returns the result of the call on success, or
289 *NULL* on failure.
290
291 .. versionadded:: 2.2
292
293
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100294.. c:function:: PyObject* PyObject_CallMethodObjArgs(PyObject *o, PyObject *name, ..., NULL)
Georg Brandlf6842722008-01-19 22:08:21 +0000295
296 Calls a method of the object *o*, where the name of the method is given as a
297 Python string object in *name*. It is called with a variable number of
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100298 :c:type:`PyObject\*` arguments. The arguments are provided as a variable number
Georg Brandlf6842722008-01-19 22:08:21 +0000299 of parameters followed by *NULL*. Returns the result of the call on success, or
300 *NULL* on failure.
301
302 .. versionadded:: 2.2
303
304
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100305.. c:function:: long PyObject_Hash(PyObject *o)
Georg Brandlf6842722008-01-19 22:08:21 +0000306
307 .. index:: builtin: hash
308
309 Compute and return the hash value of an object *o*. On failure, return ``-1``.
310 This is the equivalent of the Python expression ``hash(o)``.
311
312
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100313.. c:function:: long PyObject_HashNotImplemented(PyObject *o)
Nick Coghlan6e8fef02008-08-18 13:14:22 +0000314
Andrew M. Kuchling17ff29d2008-09-30 13:00:34 +0000315 Set a :exc:`TypeError` indicating that ``type(o)`` is not hashable and return ``-1``.
Nick Coghlan6e8fef02008-08-18 13:14:22 +0000316 This function receives special treatment when stored in a ``tp_hash`` slot,
Nick Coghlan8e439a12008-08-18 13:32:19 +0000317 allowing a type to explicitly indicate to the interpreter that it is not
Nick Coghlan6e8fef02008-08-18 13:14:22 +0000318 hashable.
319
320 .. versionadded:: 2.6
321
322
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100323.. c:function:: int PyObject_IsTrue(PyObject *o)
Georg Brandlf6842722008-01-19 22:08:21 +0000324
325 Returns ``1`` if the object *o* is considered to be true, and ``0`` otherwise.
326 This is equivalent to the Python expression ``not not o``. On failure, return
327 ``-1``.
328
329
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100330.. c:function:: int PyObject_Not(PyObject *o)
Georg Brandlf6842722008-01-19 22:08:21 +0000331
332 Returns ``0`` if the object *o* is considered to be true, and ``1`` otherwise.
333 This is equivalent to the Python expression ``not o``. On failure, return
334 ``-1``.
335
336
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100337.. c:function:: PyObject* PyObject_Type(PyObject *o)
Georg Brandlf6842722008-01-19 22:08:21 +0000338
339 .. index:: builtin: type
340
341 When *o* is non-*NULL*, returns a type object corresponding to the object type
342 of object *o*. On failure, raises :exc:`SystemError` and returns *NULL*. This
343 is equivalent to the Python expression ``type(o)``. This function increments the
344 reference count of the return value. There's really no reason to use this
345 function instead of the common expression ``o->ob_type``, which returns a
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100346 pointer of type :c:type:`PyTypeObject\*`, except when the incremented reference
Georg Brandlf6842722008-01-19 22:08:21 +0000347 count is needed.
348
349
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100350.. c:function:: int PyObject_TypeCheck(PyObject *o, PyTypeObject *type)
Georg Brandlf6842722008-01-19 22:08:21 +0000351
352 Return true if the object *o* is of type *type* or a subtype of *type*. Both
353 parameters must be non-*NULL*.
354
355 .. versionadded:: 2.2
356
357
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100358.. c:function:: Py_ssize_t PyObject_Length(PyObject *o)
Georg Brandlf6842722008-01-19 22:08:21 +0000359 Py_ssize_t PyObject_Size(PyObject *o)
360
361 .. index:: builtin: len
362
363 Return the length of object *o*. If the object *o* provides either the sequence
364 and mapping protocols, the sequence length is returned. On error, ``-1`` is
365 returned. This is the equivalent to the Python expression ``len(o)``.
366
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +0000367 .. versionchanged:: 2.5
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100368 These functions returned an :c:type:`int` type. This might require
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +0000369 changes in your code for properly supporting 64-bit systems.
370
Georg Brandlf6842722008-01-19 22:08:21 +0000371
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100372.. c:function:: PyObject* PyObject_GetItem(PyObject *o, PyObject *key)
Georg Brandlf6842722008-01-19 22:08:21 +0000373
374 Return element of *o* corresponding to the object *key* or *NULL* on failure.
375 This is the equivalent of the Python expression ``o[key]``.
376
377
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100378.. c:function:: int PyObject_SetItem(PyObject *o, PyObject *key, PyObject *v)
Georg Brandlf6842722008-01-19 22:08:21 +0000379
Martin Pantered826042016-11-30 10:32:40 +0000380 Map the object *key* to the value *v*. Raise an exception and
381 return ``-1`` on failure; return ``0`` on success. This is the
Georg Brandlf6842722008-01-19 22:08:21 +0000382 equivalent of the Python statement ``o[key] = v``.
383
384
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100385.. c:function:: int PyObject_DelItem(PyObject *o, PyObject *key)
Georg Brandlf6842722008-01-19 22:08:21 +0000386
387 Delete the mapping for *key* from *o*. Returns ``-1`` on failure. This is the
388 equivalent of the Python statement ``del o[key]``.
389
390
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100391.. c:function:: int PyObject_AsFileDescriptor(PyObject *o)
Georg Brandlf6842722008-01-19 22:08:21 +0000392
393 Derives a file descriptor from a Python object. If the object is an integer or
394 long integer, its value is returned. If not, the object's :meth:`fileno` method
395 is called if it exists; the method must return an integer or long integer, which
396 is returned as the file descriptor value. Returns ``-1`` on failure.
397
398
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100399.. c:function:: PyObject* PyObject_Dir(PyObject *o)
Georg Brandlf6842722008-01-19 22:08:21 +0000400
401 This is equivalent to the Python expression ``dir(o)``, returning a (possibly
402 empty) list of strings appropriate for the object argument, or *NULL* if there
403 was an error. If the argument is *NULL*, this is like the Python ``dir()``,
404 returning the names of the current locals; in this case, if no execution frame
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100405 is active then *NULL* is returned but :c:func:`PyErr_Occurred` will return false.
Georg Brandlf6842722008-01-19 22:08:21 +0000406
407
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100408.. c:function:: PyObject* PyObject_GetIter(PyObject *o)
Georg Brandlf6842722008-01-19 22:08:21 +0000409
410 This is equivalent to the Python expression ``iter(o)``. It returns a new
411 iterator for the object argument, or the object itself if the object is already
412 an iterator. Raises :exc:`TypeError` and returns *NULL* if the object cannot be
413 iterated.