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 | |
| 9 | .. cfunction:: int PyObject_Print(PyObject *o, FILE *fp, int flags) |
| 10 | |
| 11 | Print an object *o*, on file *fp*. Returns ``-1`` on error. The flags argument |
| 12 | is used to enable certain printing options. The only option currently supported |
| 13 | is :const:`Py_PRINT_RAW`; if given, the :func:`str` of the object is written |
| 14 | instead of the :func:`repr`. |
| 15 | |
| 16 | |
| 17 | .. cfunction:: int PyObject_HasAttr(PyObject *o, PyObject *attr_name) |
| 18 | |
| 19 | Returns ``1`` if *o* has the attribute *attr_name*, and ``0`` otherwise. This |
| 20 | is equivalent to the Python expression ``hasattr(o, attr_name)``. This function |
| 21 | always succeeds. |
| 22 | |
| 23 | |
| 24 | .. cfunction:: int PyObject_HasAttrString(PyObject *o, const char *attr_name) |
| 25 | |
| 26 | Returns ``1`` if *o* has the attribute *attr_name*, and ``0`` otherwise. This |
| 27 | is equivalent to the Python expression ``hasattr(o, attr_name)``. This function |
| 28 | always succeeds. |
| 29 | |
| 30 | |
| 31 | .. cfunction:: PyObject* PyObject_GetAttr(PyObject *o, PyObject *attr_name) |
| 32 | |
| 33 | Retrieve an attribute named *attr_name* from object *o*. Returns the attribute |
| 34 | value on success, or *NULL* on failure. This is the equivalent of the Python |
| 35 | expression ``o.attr_name``. |
| 36 | |
| 37 | |
| 38 | .. cfunction:: PyObject* PyObject_GetAttrString(PyObject *o, const char *attr_name) |
| 39 | |
| 40 | Retrieve an attribute named *attr_name* from object *o*. Returns the attribute |
| 41 | value on success, or *NULL* on failure. This is the equivalent of the Python |
| 42 | expression ``o.attr_name``. |
| 43 | |
| 44 | |
| 45 | .. cfunction:: int PyObject_SetAttr(PyObject *o, PyObject *attr_name, PyObject *v) |
| 46 | |
| 47 | Set the value of the attribute named *attr_name*, for object *o*, to the value |
| 48 | *v*. Returns ``-1`` on failure. This is the equivalent of the Python statement |
| 49 | ``o.attr_name = v``. |
| 50 | |
| 51 | |
| 52 | .. cfunction:: int PyObject_SetAttrString(PyObject *o, const char *attr_name, PyObject *v) |
| 53 | |
| 54 | Set the value of the attribute named *attr_name*, for object *o*, to the value |
| 55 | *v*. Returns ``-1`` on failure. This is the equivalent of the Python statement |
| 56 | ``o.attr_name = v``. |
| 57 | |
| 58 | |
| 59 | .. cfunction:: int PyObject_DelAttr(PyObject *o, PyObject *attr_name) |
| 60 | |
| 61 | Delete attribute named *attr_name*, for object *o*. Returns ``-1`` on failure. |
| 62 | This is the equivalent of the Python statement ``del o.attr_name``. |
| 63 | |
| 64 | |
| 65 | .. cfunction:: int PyObject_DelAttrString(PyObject *o, const char *attr_name) |
| 66 | |
| 67 | Delete attribute named *attr_name*, for object *o*. Returns ``-1`` on failure. |
| 68 | This is the equivalent of the Python statement ``del o.attr_name``. |
| 69 | |
| 70 | |
| 71 | .. cfunction:: PyObject* PyObject_RichCompare(PyObject *o1, PyObject *o2, int opid) |
| 72 | |
| 73 | Compare the values of *o1* and *o2* using the operation specified by *opid*, |
| 74 | which must be one of :const:`Py_LT`, :const:`Py_LE`, :const:`Py_EQ`, |
| 75 | :const:`Py_NE`, :const:`Py_GT`, or :const:`Py_GE`, corresponding to ``<``, |
| 76 | ``<=``, ``==``, ``!=``, ``>``, or ``>=`` respectively. This is the equivalent of |
| 77 | the Python expression ``o1 op o2``, where ``op`` is the operator corresponding |
| 78 | to *opid*. Returns the value of the comparison on success, or *NULL* on failure. |
| 79 | |
| 80 | |
| 81 | .. cfunction:: int PyObject_RichCompareBool(PyObject *o1, PyObject *o2, int opid) |
| 82 | |
| 83 | Compare the values of *o1* and *o2* using the operation specified by *opid*, |
| 84 | which must be one of :const:`Py_LT`, :const:`Py_LE`, :const:`Py_EQ`, |
| 85 | :const:`Py_NE`, :const:`Py_GT`, or :const:`Py_GE`, corresponding to ``<``, |
| 86 | ``<=``, ``==``, ``!=``, ``>``, or ``>=`` respectively. Returns ``-1`` on error, |
| 87 | ``0`` if the result is false, ``1`` otherwise. This is the equivalent of the |
| 88 | Python expression ``o1 op o2``, where ``op`` is the operator corresponding to |
| 89 | *opid*. |
| 90 | |
| 91 | |
| 92 | .. cfunction:: int PyObject_Cmp(PyObject *o1, PyObject *o2, int *result) |
| 93 | |
| 94 | .. index:: builtin: cmp |
| 95 | |
| 96 | Compare the values of *o1* and *o2* using a routine provided by *o1*, if one |
| 97 | exists, otherwise with a routine provided by *o2*. The result of the comparison |
| 98 | is returned in *result*. Returns ``-1`` on failure. This is the equivalent of |
| 99 | the Python statement ``result = cmp(o1, o2)``. |
| 100 | |
| 101 | |
| 102 | .. cfunction:: int PyObject_Compare(PyObject *o1, PyObject *o2) |
| 103 | |
| 104 | .. index:: builtin: cmp |
| 105 | |
| 106 | Compare the values of *o1* and *o2* using a routine provided by *o1*, if one |
| 107 | exists, otherwise with a routine provided by *o2*. Returns the result of the |
| 108 | comparison on success. On error, the value returned is undefined; use |
| 109 | :cfunc:`PyErr_Occurred` to detect an error. This is equivalent to the Python |
| 110 | expression ``cmp(o1, o2)``. |
| 111 | |
| 112 | |
| 113 | .. cfunction:: PyObject* PyObject_Repr(PyObject *o) |
| 114 | |
| 115 | .. index:: builtin: repr |
| 116 | |
| 117 | Compute a string representation of object *o*. Returns the string |
| 118 | representation on success, *NULL* on failure. This is the equivalent of the |
| 119 | Python expression ``repr(o)``. Called by the :func:`repr` built-in function and |
| 120 | by reverse quotes. |
| 121 | |
| 122 | |
| 123 | .. cfunction:: PyObject* PyObject_Str(PyObject *o) |
| 124 | |
| 125 | .. index:: builtin: str |
| 126 | |
| 127 | Compute a string representation of object *o*. Returns the string |
| 128 | representation on success, *NULL* on failure. This is the equivalent of the |
| 129 | Python expression ``str(o)``. Called by the :func:`str` built-in function and |
| 130 | by the :keyword:`print` statement. |
| 131 | |
| 132 | |
| 133 | .. cfunction:: PyObject* PyObject_Unicode(PyObject *o) |
| 134 | |
| 135 | .. index:: builtin: unicode |
| 136 | |
| 137 | Compute a Unicode string representation of object *o*. Returns the Unicode |
| 138 | string representation on success, *NULL* on failure. This is the equivalent of |
| 139 | the Python expression ``unicode(o)``. Called by the :func:`unicode` built-in |
| 140 | function. |
| 141 | |
| 142 | |
| 143 | .. cfunction:: int PyObject_IsInstance(PyObject *inst, PyObject *cls) |
| 144 | |
| 145 | Returns ``1`` if *inst* is an instance of the class *cls* or a subclass of |
| 146 | *cls*, or ``0`` if not. On error, returns ``-1`` and sets an exception. If |
| 147 | *cls* is a type object rather than a class object, :cfunc:`PyObject_IsInstance` |
| 148 | returns ``1`` if *inst* is of type *cls*. If *cls* is a tuple, the check will |
| 149 | be done against every entry in *cls*. The result will be ``1`` when at least one |
| 150 | of the checks returns ``1``, otherwise it will be ``0``. If *inst* is not a |
| 151 | class instance and *cls* is neither a type object, nor a class object, nor a |
| 152 | tuple, *inst* must have a :attr:`__class__` attribute --- the class relationship |
| 153 | of the value of that attribute with *cls* will be used to determine the result |
| 154 | of this function. |
| 155 | |
| 156 | .. versionadded:: 2.1 |
| 157 | |
| 158 | .. versionchanged:: 2.2 |
| 159 | Support for a tuple as the second argument added. |
| 160 | |
| 161 | Subclass determination is done in a fairly straightforward way, but includes a |
| 162 | wrinkle that implementors of extensions to the class system may want to be aware |
| 163 | of. If :class:`A` and :class:`B` are class objects, :class:`B` is a subclass of |
| 164 | :class:`A` if it inherits from :class:`A` either directly or indirectly. If |
| 165 | either is not a class object, a more general mechanism is used to determine the |
| 166 | class relationship of the two objects. When testing if *B* is a subclass of |
| 167 | *A*, if *A* is *B*, :cfunc:`PyObject_IsSubclass` returns true. If *A* and *B* |
| 168 | are different objects, *B*'s :attr:`__bases__` attribute is searched in a |
| 169 | depth-first fashion for *A* --- the presence of the :attr:`__bases__` attribute |
| 170 | is considered sufficient for this determination. |
| 171 | |
| 172 | |
| 173 | .. cfunction:: int PyObject_IsSubclass(PyObject *derived, PyObject *cls) |
| 174 | |
| 175 | Returns ``1`` if the class *derived* is identical to or derived from the class |
| 176 | *cls*, otherwise returns ``0``. In case of an error, returns ``-1``. If *cls* |
| 177 | is a tuple, the check will be done against every entry in *cls*. The result will |
| 178 | be ``1`` when at least one of the checks returns ``1``, otherwise it will be |
| 179 | ``0``. If either *derived* or *cls* is not an actual class object (or tuple), |
| 180 | this function uses the generic algorithm described above. |
| 181 | |
| 182 | .. versionadded:: 2.1 |
| 183 | |
| 184 | .. versionchanged:: 2.3 |
| 185 | Older versions of Python did not support a tuple as the second argument. |
| 186 | |
| 187 | |
| 188 | .. cfunction:: int PyCallable_Check(PyObject *o) |
| 189 | |
| 190 | Determine if the object *o* is callable. Return ``1`` if the object is callable |
| 191 | and ``0`` otherwise. This function always succeeds. |
| 192 | |
| 193 | |
| 194 | .. cfunction:: PyObject* PyObject_Call(PyObject *callable_object, PyObject *args, PyObject *kw) |
| 195 | |
| 196 | .. index:: builtin: apply |
| 197 | |
| 198 | Call a callable Python object *callable_object*, with arguments given by the |
| 199 | tuple *args*, and named arguments given by the dictionary *kw*. If no named |
| 200 | arguments are needed, *kw* may be *NULL*. *args* must not be *NULL*, use an |
| 201 | empty tuple if no arguments are needed. Returns the result of the call on |
| 202 | success, or *NULL* on failure. This is the equivalent of the Python expression |
| 203 | ``apply(callable_object, args, kw)`` or ``callable_object(*args, **kw)``. |
| 204 | |
| 205 | .. versionadded:: 2.2 |
| 206 | |
| 207 | |
| 208 | .. cfunction:: PyObject* PyObject_CallObject(PyObject *callable_object, PyObject *args) |
| 209 | |
| 210 | .. index:: builtin: apply |
| 211 | |
| 212 | Call a callable Python object *callable_object*, with arguments given by the |
| 213 | tuple *args*. If no arguments are needed, then *args* may be *NULL*. Returns |
| 214 | the result of the call on success, or *NULL* on failure. This is the equivalent |
| 215 | of the Python expression ``apply(callable_object, args)`` or |
| 216 | ``callable_object(*args)``. |
| 217 | |
| 218 | |
| 219 | .. cfunction:: PyObject* PyObject_CallFunction(PyObject *callable, char *format, ...) |
| 220 | |
| 221 | .. index:: builtin: apply |
| 222 | |
| 223 | Call a callable Python object *callable*, with a variable number of C arguments. |
| 224 | The C arguments are described using a :cfunc:`Py_BuildValue` style format |
| 225 | string. The format may be *NULL*, indicating that no arguments are provided. |
| 226 | Returns the result of the call on success, or *NULL* on failure. This is the |
| 227 | equivalent of the Python expression ``apply(callable, args)`` or |
| 228 | ``callable(*args)``. Note that if you only pass :ctype:`PyObject \*` args, |
| 229 | :cfunc:`PyObject_CallFunctionObjArgs` is a faster alternative. |
| 230 | |
| 231 | |
| 232 | .. cfunction:: PyObject* PyObject_CallMethod(PyObject *o, char *method, char *format, ...) |
| 233 | |
| 234 | Call the method named *method* of object *o* with a variable number of C |
| 235 | arguments. The C arguments are described by a :cfunc:`Py_BuildValue` format |
| 236 | string that should produce a tuple. The format may be *NULL*, indicating that |
| 237 | no arguments are provided. Returns the result of the call on success, or *NULL* |
| 238 | on failure. This is the equivalent of the Python expression ``o.method(args)``. |
| 239 | Note that if you only pass :ctype:`PyObject \*` args, |
| 240 | :cfunc:`PyObject_CallMethodObjArgs` is a faster alternative. |
| 241 | |
| 242 | |
| 243 | .. cfunction:: PyObject* PyObject_CallFunctionObjArgs(PyObject *callable, ..., NULL) |
| 244 | |
| 245 | Call a callable Python object *callable*, with a variable number of |
| 246 | :ctype:`PyObject\*` arguments. The arguments are provided as a variable number |
| 247 | of parameters followed by *NULL*. Returns the result of the call on success, or |
| 248 | *NULL* on failure. |
| 249 | |
| 250 | .. versionadded:: 2.2 |
| 251 | |
| 252 | |
| 253 | .. cfunction:: PyObject* PyObject_CallMethodObjArgs(PyObject *o, PyObject *name, ..., NULL) |
| 254 | |
| 255 | Calls a method of the object *o*, where the name of the method is given as a |
| 256 | Python string object in *name*. It is called with a variable number of |
| 257 | :ctype:`PyObject\*` arguments. The arguments are provided as a variable number |
| 258 | of parameters followed by *NULL*. Returns the result of the call on success, or |
| 259 | *NULL* on failure. |
| 260 | |
| 261 | .. versionadded:: 2.2 |
| 262 | |
| 263 | |
| 264 | .. cfunction:: long PyObject_Hash(PyObject *o) |
| 265 | |
| 266 | .. index:: builtin: hash |
| 267 | |
| 268 | Compute and return the hash value of an object *o*. On failure, return ``-1``. |
| 269 | This is the equivalent of the Python expression ``hash(o)``. |
| 270 | |
| 271 | |
| 272 | .. cfunction:: int PyObject_IsTrue(PyObject *o) |
| 273 | |
| 274 | Returns ``1`` if the object *o* is considered to be true, and ``0`` otherwise. |
| 275 | This is equivalent to the Python expression ``not not o``. On failure, return |
| 276 | ``-1``. |
| 277 | |
| 278 | |
| 279 | .. cfunction:: int PyObject_Not(PyObject *o) |
| 280 | |
| 281 | Returns ``0`` if the object *o* is considered to be true, and ``1`` otherwise. |
| 282 | This is equivalent to the Python expression ``not o``. On failure, return |
| 283 | ``-1``. |
| 284 | |
| 285 | |
| 286 | .. cfunction:: PyObject* PyObject_Type(PyObject *o) |
| 287 | |
| 288 | .. index:: builtin: type |
| 289 | |
| 290 | When *o* is non-*NULL*, returns a type object corresponding to the object type |
| 291 | of object *o*. On failure, raises :exc:`SystemError` and returns *NULL*. This |
| 292 | is equivalent to the Python expression ``type(o)``. This function increments the |
| 293 | reference count of the return value. There's really no reason to use this |
| 294 | function instead of the common expression ``o->ob_type``, which returns a |
| 295 | pointer of type :ctype:`PyTypeObject\*`, except when the incremented reference |
| 296 | count is needed. |
| 297 | |
| 298 | |
| 299 | .. cfunction:: int PyObject_TypeCheck(PyObject *o, PyTypeObject *type) |
| 300 | |
| 301 | Return true if the object *o* is of type *type* or a subtype of *type*. Both |
| 302 | parameters must be non-*NULL*. |
| 303 | |
| 304 | .. versionadded:: 2.2 |
| 305 | |
| 306 | |
| 307 | .. cfunction:: Py_ssize_t PyObject_Length(PyObject *o) |
| 308 | Py_ssize_t PyObject_Size(PyObject *o) |
| 309 | |
| 310 | .. index:: builtin: len |
| 311 | |
| 312 | Return the length of object *o*. If the object *o* provides either the sequence |
| 313 | and mapping protocols, the sequence length is returned. On error, ``-1`` is |
| 314 | returned. This is the equivalent to the Python expression ``len(o)``. |
| 315 | |
| 316 | |
| 317 | .. cfunction:: PyObject* PyObject_GetItem(PyObject *o, PyObject *key) |
| 318 | |
| 319 | Return element of *o* corresponding to the object *key* or *NULL* on failure. |
| 320 | This is the equivalent of the Python expression ``o[key]``. |
| 321 | |
| 322 | |
| 323 | .. cfunction:: int PyObject_SetItem(PyObject *o, PyObject *key, PyObject *v) |
| 324 | |
| 325 | Map the object *key* to the value *v*. Returns ``-1`` on failure. This is the |
| 326 | equivalent of the Python statement ``o[key] = v``. |
| 327 | |
| 328 | |
| 329 | .. cfunction:: int PyObject_DelItem(PyObject *o, PyObject *key) |
| 330 | |
| 331 | Delete the mapping for *key* from *o*. Returns ``-1`` on failure. This is the |
| 332 | equivalent of the Python statement ``del o[key]``. |
| 333 | |
| 334 | |
| 335 | .. cfunction:: int PyObject_AsFileDescriptor(PyObject *o) |
| 336 | |
| 337 | Derives a file descriptor from a Python object. If the object is an integer or |
| 338 | long integer, its value is returned. If not, the object's :meth:`fileno` method |
| 339 | is called if it exists; the method must return an integer or long integer, which |
| 340 | is returned as the file descriptor value. Returns ``-1`` on failure. |
| 341 | |
| 342 | |
| 343 | .. cfunction:: PyObject* PyObject_Dir(PyObject *o) |
| 344 | |
| 345 | This is equivalent to the Python expression ``dir(o)``, returning a (possibly |
| 346 | empty) list of strings appropriate for the object argument, or *NULL* if there |
| 347 | was an error. If the argument is *NULL*, this is like the Python ``dir()``, |
| 348 | returning the names of the current locals; in this case, if no execution frame |
| 349 | is active then *NULL* is returned but :cfunc:`PyErr_Occurred` will return false. |
| 350 | |
| 351 | |
| 352 | .. cfunction:: PyObject* PyObject_GetIter(PyObject *o) |
| 353 | |
| 354 | This is equivalent to the Python expression ``iter(o)``. It returns a new |
| 355 | iterator for the object argument, or the object itself if the object is already |
| 356 | an iterator. Raises :exc:`TypeError` and returns *NULL* if the object cannot be |
| 357 | iterated. |