| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 1 | .. highlightlang:: c | 
 | 2 |  | 
 | 3 | .. _object: | 
 | 4 |  | 
 | 5 | Object Protocol | 
 | 6 | =============== | 
 | 7 |  | 
 | 8 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 9 | .. c:function:: int PyObject_Print(PyObject *o, FILE *fp, int flags) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +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 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 17 | .. c:function:: int PyObject_HasAttr(PyObject *o, PyObject *attr_name) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +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 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 24 | .. c:function:: int PyObject_HasAttrString(PyObject *o, const char *attr_name) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +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 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 31 | .. c:function:: PyObject* PyObject_GetAttr(PyObject *o, PyObject *attr_name) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +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 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 38 | .. c:function:: PyObject* PyObject_GetAttrString(PyObject *o, const char *attr_name) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +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 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 45 | .. c:function:: PyObject* PyObject_GenericGetAttr(PyObject *o, PyObject *name) | 
| Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +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 | 
 | 50 |    :attr:`__dict__` (if present).  As outlined in :ref:`descriptors`, data | 
 | 51 |    descriptors take preference over instance attributes, while non-data | 
 | 52 |    descriptors don't.  Otherwise, an :exc:`AttributeError` is raised. | 
 | 53 |  | 
 | 54 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 55 | .. c:function:: int PyObject_SetAttr(PyObject *o, PyObject *attr_name, PyObject *v) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 56 |  | 
 | 57 |    Set the value of the attribute named *attr_name*, for object *o*, to the value | 
 | 58 |    *v*. Returns ``-1`` on failure.  This is the equivalent of the Python statement | 
 | 59 |    ``o.attr_name = v``. | 
 | 60 |  | 
 | 61 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 62 | .. c:function:: int PyObject_SetAttrString(PyObject *o, const char *attr_name, PyObject *v) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 63 |  | 
 | 64 |    Set the value of the attribute named *attr_name*, for object *o*, to the value | 
 | 65 |    *v*. Returns ``-1`` on failure.  This is the equivalent of the Python statement | 
 | 66 |    ``o.attr_name = v``. | 
 | 67 |  | 
 | 68 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 69 | .. c:function:: int PyObject_GenericSetAttr(PyObject *o, PyObject *name, PyObject *value) | 
| Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 70 |  | 
 | 71 |    Generic attribute setter function that is meant to be put into a type | 
 | 72 |    object's ``tp_setattro`` slot.  It looks for a data descriptor in the | 
 | 73 |    dictionary of classes in the object's MRO, and if found it takes preference | 
 | 74 |    over setting the attribute in the instance dictionary. Otherwise, the | 
 | 75 |    attribute is set in the object's :attr:`__dict__` (if present).  Otherwise, | 
 | 76 |    an :exc:`AttributeError` is raised and ``-1`` is returned. | 
 | 77 |  | 
 | 78 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 79 | .. c:function:: int PyObject_DelAttr(PyObject *o, PyObject *attr_name) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 80 |  | 
 | 81 |    Delete attribute named *attr_name*, for object *o*. Returns ``-1`` on failure. | 
 | 82 |    This is the equivalent of the Python statement ``del o.attr_name``. | 
 | 83 |  | 
 | 84 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 85 | .. c:function:: int PyObject_DelAttrString(PyObject *o, const char *attr_name) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 86 |  | 
 | 87 |    Delete attribute named *attr_name*, for object *o*. Returns ``-1`` on failure. | 
 | 88 |    This is the equivalent of the Python statement ``del o.attr_name``. | 
 | 89 |  | 
 | 90 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 91 | .. c:function:: PyObject* PyObject_RichCompare(PyObject *o1, PyObject *o2, int opid) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 92 |  | 
 | 93 |    Compare the values of *o1* and *o2* using the operation specified by *opid*, | 
 | 94 |    which must be one of :const:`Py_LT`, :const:`Py_LE`, :const:`Py_EQ`, | 
 | 95 |    :const:`Py_NE`, :const:`Py_GT`, or :const:`Py_GE`, corresponding to ``<``, | 
 | 96 |    ``<=``, ``==``, ``!=``, ``>``, or ``>=`` respectively. This is the equivalent of | 
 | 97 |    the Python expression ``o1 op o2``, where ``op`` is the operator corresponding | 
 | 98 |    to *opid*. Returns the value of the comparison on success, or *NULL* on failure. | 
 | 99 |  | 
 | 100 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 101 | .. c:function:: int PyObject_RichCompareBool(PyObject *o1, PyObject *o2, int opid) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +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. Returns ``-1`` on error, | 
 | 107 |    ``0`` if the result is false, ``1`` otherwise. This is the equivalent of the | 
 | 108 |    Python expression ``o1 op o2``, where ``op`` is the operator corresponding to | 
 | 109 |    *opid*. | 
 | 110 |  | 
| Eli Bendersky | ad30c42 | 2011-01-15 10:23:34 +0000 | [diff] [blame] | 111 | .. note:: | 
 | 112 |    If *o1* and *o2* are the same object, :c:func:`PyObject_RichCompareBool` | 
 | 113 |    will always return ``1`` for :const:`Py_EQ` and ``0`` for :const:`Py_NE`. | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 114 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 115 | .. c:function:: PyObject* PyObject_Repr(PyObject *o) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 116 |  | 
 | 117 |    .. index:: builtin: repr | 
 | 118 |  | 
 | 119 |    Compute a string representation of object *o*.  Returns the string | 
 | 120 |    representation on success, *NULL* on failure.  This is the equivalent of the | 
| Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 121 |    Python expression ``repr(o)``.  Called by the :func:`repr` built-in function. | 
 | 122 |  | 
 | 123 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 124 | .. c:function:: PyObject* PyObject_ASCII(PyObject *o) | 
| Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 125 |  | 
 | 126 |    .. index:: builtin: ascii | 
 | 127 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 128 |    As :c:func:`PyObject_Repr`, compute a string representation of object *o*, but | 
| Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 129 |    escape the non-ASCII characters in the string returned by | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 130 |    :c:func:`PyObject_Repr` with ``\x``, ``\u`` or ``\U`` escapes.  This generates | 
 | 131 |    a string similar to that returned by :c:func:`PyObject_Repr` in Python 2. | 
| Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 132 |    Called by the :func:`ascii` built-in function. | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 133 |  | 
 | 134 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 135 | .. c:function:: PyObject* PyObject_Str(PyObject *o) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 136 |  | 
 | 137 |    .. index:: builtin: str | 
 | 138 |  | 
 | 139 |    Compute a string representation of object *o*.  Returns the string | 
 | 140 |    representation on success, *NULL* on failure.  This is the equivalent of the | 
 | 141 |    Python expression ``str(o)``.  Called by the :func:`str` built-in function | 
 | 142 |    and, therefore, by the :func:`print` function. | 
 | 143 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 144 | .. c:function:: PyObject* PyObject_Bytes(PyObject *o) | 
| Benjamin Peterson | c15a073 | 2008-08-26 16:46:47 +0000 | [diff] [blame] | 145 |  | 
 | 146 |    .. index:: builtin: bytes | 
 | 147 |  | 
| Alexandre Vassalotti | eb6f8de | 2009-12-31 03:56:09 +0000 | [diff] [blame] | 148 |    Compute a bytes representation of object *o*.  *NULL* is returned on | 
 | 149 |    failure and a bytes object on success.  This is equivalent to the Python | 
 | 150 |    expression ``bytes(o)``, when *o* is not an integer.  Unlike ``bytes(o)``, | 
 | 151 |    a TypeError is raised when *o* is an integer instead of a zero-initialized | 
 | 152 |    bytes object. | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 153 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 154 | .. c:function:: int PyObject_IsInstance(PyObject *inst, PyObject *cls) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 155 |  | 
 | 156 |    Returns ``1`` if *inst* is an instance of the class *cls* or a subclass of | 
 | 157 |    *cls*, or ``0`` if not.  On error, returns ``-1`` and sets an exception.  If | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 158 |    *cls* is a type object rather than a class object, :c:func:`PyObject_IsInstance` | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 159 |    returns ``1`` if *inst* is of type *cls*.  If *cls* is a tuple, the check will | 
 | 160 |    be done against every entry in *cls*. The result will be ``1`` when at least one | 
 | 161 |    of the checks returns ``1``, otherwise it will be ``0``. If *inst* is not a | 
 | 162 |    class instance and *cls* is neither a type object, nor a class object, nor a | 
 | 163 |    tuple, *inst* must have a :attr:`__class__` attribute --- the class relationship | 
 | 164 |    of the value of that attribute with *cls* will be used to determine the result | 
 | 165 |    of this function. | 
 | 166 |  | 
 | 167 |  | 
 | 168 | Subclass determination is done in a fairly straightforward way, but includes a | 
 | 169 | wrinkle that implementors of extensions to the class system may want to be aware | 
 | 170 | of.  If :class:`A` and :class:`B` are class objects, :class:`B` is a subclass of | 
 | 171 | :class:`A` if it inherits from :class:`A` either directly or indirectly.  If | 
 | 172 | either is not a class object, a more general mechanism is used to determine the | 
 | 173 | class relationship of the two objects.  When testing if *B* is a subclass of | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 174 | *A*, if *A* is *B*, :c:func:`PyObject_IsSubclass` returns true.  If *A* and *B* | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 175 | are different objects, *B*'s :attr:`__bases__` attribute is searched in a | 
 | 176 | depth-first fashion for *A* --- the presence of the :attr:`__bases__` attribute | 
 | 177 | is considered sufficient for this determination. | 
 | 178 |  | 
 | 179 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 180 | .. c:function:: int PyObject_IsSubclass(PyObject *derived, PyObject *cls) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 181 |  | 
 | 182 |    Returns ``1`` if the class *derived* is identical to or derived from the class | 
 | 183 |    *cls*, otherwise returns ``0``.  In case of an error, returns ``-1``. If *cls* | 
 | 184 |    is a tuple, the check will be done against every entry in *cls*. The result will | 
 | 185 |    be ``1`` when at least one of the checks returns ``1``, otherwise it will be | 
 | 186 |    ``0``. If either *derived* or *cls* is not an actual class object (or tuple), | 
 | 187 |    this function uses the generic algorithm described above. | 
 | 188 |  | 
 | 189 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 190 | .. c:function:: int PyCallable_Check(PyObject *o) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 191 |  | 
 | 192 |    Determine if the object *o* is callable.  Return ``1`` if the object is callable | 
 | 193 |    and ``0`` otherwise.  This function always succeeds. | 
 | 194 |  | 
 | 195 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 196 | .. c:function:: PyObject* PyObject_Call(PyObject *callable_object, PyObject *args, PyObject *kw) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 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 |    ``callable_object(*args, **kw)``. | 
 | 204 |  | 
 | 205 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 206 | .. c:function:: PyObject* PyObject_CallObject(PyObject *callable_object, PyObject *args) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 207 |  | 
 | 208 |    Call a callable Python object *callable_object*, with arguments given by the | 
 | 209 |    tuple *args*.  If no arguments are needed, then *args* may be *NULL*.  Returns | 
 | 210 |    the result of the call on success, or *NULL* on failure.  This is the equivalent | 
 | 211 |    of the Python expression ``callable_object(*args)``. | 
 | 212 |  | 
 | 213 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 214 | .. c:function:: PyObject* PyObject_CallFunction(PyObject *callable, char *format, ...) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 215 |  | 
 | 216 |    Call a callable Python object *callable*, with a variable number of C arguments. | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 217 |    The C arguments are described using a :c:func:`Py_BuildValue` style format | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 218 |    string.  The format may be *NULL*, indicating that no arguments are provided. | 
 | 219 |    Returns the result of the call on success, or *NULL* on failure.  This is the | 
 | 220 |    equivalent of the Python expression ``callable(*args)``. Note that if you only | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 221 |    pass :c:type:`PyObject \*` args, :c:func:`PyObject_CallFunctionObjArgs` is a | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 222 |    faster alternative. | 
 | 223 |  | 
 | 224 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 225 | .. c:function:: PyObject* PyObject_CallMethod(PyObject *o, char *method, char *format, ...) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 226 |  | 
 | 227 |    Call the method named *method* of object *o* with a variable number of C | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 228 |    arguments.  The C arguments are described by a :c:func:`Py_BuildValue` format | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 229 |    string that should  produce a tuple.  The format may be *NULL*, indicating that | 
 | 230 |    no arguments are provided. Returns the result of the call on success, or *NULL* | 
 | 231 |    on failure.  This is the equivalent of the Python expression ``o.method(args)``. | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 232 |    Note that if you only pass :c:type:`PyObject \*` args, | 
 | 233 |    :c:func:`PyObject_CallMethodObjArgs` is a faster alternative. | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 234 |  | 
 | 235 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 236 | .. c:function:: PyObject* PyObject_CallFunctionObjArgs(PyObject *callable, ..., NULL) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 237 |  | 
 | 238 |    Call a callable Python object *callable*, with a variable number of | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 239 |    :c:type:`PyObject\*` arguments.  The arguments are provided as a variable number | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 240 |    of parameters followed by *NULL*. Returns the result of the call on success, or | 
 | 241 |    *NULL* on failure. | 
 | 242 |  | 
 | 243 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 244 | .. c:function:: PyObject* PyObject_CallMethodObjArgs(PyObject *o, PyObject *name, ..., NULL) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 245 |  | 
 | 246 |    Calls a method of the object *o*, where the name of the method is given as a | 
 | 247 |    Python string object in *name*.  It is called with a variable number of | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 248 |    :c:type:`PyObject\*` arguments.  The arguments are provided as a variable number | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 249 |    of parameters followed by *NULL*. Returns the result of the call on success, or | 
 | 250 |    *NULL* on failure. | 
 | 251 |  | 
 | 252 |  | 
| Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 253 | .. c:function:: Py_hash_t PyObject_Hash(PyObject *o) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 254 |  | 
 | 255 |    .. index:: builtin: hash | 
 | 256 |  | 
 | 257 |    Compute and return the hash value of an object *o*.  On failure, return ``-1``. | 
 | 258 |    This is the equivalent of the Python expression ``hash(o)``. | 
 | 259 |  | 
| Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 260 |    .. versionchanged:: 3.2 | 
| Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 261 |       The return type is now Py_hash_t.  This is a signed integer the same size | 
 | 262 |       as Py_ssize_t. | 
 | 263 |  | 
 | 264 |  | 
 | 265 | .. c:function:: Py_hash_t PyObject_HashNotImplemented(PyObject *o) | 
| Nick Coghlan | 7a70a3a | 2008-08-18 13:18:16 +0000 | [diff] [blame] | 266 |  | 
| Benjamin Peterson | e5384b0 | 2008-10-04 22:00:42 +0000 | [diff] [blame] | 267 |    Set a :exc:`TypeError` indicating that ``type(o)`` is not hashable and return ``-1``. | 
| Nick Coghlan | 7a70a3a | 2008-08-18 13:18:16 +0000 | [diff] [blame] | 268 |    This function receives special treatment when stored in a ``tp_hash`` slot, | 
| Benjamin Peterson | c4fe6f3 | 2008-08-19 18:57:56 +0000 | [diff] [blame] | 269 |    allowing a type to explicitly indicate to the interpreter that it is not | 
| Nick Coghlan | 7a70a3a | 2008-08-18 13:18:16 +0000 | [diff] [blame] | 270 |    hashable. | 
 | 271 |  | 
| Nick Coghlan | 7a70a3a | 2008-08-18 13:18:16 +0000 | [diff] [blame] | 272 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 273 | .. c:function:: int PyObject_IsTrue(PyObject *o) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 274 |  | 
 | 275 |    Returns ``1`` if the object *o* is considered to be true, and ``0`` otherwise. | 
 | 276 |    This is equivalent to the Python expression ``not not o``.  On failure, return | 
 | 277 |    ``-1``. | 
 | 278 |  | 
 | 279 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 280 | .. c:function:: int PyObject_Not(PyObject *o) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 281 |  | 
 | 282 |    Returns ``0`` if the object *o* is considered to be true, and ``1`` otherwise. | 
 | 283 |    This is equivalent to the Python expression ``not o``.  On failure, return | 
 | 284 |    ``-1``. | 
 | 285 |  | 
 | 286 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 287 | .. c:function:: PyObject* PyObject_Type(PyObject *o) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 288 |  | 
 | 289 |    .. index:: builtin: type | 
 | 290 |  | 
 | 291 |    When *o* is non-*NULL*, returns a type object corresponding to the object type | 
 | 292 |    of object *o*. On failure, raises :exc:`SystemError` and returns *NULL*.  This | 
 | 293 |    is equivalent to the Python expression ``type(o)``. This function increments the | 
 | 294 |    reference count of the return value. There's really no reason to use this | 
 | 295 |    function instead of the common expression ``o->ob_type``, which returns a | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 296 |    pointer of type :c:type:`PyTypeObject\*`, except when the incremented reference | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 297 |    count is needed. | 
 | 298 |  | 
 | 299 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 300 | .. c:function:: int PyObject_TypeCheck(PyObject *o, PyTypeObject *type) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 301 |  | 
 | 302 |    Return true if the object *o* is of type *type* or a subtype of *type*.  Both | 
 | 303 |    parameters must be non-*NULL*. | 
 | 304 |  | 
 | 305 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 306 | .. c:function:: Py_ssize_t PyObject_Length(PyObject *o) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 307 |                Py_ssize_t PyObject_Size(PyObject *o) | 
 | 308 |  | 
 | 309 |    .. index:: builtin: len | 
 | 310 |  | 
 | 311 |    Return the length of object *o*.  If the object *o* provides either the sequence | 
 | 312 |    and mapping protocols, the sequence length is returned.  On error, ``-1`` is | 
 | 313 |    returned.  This is the equivalent to the Python expression ``len(o)``. | 
 | 314 |  | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 315 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 316 | .. c:function:: PyObject* PyObject_GetItem(PyObject *o, PyObject *key) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 317 |  | 
 | 318 |    Return element of *o* corresponding to the object *key* or *NULL* on failure. | 
 | 319 |    This is the equivalent of the Python expression ``o[key]``. | 
 | 320 |  | 
 | 321 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 322 | .. c:function:: int PyObject_SetItem(PyObject *o, PyObject *key, PyObject *v) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 323 |  | 
 | 324 |    Map the object *key* to the value *v*.  Returns ``-1`` on failure.  This is the | 
 | 325 |    equivalent of the Python statement ``o[key] = v``. | 
 | 326 |  | 
 | 327 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 328 | .. c:function:: int PyObject_DelItem(PyObject *o, PyObject *key) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 329 |  | 
 | 330 |    Delete the mapping for *key* from *o*.  Returns ``-1`` on failure. This is the | 
 | 331 |    equivalent of the Python statement ``del o[key]``. | 
 | 332 |  | 
 | 333 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 334 | .. c:function:: PyObject* PyObject_Dir(PyObject *o) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 335 |  | 
 | 336 |    This is equivalent to the Python expression ``dir(o)``, returning a (possibly | 
 | 337 |    empty) list of strings appropriate for the object argument, or *NULL* if there | 
 | 338 |    was an error.  If the argument is *NULL*, this is like the Python ``dir()``, | 
 | 339 |    returning the names of the current locals; in this case, if no execution frame | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 340 |    is active then *NULL* is returned but :c:func:`PyErr_Occurred` will return false. | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 341 |  | 
 | 342 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 343 | .. c:function:: PyObject* PyObject_GetIter(PyObject *o) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 344 |  | 
 | 345 |    This is equivalent to the Python expression ``iter(o)``. It returns a new | 
 | 346 |    iterator for the object argument, or the object  itself if the object is already | 
 | 347 |    an iterator.  Raises :exc:`TypeError` and returns *NULL* if the object cannot be | 
 | 348 |    iterated. |