| 1 |
.. highlightlang:: c |
| 2 |
|
| 3 |
.. _object: |
| 4 |
|
| 5 |
Object Protocol |
| 6 |
=============== |
| 7 |
|
| 8 |
|
| 9 |
.. c:function:: 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 |
.. c:function:: 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 |
.. c:function:: 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 |
.. c:function:: 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 |
.. c:function:: 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 |
.. c:function:: PyObject* PyObject_GenericGetAttr(PyObject *o, PyObject *name) |
| 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 |
| 50 |
:attr:`~object.__dict__` (if present). As outlined in :ref:`descriptors`, |
| 51 |
data descriptors take preference over instance attributes, while non-data |
| 52 |
descriptors don't. Otherwise, an :exc:`AttributeError` is raised. |
| 53 |
|
| 54 |
|
| 55 |
.. c:function:: int PyObject_SetAttr(PyObject *o, PyObject *attr_name, PyObject *v) |
| 56 |
|
| 57 |
Set the value of the attribute named *attr_name*, for object *o*, to the value |
| 58 |
*v*. Raise an exception and return ``-1`` on failure; |
| 59 |
return ``0`` on success. This is the equivalent of the Python statement |
| 60 |
``o.attr_name = v``. |
| 61 |
|
| 62 |
If *v* is *NULL*, the attribute is deleted, however this feature is |
| 63 |
deprecated in favour of using :c:func:`PyObject_DelAttr`. |
| 64 |
|
| 65 |
|
| 66 |
.. c:function:: int PyObject_SetAttrString(PyObject *o, const char *attr_name, PyObject *v) |
| 67 |
|
| 68 |
Set the value of the attribute named *attr_name*, for object *o*, to the value |
| 69 |
*v*. Raise an exception and return ``-1`` on failure; |
| 70 |
return ``0`` on success. This is the equivalent of the Python statement |
| 71 |
``o.attr_name = v``. |
| 72 |
|
| 73 |
If *v* is *NULL*, the attribute is deleted, however this feature is |
| 74 |
deprecated in favour of using :c:func:`PyObject_DelAttrString`. |
| 75 |
|
| 76 |
|
| 77 |
.. c:function:: int PyObject_GenericSetAttr(PyObject *o, PyObject *name, PyObject *value) |
| 78 |
|
| 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 |
| 82 |
dictionary of classes in the object's MRO, and if found it takes preference |
| 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. |
| 87 |
|
| 88 |
|
| 89 |
.. c:function:: int PyObject_DelAttr(PyObject *o, PyObject *attr_name) |
| 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 |
|
| 95 |
.. c:function:: int PyObject_DelAttrString(PyObject *o, const char *attr_name) |
| 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 |
|
| 101 |
.. c:function:: PyObject* PyObject_RichCompare(PyObject *o1, PyObject *o2, int opid) |
| 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 |
|
| 111 |
.. c:function:: int PyObject_RichCompareBool(PyObject *o1, PyObject *o2, int opid) |
| 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 |
|
| 121 |
.. note:: |
| 122 |
If *o1* and *o2* are the same object, :c:func:`PyObject_RichCompareBool` |
| 123 |
will always return ``1`` for :const:`Py_EQ` and ``0`` for :const:`Py_NE`. |
| 124 |
|
| 125 |
.. c:function:: int PyObject_Cmp(PyObject *o1, PyObject *o2, int *result) |
| 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 |
|
| 135 |
.. c:function:: int PyObject_Compare(PyObject *o1, PyObject *o2) |
| 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 |
| 142 |
:c:func:`PyErr_Occurred` to detect an error. This is equivalent to the Python |
| 143 |
expression ``cmp(o1, o2)``. |
| 144 |
|
| 145 |
|
| 146 |
.. c:function:: PyObject* PyObject_Repr(PyObject *o) |
| 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 |
|
| 156 |
.. c:function:: PyObject* PyObject_Str(PyObject *o) |
| 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 |
|
| 166 |
.. c:function:: PyObject* PyObject_Bytes(PyObject *o) |
| 167 |
|
| 168 |
.. index:: builtin: bytes |
| 169 |
|
| 170 |
Compute a bytes representation of object *o*. In 2.x, this is just an alias |
| 171 |
for :c:func:`PyObject_Str`. |
| 172 |
|
| 173 |
|
| 174 |
.. c:function:: PyObject* PyObject_Unicode(PyObject *o) |
| 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 |
|
| 184 |
.. c:function:: int PyObject_IsInstance(PyObject *inst, PyObject *cls) |
| 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 |
| 188 |
*cls* is a type object rather than a class object, :c:func:`PyObject_IsInstance` |
| 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 |
| 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. |
| 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 |
| 208 |
*A*, if *A* is *B*, :c:func:`PyObject_IsSubclass` returns true. If *A* and *B* |
| 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. |
| 212 |
|
| 213 |
|
| 214 |
.. c:function:: int PyObject_IsSubclass(PyObject *derived, PyObject *cls) |
| 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 |
|
| 229 |
.. c:function:: int PyCallable_Check(PyObject *o) |
| 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 |
|
| 235 |
.. c:function:: PyObject* PyObject_Call(PyObject *callable_object, PyObject *args, PyObject *kw) |
| 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 |
|
| 249 |
.. c:function:: PyObject* PyObject_CallObject(PyObject *callable_object, PyObject *args) |
| 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 |
|
| 260 |
.. c:function:: PyObject* PyObject_CallFunction(PyObject *callable, char *format, ...) |
| 261 |
|
| 262 |
.. index:: builtin: apply |
| 263 |
|
| 264 |
Call a callable Python object *callable*, with a variable number of C arguments. |
| 265 |
The C arguments are described using a :c:func:`Py_BuildValue` style format |
| 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 |
| 269 |
``callable(*args)``. Note that if you only pass :c:type:`PyObject \*` args, |
| 270 |
:c:func:`PyObject_CallFunctionObjArgs` is a faster alternative. |
| 271 |
|
| 272 |
|
| 273 |
.. c:function:: PyObject* PyObject_CallMethod(PyObject *o, char *method, char *format, ...) |
| 274 |
|
| 275 |
Call the method named *method* of object *o* with a variable number of C |
| 276 |
arguments. The C arguments are described by a :c:func:`Py_BuildValue` format |
| 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)``. |
| 280 |
Note that if you only pass :c:type:`PyObject \*` args, |
| 281 |
:c:func:`PyObject_CallMethodObjArgs` is a faster alternative. |
| 282 |
|
| 283 |
|
| 284 |
.. c:function:: PyObject* PyObject_CallFunctionObjArgs(PyObject *callable, ..., NULL) |
| 285 |
|
| 286 |
Call a callable Python object *callable*, with a variable number of |
| 287 |
:c:type:`PyObject\*` arguments. The arguments are provided as a variable number |
| 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 |
|
| 294 |
.. c:function:: PyObject* PyObject_CallMethodObjArgs(PyObject *o, PyObject *name, ..., NULL) |
| 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 |
| 298 |
:c:type:`PyObject\*` arguments. The arguments are provided as a variable number |
| 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 |
|
| 305 |
.. c:function:: long PyObject_Hash(PyObject *o) |
| 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 |
|
| 313 |
.. c:function:: long PyObject_HashNotImplemented(PyObject *o) |
| 314 |
|
| 315 |
Set a :exc:`TypeError` indicating that ``type(o)`` is not hashable and return ``-1``. |
| 316 |
This function receives special treatment when stored in a ``tp_hash`` slot, |
| 317 |
allowing a type to explicitly indicate to the interpreter that it is not |
| 318 |
hashable. |
| 319 |
|
| 320 |
.. versionadded:: 2.6 |
| 321 |
|
| 322 |
|
| 323 |
.. c:function:: int PyObject_IsTrue(PyObject *o) |
| 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 |
|
| 330 |
.. c:function:: int PyObject_Not(PyObject *o) |
| 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 |
|
| 337 |
.. c:function:: PyObject* PyObject_Type(PyObject *o) |
| 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 |
| 346 |
pointer of type :c:type:`PyTypeObject\*`, except when the incremented reference |
| 347 |
count is needed. |
| 348 |
|
| 349 |
|
| 350 |
.. c:function:: int PyObject_TypeCheck(PyObject *o, PyTypeObject *type) |
| 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 |
|
| 358 |
.. c:function:: Py_ssize_t PyObject_Length(PyObject *o) |
| 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 |
|
| 367 |
.. versionchanged:: 2.5 |
| 368 |
These functions returned an :c:type:`int` type. This might require |
| 369 |
changes in your code for properly supporting 64-bit systems. |
| 370 |
|
| 371 |
|
| 372 |
.. c:function:: PyObject* PyObject_GetItem(PyObject *o, PyObject *key) |
| 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 |
|
| 378 |
.. c:function:: int PyObject_SetItem(PyObject *o, PyObject *key, PyObject *v) |
| 379 |
|
| 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 |
| 382 |
equivalent of the Python statement ``o[key] = v``. |
| 383 |
|
| 384 |
|
| 385 |
.. c:function:: int PyObject_DelItem(PyObject *o, PyObject *key) |
| 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 |
|
| 391 |
.. c:function:: int PyObject_AsFileDescriptor(PyObject *o) |
| 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 |
|
| 399 |
.. c:function:: PyObject* PyObject_Dir(PyObject *o) |
| 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 |
| 405 |
is active then *NULL* is returned but :c:func:`PyErr_Occurred` will return false. |
| 406 |
|
| 407 |
|
| 408 |
.. c:function:: PyObject* PyObject_GetIter(PyObject *o) |
| 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. |