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