Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 1 | .. highlightlang:: c |
| 2 | |
| 3 | .. _object: |
| 4 | |
| 5 | Object Protocol |
| 6 | =============== |
| 7 | |
| 8 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 9 | .. c:function:: int PyObject_Print(PyObject *o, FILE *fp, int flags) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 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 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 17 | .. c:function:: int PyObject_HasAttr(PyObject *o, PyObject *attr_name) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 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 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 24 | .. c:function:: int PyObject_HasAttrString(PyObject *o, const char *attr_name) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 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 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 31 | .. c:function:: PyObject* PyObject_GetAttr(PyObject *o, PyObject *attr_name) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 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 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 38 | .. c:function:: PyObject* PyObject_GetAttrString(PyObject *o, const char *attr_name) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 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 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 45 | .. c:function:: PyObject* PyObject_GenericGetAttr(PyObject *o, PyObject *name) |
Georg Brandl | aa11810 | 2009-03-31 17:13:06 +0000 | [diff] [blame] | 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 |
Serhiy Storchaka | 99a196f | 2013-10-09 13:25:21 +0300 | [diff] [blame] | 50 | :attr:`~object.__dict__` (if present). As outlined in :ref:`descriptors`, |
| 51 | data descriptors take preference over instance attributes, while non-data |
Georg Brandl | aa11810 | 2009-03-31 17:13:06 +0000 | [diff] [blame] | 52 | descriptors don't. Otherwise, an :exc:`AttributeError` is raised. |
| 53 | |
| 54 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 55 | .. c:function:: int PyObject_SetAttr(PyObject *o, PyObject *attr_name, PyObject *v) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 56 | |
| 57 | Set the value of the attribute named *attr_name*, for object *o*, to the value |
Martin Panter | ed82604 | 2016-11-30 10:32:40 +0000 | [diff] [blame] | 58 | *v*. Raise an exception and return ``-1`` on failure; |
| 59 | return ``0`` on success. This is the equivalent of the Python statement |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 60 | ``o.attr_name = v``. |
| 61 | |
Martin Panter | ed82604 | 2016-11-30 10:32:40 +0000 | [diff] [blame] | 62 | If *v* is *NULL*, the attribute is deleted, however this feature is |
| 63 | deprecated in favour of using :c:func:`PyObject_DelAttr`. |
| 64 | |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 65 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 66 | .. c:function:: int PyObject_SetAttrString(PyObject *o, const char *attr_name, PyObject *v) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 67 | |
| 68 | Set the value of the attribute named *attr_name*, for object *o*, to the value |
Martin Panter | ed82604 | 2016-11-30 10:32:40 +0000 | [diff] [blame] | 69 | *v*. Raise an exception and return ``-1`` on failure; |
| 70 | return ``0`` on success. This is the equivalent of the Python statement |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 71 | ``o.attr_name = v``. |
| 72 | |
Martin Panter | ed82604 | 2016-11-30 10:32:40 +0000 | [diff] [blame] | 73 | If *v* is *NULL*, the attribute is deleted, however this feature is |
| 74 | deprecated in favour of using :c:func:`PyObject_DelAttrString`. |
| 75 | |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 76 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 77 | .. c:function:: int PyObject_GenericSetAttr(PyObject *o, PyObject *name, PyObject *value) |
Georg Brandl | aa11810 | 2009-03-31 17:13:06 +0000 | [diff] [blame] | 78 | |
Martin Panter | ed82604 | 2016-11-30 10:32:40 +0000 | [diff] [blame] | 79 | 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 Brandl | aa11810 | 2009-03-31 17:13:06 +0000 | [diff] [blame] | 82 | dictionary of classes in the object's MRO, and if found it takes preference |
Martin Panter | ed82604 | 2016-11-30 10:32:40 +0000 | [diff] [blame] | 83 | 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 Brandl | aa11810 | 2009-03-31 17:13:06 +0000 | [diff] [blame] | 87 | |
| 88 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 89 | .. c:function:: int PyObject_DelAttr(PyObject *o, PyObject *attr_name) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 90 | |
| 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 Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 95 | .. c:function:: int PyObject_DelAttrString(PyObject *o, const char *attr_name) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 96 | |
| 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 Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 101 | .. c:function:: PyObject* PyObject_RichCompare(PyObject *o1, PyObject *o2, int opid) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 102 | |
| 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 Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 111 | .. c:function:: int PyObject_RichCompareBool(PyObject *o1, PyObject *o2, int opid) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 112 | |
| 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 Bendersky | 11d1171 | 2011-04-30 08:51:55 +0300 | [diff] [blame] | 121 | .. note:: |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 122 | If *o1* and *o2* are the same object, :c:func:`PyObject_RichCompareBool` |
Eli Bendersky | 11d1171 | 2011-04-30 08:51:55 +0300 | [diff] [blame] | 123 | will always return ``1`` for :const:`Py_EQ` and ``0`` for :const:`Py_NE`. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 124 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 125 | .. c:function:: int PyObject_Cmp(PyObject *o1, PyObject *o2, int *result) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 126 | |
| 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 Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 135 | .. c:function:: int PyObject_Compare(PyObject *o1, PyObject *o2) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 136 | |
| 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 Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 142 | :c:func:`PyErr_Occurred` to detect an error. This is equivalent to the Python |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 143 | expression ``cmp(o1, o2)``. |
| 144 | |
| 145 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 146 | .. c:function:: PyObject* PyObject_Repr(PyObject *o) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 147 | |
| 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 Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 156 | .. c:function:: PyObject* PyObject_Str(PyObject *o) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 157 | |
| 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 Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 166 | .. c:function:: PyObject* PyObject_Bytes(PyObject *o) |
Benjamin Peterson | 14cb6bc | 2008-08-26 17:08:40 +0000 | [diff] [blame] | 167 | |
| 168 | .. index:: builtin: bytes |
| 169 | |
Serhiy Storchaka | 9a118f1 | 2016-04-17 09:37:36 +0300 | [diff] [blame] | 170 | Compute a bytes representation of object *o*. In 2.x, this is just an alias |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 171 | for :c:func:`PyObject_Str`. |
Benjamin Peterson | 14cb6bc | 2008-08-26 17:08:40 +0000 | [diff] [blame] | 172 | |
| 173 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 174 | .. c:function:: PyObject* PyObject_Unicode(PyObject *o) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 175 | |
| 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 Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 184 | .. c:function:: int PyObject_IsInstance(PyObject *inst, PyObject *cls) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 185 | |
| 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 Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 188 | *cls* is a type object rather than a class object, :c:func:`PyObject_IsInstance` |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 189 | 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 Storchaka | 99a196f | 2013-10-09 13:25:21 +0300 | [diff] [blame] | 193 | 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 Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 196 | |
| 197 | .. versionadded:: 2.1 |
| 198 | |
| 199 | .. versionchanged:: 2.2 |
| 200 | Support for a tuple as the second argument added. |
| 201 | |
| 202 | Subclass determination is done in a fairly straightforward way, but includes a |
| 203 | wrinkle that implementors of extensions to the class system may want to be aware |
| 204 | of. 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 |
| 206 | either is not a class object, a more general mechanism is used to determine the |
| 207 | class relationship of the two objects. When testing if *B* is a subclass of |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 208 | *A*, if *A* is *B*, :c:func:`PyObject_IsSubclass` returns true. If *A* and *B* |
Serhiy Storchaka | 99a196f | 2013-10-09 13:25:21 +0300 | [diff] [blame] | 209 | are different objects, *B*'s :attr:`~class.__bases__` attribute is searched in |
| 210 | a depth-first fashion for *A* --- the presence of the :attr:`~class.__bases__` |
| 211 | attribute is considered sufficient for this determination. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 212 | |
| 213 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 214 | .. c:function:: int PyObject_IsSubclass(PyObject *derived, PyObject *cls) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 215 | |
| 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 Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 229 | .. c:function:: int PyCallable_Check(PyObject *o) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 230 | |
| 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 Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 235 | .. c:function:: PyObject* PyObject_Call(PyObject *callable_object, PyObject *args, PyObject *kw) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 236 | |
| 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 Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 249 | .. c:function:: PyObject* PyObject_CallObject(PyObject *callable_object, PyObject *args) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 250 | |
| 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 Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 260 | .. c:function:: PyObject* PyObject_CallFunction(PyObject *callable, char *format, ...) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 261 | |
| 262 | .. index:: builtin: apply |
| 263 | |
| 264 | Call a callable Python object *callable*, with a variable number of C arguments. |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 265 | The C arguments are described using a :c:func:`Py_BuildValue` style format |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 266 | 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 Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 269 | ``callable(*args)``. Note that if you only pass :c:type:`PyObject \*` args, |
| 270 | :c:func:`PyObject_CallFunctionObjArgs` is a faster alternative. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 271 | |
| 272 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 273 | .. c:function:: PyObject* PyObject_CallMethod(PyObject *o, char *method, char *format, ...) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 274 | |
| 275 | Call the method named *method* of object *o* with a variable number of C |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 276 | arguments. The C arguments are described by a :c:func:`Py_BuildValue` format |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 277 | 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 Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 280 | Note that if you only pass :c:type:`PyObject \*` args, |
| 281 | :c:func:`PyObject_CallMethodObjArgs` is a faster alternative. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 282 | |
| 283 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 284 | .. c:function:: PyObject* PyObject_CallFunctionObjArgs(PyObject *callable, ..., NULL) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 285 | |
| 286 | Call a callable Python object *callable*, with a variable number of |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 287 | :c:type:`PyObject\*` arguments. The arguments are provided as a variable number |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 288 | 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 Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 294 | .. c:function:: PyObject* PyObject_CallMethodObjArgs(PyObject *o, PyObject *name, ..., NULL) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 295 | |
| 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 Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 298 | :c:type:`PyObject\*` arguments. The arguments are provided as a variable number |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 299 | 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 Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 305 | .. c:function:: long PyObject_Hash(PyObject *o) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 306 | |
| 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 Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 313 | .. c:function:: long PyObject_HashNotImplemented(PyObject *o) |
Nick Coghlan | 6e8fef0 | 2008-08-18 13:14:22 +0000 | [diff] [blame] | 314 | |
Andrew M. Kuchling | 17ff29d | 2008-09-30 13:00:34 +0000 | [diff] [blame] | 315 | Set a :exc:`TypeError` indicating that ``type(o)`` is not hashable and return ``-1``. |
Nick Coghlan | 6e8fef0 | 2008-08-18 13:14:22 +0000 | [diff] [blame] | 316 | This function receives special treatment when stored in a ``tp_hash`` slot, |
Nick Coghlan | 8e439a1 | 2008-08-18 13:32:19 +0000 | [diff] [blame] | 317 | allowing a type to explicitly indicate to the interpreter that it is not |
Nick Coghlan | 6e8fef0 | 2008-08-18 13:14:22 +0000 | [diff] [blame] | 318 | hashable. |
| 319 | |
| 320 | .. versionadded:: 2.6 |
| 321 | |
| 322 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 323 | .. c:function:: int PyObject_IsTrue(PyObject *o) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 324 | |
| 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 Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 330 | .. c:function:: int PyObject_Not(PyObject *o) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 331 | |
| 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 Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 337 | .. c:function:: PyObject* PyObject_Type(PyObject *o) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 338 | |
| 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 Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 346 | pointer of type :c:type:`PyTypeObject\*`, except when the incremented reference |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 347 | count is needed. |
| 348 | |
| 349 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 350 | .. c:function:: int PyObject_TypeCheck(PyObject *o, PyTypeObject *type) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 351 | |
| 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 Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 358 | .. c:function:: Py_ssize_t PyObject_Length(PyObject *o) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 359 | 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 Werven | 089c5cd | 2009-04-25 17:59:03 +0000 | [diff] [blame] | 367 | .. versionchanged:: 2.5 |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 368 | These functions returned an :c:type:`int` type. This might require |
Jeroen Ruigrok van der Werven | 089c5cd | 2009-04-25 17:59:03 +0000 | [diff] [blame] | 369 | changes in your code for properly supporting 64-bit systems. |
| 370 | |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 371 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 372 | .. c:function:: PyObject* PyObject_GetItem(PyObject *o, PyObject *key) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 373 | |
| 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 Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 378 | .. c:function:: int PyObject_SetItem(PyObject *o, PyObject *key, PyObject *v) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 379 | |
Martin Panter | ed82604 | 2016-11-30 10:32:40 +0000 | [diff] [blame] | 380 | 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 Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 382 | equivalent of the Python statement ``o[key] = v``. |
| 383 | |
| 384 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 385 | .. c:function:: int PyObject_DelItem(PyObject *o, PyObject *key) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 386 | |
| 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 Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 391 | .. c:function:: int PyObject_AsFileDescriptor(PyObject *o) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 392 | |
| 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 Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 399 | .. c:function:: PyObject* PyObject_Dir(PyObject *o) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 400 | |
| 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 Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 405 | is active then *NULL* is returned but :c:func:`PyErr_Occurred` will return false. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 406 | |
| 407 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 408 | .. c:function:: PyObject* PyObject_GetIter(PyObject *o) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 409 | |
| 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. |