Stéphane Wirtel | cbb6484 | 2019-05-17 11:55:34 +0200 | [diff] [blame] | 1 | .. highlight:: c |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 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 | |
Serhiy Storchaka | 3fcc1e0 | 2018-12-18 13:57:17 +0200 | [diff] [blame] | 36 | Note that exceptions which occur while calling :meth:`__getattr__` and |
| 37 | :meth:`__getattribute__` methods will get suppressed. |
| 38 | To get error reporting use :c:func:`PyObject_GetAttr()` instead. |
| 39 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 40 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 41 | .. c:function:: int PyObject_HasAttrString(PyObject *o, const char *attr_name) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 42 | |
| 43 | Returns ``1`` if *o* has the attribute *attr_name*, and ``0`` otherwise. This |
| 44 | is equivalent to the Python expression ``hasattr(o, attr_name)``. This function |
| 45 | always succeeds. |
| 46 | |
Serhiy Storchaka | 3fcc1e0 | 2018-12-18 13:57:17 +0200 | [diff] [blame] | 47 | Note that exceptions which occur while calling :meth:`__getattr__` and |
| 48 | :meth:`__getattribute__` methods and creating a temporary string object |
| 49 | will get suppressed. |
| 50 | To get error reporting use :c:func:`PyObject_GetAttrString()` instead. |
| 51 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 52 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 53 | .. c:function:: PyObject* PyObject_GetAttr(PyObject *o, PyObject *attr_name) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 54 | |
| 55 | Retrieve an attribute named *attr_name* from object *o*. Returns the attribute |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 56 | value on success, or ``NULL`` on failure. This is the equivalent of the Python |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 57 | expression ``o.attr_name``. |
| 58 | |
| 59 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 60 | .. c:function:: PyObject* PyObject_GetAttrString(PyObject *o, const char *attr_name) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 61 | |
| 62 | Retrieve an attribute named *attr_name* from object *o*. Returns the attribute |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 63 | value on success, or ``NULL`` on failure. This is the equivalent of the Python |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 64 | expression ``o.attr_name``. |
| 65 | |
| 66 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 67 | .. c:function:: PyObject* PyObject_GenericGetAttr(PyObject *o, PyObject *name) |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 68 | |
| 69 | Generic attribute getter function that is meant to be put into a type |
| 70 | object's ``tp_getattro`` slot. It looks for a descriptor in the dictionary |
| 71 | of classes in the object's MRO as well as an attribute in the object's |
Serhiy Storchaka | 0b68a2d | 2013-10-09 13:26:17 +0300 | [diff] [blame] | 72 | :attr:`~object.__dict__` (if present). As outlined in :ref:`descriptors`, |
| 73 | data descriptors take preference over instance attributes, while non-data |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 74 | descriptors don't. Otherwise, an :exc:`AttributeError` is raised. |
| 75 | |
| 76 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 77 | .. c:function:: int PyObject_SetAttr(PyObject *o, PyObject *attr_name, PyObject *v) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 78 | |
| 79 | Set the value of the attribute named *attr_name*, for object *o*, to the value |
Martin Panter | 45be8d6 | 2015-12-08 00:03:20 +0000 | [diff] [blame] | 80 | *v*. Raise an exception and return ``-1`` on failure; |
| 81 | return ``0`` on success. This is the equivalent of the Python statement |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 82 | ``o.attr_name = v``. |
| 83 | |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 84 | If *v* is ``NULL``, the attribute is deleted, however this feature is |
Martin Panter | 45be8d6 | 2015-12-08 00:03:20 +0000 | [diff] [blame] | 85 | deprecated in favour of using :c:func:`PyObject_DelAttr`. |
| 86 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 87 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 88 | .. 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] | 89 | |
| 90 | Set the value of the attribute named *attr_name*, for object *o*, to the value |
Martin Panter | 45be8d6 | 2015-12-08 00:03:20 +0000 | [diff] [blame] | 91 | *v*. Raise an exception and return ``-1`` on failure; |
| 92 | return ``0`` on success. This is the equivalent of the Python statement |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 93 | ``o.attr_name = v``. |
| 94 | |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 95 | If *v* is ``NULL``, the attribute is deleted, however this feature is |
Martin Panter | 45be8d6 | 2015-12-08 00:03:20 +0000 | [diff] [blame] | 96 | deprecated in favour of using :c:func:`PyObject_DelAttrString`. |
| 97 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 98 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 99 | .. c:function:: int PyObject_GenericSetAttr(PyObject *o, PyObject *name, PyObject *value) |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 100 | |
Martin Panter | 45be8d6 | 2015-12-08 00:03:20 +0000 | [diff] [blame] | 101 | Generic attribute setter and deleter function that is meant |
| 102 | to be put into a type object's :c:member:`~PyTypeObject.tp_setattro` |
| 103 | slot. It looks for a data descriptor in the |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 104 | dictionary of classes in the object's MRO, and if found it takes preference |
Martin Panter | 45be8d6 | 2015-12-08 00:03:20 +0000 | [diff] [blame] | 105 | over setting or deleting the attribute in the instance dictionary. Otherwise, the |
| 106 | attribute is set or deleted in the object's :attr:`~object.__dict__` (if present). |
| 107 | On success, ``0`` is returned, otherwise an :exc:`AttributeError` |
| 108 | is raised and ``-1`` is returned. |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 109 | |
| 110 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 111 | .. c:function:: int PyObject_DelAttr(PyObject *o, PyObject *attr_name) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 112 | |
| 113 | Delete attribute named *attr_name*, for object *o*. Returns ``-1`` on failure. |
| 114 | This is the equivalent of the Python statement ``del o.attr_name``. |
| 115 | |
| 116 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 117 | .. c:function:: int PyObject_DelAttrString(PyObject *o, const char *attr_name) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 118 | |
| 119 | Delete attribute named *attr_name*, for object *o*. Returns ``-1`` on failure. |
| 120 | This is the equivalent of the Python statement ``del o.attr_name``. |
| 121 | |
| 122 | |
Benjamin Peterson | 1c262a6 | 2014-10-05 21:20:36 -0400 | [diff] [blame] | 123 | .. c:function:: PyObject* PyObject_GenericGetDict(PyObject *o, void *context) |
Benjamin Peterson | 8eb1269 | 2012-02-19 19:59:10 -0500 | [diff] [blame] | 124 | |
| 125 | A generic implementation for the getter of a ``__dict__`` descriptor. It |
| 126 | creates the dictionary if necessary. |
| 127 | |
Benjamin Peterson | 4384435 | 2012-02-20 08:48:25 -0500 | [diff] [blame] | 128 | .. versionadded:: 3.3 |
| 129 | |
Benjamin Peterson | 8eb1269 | 2012-02-19 19:59:10 -0500 | [diff] [blame] | 130 | |
Benjamin Peterson | 1c262a6 | 2014-10-05 21:20:36 -0400 | [diff] [blame] | 131 | .. c:function:: int PyObject_GenericSetDict(PyObject *o, void *context) |
Benjamin Peterson | 8eb1269 | 2012-02-19 19:59:10 -0500 | [diff] [blame] | 132 | |
| 133 | A generic implementation for the setter of a ``__dict__`` descriptor. This |
| 134 | implementation does not allow the dictionary to be deleted. |
| 135 | |
Benjamin Peterson | 4384435 | 2012-02-20 08:48:25 -0500 | [diff] [blame] | 136 | .. versionadded:: 3.3 |
| 137 | |
Benjamin Peterson | 8eb1269 | 2012-02-19 19:59:10 -0500 | [diff] [blame] | 138 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 139 | .. c:function:: PyObject* PyObject_RichCompare(PyObject *o1, PyObject *o2, int opid) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 140 | |
| 141 | Compare the values of *o1* and *o2* using the operation specified by *opid*, |
| 142 | which must be one of :const:`Py_LT`, :const:`Py_LE`, :const:`Py_EQ`, |
| 143 | :const:`Py_NE`, :const:`Py_GT`, or :const:`Py_GE`, corresponding to ``<``, |
| 144 | ``<=``, ``==``, ``!=``, ``>``, or ``>=`` respectively. This is the equivalent of |
| 145 | the Python expression ``o1 op o2``, where ``op`` is the operator corresponding |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 146 | to *opid*. Returns the value of the comparison on success, or ``NULL`` on failure. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 147 | |
| 148 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 149 | .. c:function:: int PyObject_RichCompareBool(PyObject *o1, PyObject *o2, int opid) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 150 | |
| 151 | Compare the values of *o1* and *o2* using the operation specified by *opid*, |
| 152 | which must be one of :const:`Py_LT`, :const:`Py_LE`, :const:`Py_EQ`, |
| 153 | :const:`Py_NE`, :const:`Py_GT`, or :const:`Py_GE`, corresponding to ``<``, |
| 154 | ``<=``, ``==``, ``!=``, ``>``, or ``>=`` respectively. Returns ``-1`` on error, |
| 155 | ``0`` if the result is false, ``1`` otherwise. This is the equivalent of the |
| 156 | Python expression ``o1 op o2``, where ``op`` is the operator corresponding to |
| 157 | *opid*. |
| 158 | |
Eli Bendersky | ad30c42 | 2011-01-15 10:23:34 +0000 | [diff] [blame] | 159 | .. note:: |
| 160 | If *o1* and *o2* are the same object, :c:func:`PyObject_RichCompareBool` |
| 161 | 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] | 162 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 163 | .. c:function:: PyObject* PyObject_Repr(PyObject *o) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 164 | |
| 165 | .. index:: builtin: repr |
| 166 | |
| 167 | Compute a string representation of object *o*. Returns the string |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 168 | representation on success, ``NULL`` on failure. This is the equivalent of the |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 169 | Python expression ``repr(o)``. Called by the :func:`repr` built-in function. |
| 170 | |
Nick Coghlan | c0bc0b4 | 2014-02-09 12:00:01 +1000 | [diff] [blame] | 171 | .. versionchanged:: 3.4 |
| 172 | This function now includes a debug assertion to help ensure that it |
| 173 | does not silently discard an active exception. |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 174 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 175 | .. c:function:: PyObject* PyObject_ASCII(PyObject *o) |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 176 | |
| 177 | .. index:: builtin: ascii |
| 178 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 179 | 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] | 180 | escape the non-ASCII characters in the string returned by |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 181 | :c:func:`PyObject_Repr` with ``\x``, ``\u`` or ``\U`` escapes. This generates |
| 182 | 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] | 183 | Called by the :func:`ascii` built-in function. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 184 | |
Chris Jerdonek | bb4e941 | 2012-11-28 01:38:40 -0800 | [diff] [blame] | 185 | .. index:: string; PyObject_Str (C function) |
| 186 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 187 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 188 | .. c:function:: PyObject* PyObject_Str(PyObject *o) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 189 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 190 | Compute a string representation of object *o*. Returns the string |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 191 | representation on success, ``NULL`` on failure. This is the equivalent of the |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 192 | Python expression ``str(o)``. Called by the :func:`str` built-in function |
| 193 | and, therefore, by the :func:`print` function. |
| 194 | |
Nick Coghlan | 3d7b364 | 2014-02-09 10:57:34 +1000 | [diff] [blame] | 195 | .. versionchanged:: 3.4 |
Nick Coghlan | c0bc0b4 | 2014-02-09 12:00:01 +1000 | [diff] [blame] | 196 | This function now includes a debug assertion to help ensure that it |
| 197 | does not silently discard an active exception. |
Nick Coghlan | 3d7b364 | 2014-02-09 10:57:34 +1000 | [diff] [blame] | 198 | |
Jeroen Demeyer | bf17d41 | 2019-11-05 16:48:04 +0100 | [diff] [blame^] | 199 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 200 | .. c:function:: PyObject* PyObject_Bytes(PyObject *o) |
Benjamin Peterson | c15a073 | 2008-08-26 16:46:47 +0000 | [diff] [blame] | 201 | |
| 202 | .. index:: builtin: bytes |
| 203 | |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 204 | Compute a bytes representation of object *o*. ``NULL`` is returned on |
Alexandre Vassalotti | eb6f8de | 2009-12-31 03:56:09 +0000 | [diff] [blame] | 205 | failure and a bytes object on success. This is equivalent to the Python |
| 206 | expression ``bytes(o)``, when *o* is not an integer. Unlike ``bytes(o)``, |
| 207 | a TypeError is raised when *o* is an integer instead of a zero-initialized |
| 208 | bytes object. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 209 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 210 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 211 | .. c:function:: int PyObject_IsSubclass(PyObject *derived, PyObject *cls) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 212 | |
Georg Brandl | f6d6dc2 | 2014-10-06 14:38:53 +0200 | [diff] [blame] | 213 | Return ``1`` if the class *derived* is identical to or derived from the class |
| 214 | *cls*, otherwise return ``0``. In case of an error, return ``-1``. |
| 215 | |
| 216 | If *cls* is a tuple, the check will be done against every entry in *cls*. |
| 217 | The result will be ``1`` when at least one of the checks returns ``1``, |
| 218 | otherwise it will be ``0``. |
| 219 | |
| 220 | If *cls* has a :meth:`~class.__subclasscheck__` method, it will be called to |
| 221 | determine the subclass status as described in :pep:`3119`. Otherwise, |
| 222 | *derived* is a subclass of *cls* if it is a direct or indirect subclass, |
| 223 | i.e. contained in ``cls.__mro__``. |
| 224 | |
| 225 | Normally only class objects, i.e. instances of :class:`type` or a derived |
Serhiy Storchaka | a7db057 | 2015-05-02 19:24:41 +0300 | [diff] [blame] | 226 | class, are considered classes. However, objects can override this by having |
Georg Brandl | f6d6dc2 | 2014-10-06 14:38:53 +0200 | [diff] [blame] | 227 | a :attr:`__bases__` attribute (which must be a tuple of base classes). |
| 228 | |
| 229 | |
| 230 | .. c:function:: int PyObject_IsInstance(PyObject *inst, PyObject *cls) |
| 231 | |
| 232 | Return ``1`` if *inst* is an instance of the class *cls* or a subclass of |
| 233 | *cls*, or ``0`` if not. On error, returns ``-1`` and sets an exception. |
| 234 | |
| 235 | If *cls* is a tuple, the check will be done against every entry in *cls*. |
| 236 | The result will be ``1`` when at least one of the checks returns ``1``, |
| 237 | otherwise it will be ``0``. |
| 238 | |
| 239 | If *cls* has a :meth:`~class.__instancecheck__` method, it will be called to |
| 240 | determine the subclass status as described in :pep:`3119`. Otherwise, *inst* |
| 241 | is an instance of *cls* if its class is a subclass of *cls*. |
| 242 | |
| 243 | An instance *inst* can override what is considered its class by having a |
| 244 | :attr:`__class__` attribute. |
| 245 | |
| 246 | An object *cls* can override if it is considered a class, and what its base |
| 247 | classes are, by having a :attr:`__bases__` attribute (which must be a tuple |
| 248 | of base classes). |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 249 | |
| 250 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 251 | .. c:function:: int PyCallable_Check(PyObject *o) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 252 | |
| 253 | Determine if the object *o* is callable. Return ``1`` if the object is callable |
| 254 | and ``0`` otherwise. This function always succeeds. |
| 255 | |
| 256 | |
Victor Stinner | 2ff58a2 | 2019-06-17 14:27:23 +0200 | [diff] [blame] | 257 | .. c:function:: PyObject* PyObject_CallNoArgs(PyObject *callable) |
| 258 | |
Victor Stinner | 5352cc4 | 2019-06-17 17:15:36 +0200 | [diff] [blame] | 259 | Call a callable Python object *callable* without any arguments. It is the |
| 260 | most efficient way to call a callable Python object without any argument. |
Victor Stinner | 2ff58a2 | 2019-06-17 14:27:23 +0200 | [diff] [blame] | 261 | |
Victor Stinner | 1ce2656 | 2019-06-17 14:58:10 +0200 | [diff] [blame] | 262 | Return the result of the call on success, or raise an exception and return |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 263 | ``NULL`` on failure. |
Victor Stinner | 2ff58a2 | 2019-06-17 14:27:23 +0200 | [diff] [blame] | 264 | |
| 265 | .. versionadded:: 3.9 |
| 266 | |
| 267 | |
Jeroen Demeyer | 196a530 | 2019-07-04 12:31:34 +0200 | [diff] [blame] | 268 | .. c:function:: PyObject* _PyObject_CallOneArg(PyObject *callable, PyObject *arg) |
| 269 | |
| 270 | Call a callable Python object *callable* with exactly 1 positional argument |
| 271 | *arg* and no keyword arguments. |
| 272 | |
| 273 | Return the result of the call on success, or raise an exception and return |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 274 | ``NULL`` on failure. |
Jeroen Demeyer | 196a530 | 2019-07-04 12:31:34 +0200 | [diff] [blame] | 275 | |
| 276 | .. versionadded:: 3.9 |
| 277 | |
| 278 | |
Victor Stinner | 2d0eb65 | 2016-12-06 16:27:24 +0100 | [diff] [blame] | 279 | .. c:function:: PyObject* PyObject_Call(PyObject *callable, PyObject *args, PyObject *kwargs) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 280 | |
Victor Stinner | 2d0eb65 | 2016-12-06 16:27:24 +0100 | [diff] [blame] | 281 | Call a callable Python object *callable*, with arguments given by the |
| 282 | tuple *args*, and named arguments given by the dictionary *kwargs*. |
| 283 | |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 284 | *args* must not be ``NULL``, use an empty tuple if no arguments are needed. |
| 285 | If no named arguments are needed, *kwargs* can be ``NULL``. |
Victor Stinner | 2d0eb65 | 2016-12-06 16:27:24 +0100 | [diff] [blame] | 286 | |
Victor Stinner | 1ce2656 | 2019-06-17 14:58:10 +0200 | [diff] [blame] | 287 | Return the result of the call on success, or raise an exception and return |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 288 | ``NULL`` on failure. |
Victor Stinner | 2d0eb65 | 2016-12-06 16:27:24 +0100 | [diff] [blame] | 289 | |
| 290 | This is the equivalent of the Python expression: |
| 291 | ``callable(*args, **kwargs)``. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 292 | |
| 293 | |
Victor Stinner | 2d0eb65 | 2016-12-06 16:27:24 +0100 | [diff] [blame] | 294 | .. c:function:: PyObject* PyObject_CallObject(PyObject *callable, PyObject *args) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 295 | |
Victor Stinner | 2d0eb65 | 2016-12-06 16:27:24 +0100 | [diff] [blame] | 296 | Call a callable Python object *callable*, with arguments given by the |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 297 | tuple *args*. If no arguments are needed, then *args* can be ``NULL``. |
Victor Stinner | 2d0eb65 | 2016-12-06 16:27:24 +0100 | [diff] [blame] | 298 | |
Victor Stinner | 1ce2656 | 2019-06-17 14:58:10 +0200 | [diff] [blame] | 299 | Return the result of the call on success, or raise an exception and return |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 300 | ``NULL`` on failure. |
Victor Stinner | 2d0eb65 | 2016-12-06 16:27:24 +0100 | [diff] [blame] | 301 | |
| 302 | This is the equivalent of the Python expression: ``callable(*args)``. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 303 | |
| 304 | |
Serhiy Storchaka | 1cfebc7 | 2013-05-29 18:50:54 +0300 | [diff] [blame] | 305 | .. c:function:: PyObject* PyObject_CallFunction(PyObject *callable, const char *format, ...) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 306 | |
| 307 | 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] | 308 | The C arguments are described using a :c:func:`Py_BuildValue` style format |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 309 | string. The format can be ``NULL``, indicating that no arguments are provided. |
Victor Stinner | 2d0eb65 | 2016-12-06 16:27:24 +0100 | [diff] [blame] | 310 | |
Victor Stinner | 1ce2656 | 2019-06-17 14:58:10 +0200 | [diff] [blame] | 311 | Return the result of the call on success, or raise an exception and return |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 312 | ``NULL`` on failure. |
Victor Stinner | 2d0eb65 | 2016-12-06 16:27:24 +0100 | [diff] [blame] | 313 | |
| 314 | This is the equivalent of the Python expression: ``callable(*args)``. |
| 315 | |
| 316 | Note that if you only pass :c:type:`PyObject \*` args, |
| 317 | :c:func:`PyObject_CallFunctionObjArgs` is a faster alternative. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 318 | |
Serhiy Storchaka | 1cfebc7 | 2013-05-29 18:50:54 +0300 | [diff] [blame] | 319 | .. versionchanged:: 3.4 |
| 320 | The type of *format* was changed from ``char *``. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 321 | |
Serhiy Storchaka | 1cfebc7 | 2013-05-29 18:50:54 +0300 | [diff] [blame] | 322 | |
Victor Stinner | 2d0eb65 | 2016-12-06 16:27:24 +0100 | [diff] [blame] | 323 | .. c:function:: PyObject* PyObject_CallMethod(PyObject *obj, const char *name, const char *format, ...) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 324 | |
Victor Stinner | 2d0eb65 | 2016-12-06 16:27:24 +0100 | [diff] [blame] | 325 | Call the method named *name* of object *obj* with a variable number of C |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 326 | arguments. The C arguments are described by a :c:func:`Py_BuildValue` format |
Victor Stinner | 2d0eb65 | 2016-12-06 16:27:24 +0100 | [diff] [blame] | 327 | string that should produce a tuple. |
| 328 | |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 329 | The format can be ``NULL``, indicating that no arguments are provided. |
Victor Stinner | 2d0eb65 | 2016-12-06 16:27:24 +0100 | [diff] [blame] | 330 | |
Victor Stinner | 1ce2656 | 2019-06-17 14:58:10 +0200 | [diff] [blame] | 331 | Return the result of the call on success, or raise an exception and return |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 332 | ``NULL`` on failure. |
Victor Stinner | 2d0eb65 | 2016-12-06 16:27:24 +0100 | [diff] [blame] | 333 | |
| 334 | This is the equivalent of the Python expression: |
| 335 | ``obj.name(arg1, arg2, ...)``. |
| 336 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 337 | Note that if you only pass :c:type:`PyObject \*` args, |
| 338 | :c:func:`PyObject_CallMethodObjArgs` is a faster alternative. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 339 | |
Serhiy Storchaka | 1cfebc7 | 2013-05-29 18:50:54 +0300 | [diff] [blame] | 340 | .. versionchanged:: 3.4 |
Victor Stinner | 2d0eb65 | 2016-12-06 16:27:24 +0100 | [diff] [blame] | 341 | The types of *name* and *format* were changed from ``char *``. |
Serhiy Storchaka | 1cfebc7 | 2013-05-29 18:50:54 +0300 | [diff] [blame] | 342 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 343 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 344 | .. c:function:: PyObject* PyObject_CallFunctionObjArgs(PyObject *callable, ..., NULL) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 345 | |
| 346 | Call a callable Python object *callable*, with a variable number of |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 347 | :c:type:`PyObject\*` arguments. The arguments are provided as a variable number |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 348 | of parameters followed by ``NULL``. |
Victor Stinner | 2d0eb65 | 2016-12-06 16:27:24 +0100 | [diff] [blame] | 349 | |
Victor Stinner | 1ce2656 | 2019-06-17 14:58:10 +0200 | [diff] [blame] | 350 | Return the result of the call on success, or raise an exception and return |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 351 | ``NULL`` on failure. |
Victor Stinner | 2d0eb65 | 2016-12-06 16:27:24 +0100 | [diff] [blame] | 352 | |
| 353 | This is the equivalent of the Python expression: |
| 354 | ``callable(arg1, arg2, ...)``. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 355 | |
| 356 | |
Victor Stinner | 2d0eb65 | 2016-12-06 16:27:24 +0100 | [diff] [blame] | 357 | .. c:function:: PyObject* PyObject_CallMethodObjArgs(PyObject *obj, PyObject *name, ..., NULL) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 358 | |
Victor Stinner | 2d0eb65 | 2016-12-06 16:27:24 +0100 | [diff] [blame] | 359 | Calls a method of the Python object *obj*, where the name of the method is given as a |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 360 | 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] | 361 | :c:type:`PyObject\*` arguments. The arguments are provided as a variable number |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 362 | of parameters followed by ``NULL``. |
Victor Stinner | 1ce2656 | 2019-06-17 14:58:10 +0200 | [diff] [blame] | 363 | |
| 364 | Return the result of the call on success, or raise an exception and return |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 365 | ``NULL`` on failure. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 366 | |
| 367 | |
Jeroen Demeyer | 762f93f | 2019-07-08 10:19:25 +0200 | [diff] [blame] | 368 | .. c:function:: PyObject* _PyObject_CallMethodNoArgs(PyObject *obj, PyObject *name) |
| 369 | |
| 370 | Call a method of the Python object *obj* without arguments, |
| 371 | where the name of the method is given as a Python string object in *name*. |
| 372 | |
| 373 | Return the result of the call on success, or raise an exception and return |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 374 | ``NULL`` on failure. |
Jeroen Demeyer | 762f93f | 2019-07-08 10:19:25 +0200 | [diff] [blame] | 375 | |
| 376 | .. versionadded:: 3.9 |
| 377 | |
| 378 | |
Jeroen Demeyer | 59ad110 | 2019-07-11 10:59:05 +0200 | [diff] [blame] | 379 | .. c:function:: PyObject* _PyObject_CallMethodOneArg(PyObject *obj, PyObject *name, PyObject *arg) |
| 380 | |
| 381 | Call a method of the Python object *obj* with a single positional argument |
| 382 | *arg*, where the name of the method is given as a Python string object in |
| 383 | *name*. |
| 384 | |
| 385 | Return the result of the call on success, or raise an exception and return |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 386 | ``NULL`` on failure. |
Jeroen Demeyer | 59ad110 | 2019-07-11 10:59:05 +0200 | [diff] [blame] | 387 | |
| 388 | .. versionadded:: 3.9 |
| 389 | |
| 390 | |
Jeroen Demeyer | 9e3e06e | 2019-06-03 01:43:13 +0200 | [diff] [blame] | 391 | .. c:function:: PyObject* _PyObject_Vectorcall(PyObject *callable, PyObject *const *args, size_t nargsf, PyObject *kwnames) |
| 392 | |
| 393 | Call a callable Python object *callable*, using |
| 394 | :c:data:`vectorcall <PyTypeObject.tp_vectorcall_offset>` if possible. |
| 395 | |
| 396 | *args* is a C array with the positional arguments. |
| 397 | |
| 398 | *nargsf* is the number of positional arguments plus optionally the flag |
| 399 | :const:`PY_VECTORCALL_ARGUMENTS_OFFSET` (see below). |
| 400 | To get actual number of arguments, use |
| 401 | :c:func:`PyVectorcall_NARGS(nargsf) <PyVectorcall_NARGS>`. |
| 402 | |
Serhiy Storchaka | e835b31 | 2019-10-30 21:37:16 +0200 | [diff] [blame] | 403 | *kwnames* can be either ``NULL`` (no keyword arguments) or a tuple of keyword |
Jeroen Demeyer | 0567786 | 2019-08-16 12:41:27 +0200 | [diff] [blame] | 404 | names, which must be strings. In the latter case, the values of the keyword |
| 405 | arguments are stored in *args* after the positional arguments. |
Jeroen Demeyer | 9e3e06e | 2019-06-03 01:43:13 +0200 | [diff] [blame] | 406 | The number of keyword arguments does not influence *nargsf*. |
| 407 | |
| 408 | *kwnames* must contain only objects of type ``str`` (not a subclass), |
| 409 | and all keys must be unique. |
| 410 | |
Victor Stinner | 1ce2656 | 2019-06-17 14:58:10 +0200 | [diff] [blame] | 411 | Return the result of the call on success, or raise an exception and return |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 412 | ``NULL`` on failure. |
Jeroen Demeyer | 9e3e06e | 2019-06-03 01:43:13 +0200 | [diff] [blame] | 413 | |
| 414 | This uses the vectorcall protocol if the callable supports it; |
| 415 | otherwise, the arguments are converted to use |
| 416 | :c:member:`~PyTypeObject.tp_call`. |
| 417 | |
| 418 | .. note:: |
| 419 | |
| 420 | This function is provisional and expected to become public in Python 3.9, |
| 421 | with a different name and, possibly, changed semantics. |
| 422 | If you use the function, plan for updating your code for Python 3.9. |
| 423 | |
| 424 | .. versionadded:: 3.8 |
| 425 | |
| 426 | .. c:var:: PY_VECTORCALL_ARGUMENTS_OFFSET |
| 427 | |
| 428 | If set in a vectorcall *nargsf* argument, the callee is allowed to |
| 429 | temporarily change ``args[-1]``. In other words, *args* points to |
| 430 | argument 1 (not 0) in the allocated vector. |
| 431 | The callee must restore the value of ``args[-1]`` before returning. |
| 432 | |
Jeroen Demeyer | b1263d5 | 2019-06-28 11:49:00 +0200 | [diff] [blame] | 433 | For :c:func:`_PyObject_VectorcallMethod`, this flag means instead that |
| 434 | ``args[0]`` may be changed. |
| 435 | |
Jeroen Demeyer | 9e3e06e | 2019-06-03 01:43:13 +0200 | [diff] [blame] | 436 | Whenever they can do so cheaply (without additional allocation), callers |
| 437 | are encouraged to use :const:`PY_VECTORCALL_ARGUMENTS_OFFSET`. |
| 438 | Doing so will allow callables such as bound methods to make their onward |
| 439 | calls (which include a prepended *self* argument) cheaply. |
| 440 | |
| 441 | .. versionadded:: 3.8 |
| 442 | |
| 443 | .. c:function:: Py_ssize_t PyVectorcall_NARGS(size_t nargsf) |
| 444 | |
| 445 | Given a vectorcall *nargsf* argument, return the actual number of |
| 446 | arguments. |
| 447 | Currently equivalent to ``nargsf & ~PY_VECTORCALL_ARGUMENTS_OFFSET``. |
| 448 | |
| 449 | .. versionadded:: 3.8 |
| 450 | |
| 451 | .. c:function:: PyObject* _PyObject_FastCallDict(PyObject *callable, PyObject *const *args, size_t nargsf, PyObject *kwdict) |
| 452 | |
| 453 | Same as :c:func:`_PyObject_Vectorcall` except that the keyword arguments |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 454 | are passed as a dictionary in *kwdict*. This may be ``NULL`` if there |
Jeroen Demeyer | 9e3e06e | 2019-06-03 01:43:13 +0200 | [diff] [blame] | 455 | are no keyword arguments. |
| 456 | |
| 457 | For callables supporting :c:data:`vectorcall <PyTypeObject.tp_vectorcall_offset>`, |
| 458 | the arguments are internally converted to the vectorcall convention. |
| 459 | Therefore, this function adds some overhead compared to |
| 460 | :c:func:`_PyObject_Vectorcall`. |
| 461 | It should only be used if the caller already has a dictionary ready to use. |
| 462 | |
| 463 | .. note:: |
| 464 | |
| 465 | This function is provisional and expected to become public in Python 3.9, |
| 466 | with a different name and, possibly, changed semantics. |
| 467 | If you use the function, plan for updating your code for Python 3.9. |
| 468 | |
| 469 | .. versionadded:: 3.8 |
| 470 | |
Jeroen Demeyer | b1263d5 | 2019-06-28 11:49:00 +0200 | [diff] [blame] | 471 | .. c:function:: PyObject* _PyObject_VectorcallMethod(PyObject *name, PyObject *const *args, size_t nargsf, PyObject *kwnames) |
| 472 | |
| 473 | Call a method using the vectorcall calling convention. The name of the method |
| 474 | is given as Python string *name*. The object whose method is called is |
| 475 | *args[0]* and the *args* array starting at *args[1]* represents the arguments |
| 476 | of the call. There must be at least one positional argument. |
| 477 | *nargsf* is the number of positional arguments including *args[0]*, |
| 478 | plus :const:`PY_VECTORCALL_ARGUMENTS_OFFSET` if the value of ``args[0]`` may |
| 479 | temporarily be changed. Keyword arguments can be passed just like in |
| 480 | :c:func:`_PyObject_Vectorcall`. |
| 481 | |
| 482 | If the object has the :const:`Py_TPFLAGS_METHOD_DESCRIPTOR` feature, |
| 483 | this will actually call the unbound method object with the full |
| 484 | *args* vector as arguments. |
| 485 | |
| 486 | Return the result of the call on success, or raise an exception and return |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 487 | ``NULL`` on failure. |
Jeroen Demeyer | b1263d5 | 2019-06-28 11:49:00 +0200 | [diff] [blame] | 488 | |
| 489 | .. versionadded:: 3.9 |
Jeroen Demeyer | 9e3e06e | 2019-06-03 01:43:13 +0200 | [diff] [blame] | 490 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 491 | .. c:function:: Py_hash_t PyObject_Hash(PyObject *o) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 492 | |
| 493 | .. index:: builtin: hash |
| 494 | |
| 495 | Compute and return the hash value of an object *o*. On failure, return ``-1``. |
| 496 | This is the equivalent of the Python expression ``hash(o)``. |
| 497 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 498 | .. versionchanged:: 3.2 |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 499 | The return type is now Py_hash_t. This is a signed integer the same size |
| 500 | as Py_ssize_t. |
| 501 | |
| 502 | |
| 503 | .. c:function:: Py_hash_t PyObject_HashNotImplemented(PyObject *o) |
Nick Coghlan | 7a70a3a | 2008-08-18 13:18:16 +0000 | [diff] [blame] | 504 | |
Benjamin Peterson | e5384b0 | 2008-10-04 22:00:42 +0000 | [diff] [blame] | 505 | 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] | 506 | This function receives special treatment when stored in a ``tp_hash`` slot, |
Benjamin Peterson | c4fe6f3 | 2008-08-19 18:57:56 +0000 | [diff] [blame] | 507 | 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] | 508 | hashable. |
| 509 | |
Nick Coghlan | 7a70a3a | 2008-08-18 13:18:16 +0000 | [diff] [blame] | 510 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 511 | .. c:function:: int PyObject_IsTrue(PyObject *o) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 512 | |
| 513 | Returns ``1`` if the object *o* is considered to be true, and ``0`` otherwise. |
| 514 | This is equivalent to the Python expression ``not not o``. On failure, return |
| 515 | ``-1``. |
| 516 | |
| 517 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 518 | .. c:function:: int PyObject_Not(PyObject *o) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 519 | |
| 520 | Returns ``0`` if the object *o* is considered to be true, and ``1`` otherwise. |
| 521 | This is equivalent to the Python expression ``not o``. On failure, return |
| 522 | ``-1``. |
| 523 | |
| 524 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 525 | .. c:function:: PyObject* PyObject_Type(PyObject *o) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 526 | |
| 527 | .. index:: builtin: type |
| 528 | |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 529 | When *o* is non-``NULL``, returns a type object corresponding to the object type |
| 530 | of object *o*. On failure, raises :exc:`SystemError` and returns ``NULL``. This |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 531 | is equivalent to the Python expression ``type(o)``. This function increments the |
| 532 | reference count of the return value. There's really no reason to use this |
| 533 | function instead of the common expression ``o->ob_type``, which returns a |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 534 | pointer of type :c:type:`PyTypeObject\*`, except when the incremented reference |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 535 | count is needed. |
| 536 | |
| 537 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 538 | .. c:function:: int PyObject_TypeCheck(PyObject *o, PyTypeObject *type) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 539 | |
| 540 | Return true if the object *o* is of type *type* or a subtype of *type*. Both |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 541 | parameters must be non-``NULL``. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 542 | |
| 543 | |
Serhiy Storchaka | f5b1183 | 2018-05-22 11:02:44 +0300 | [diff] [blame] | 544 | .. c:function:: Py_ssize_t PyObject_Size(PyObject *o) |
| 545 | Py_ssize_t PyObject_Length(PyObject *o) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 546 | |
| 547 | .. index:: builtin: len |
| 548 | |
| 549 | Return the length of object *o*. If the object *o* provides either the sequence |
| 550 | and mapping protocols, the sequence length is returned. On error, ``-1`` is |
| 551 | returned. This is the equivalent to the Python expression ``len(o)``. |
| 552 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 553 | |
Armin Ronacher | aa9a79d | 2012-10-06 14:03:24 +0200 | [diff] [blame] | 554 | .. c:function:: Py_ssize_t PyObject_LengthHint(PyObject *o, Py_ssize_t default) |
| 555 | |
Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 556 | Return an estimated length for the object *o*. First try to return its |
| 557 | actual length, then an estimate using :meth:`~object.__length_hint__`, and |
| 558 | finally return the default value. On error return ``-1``. This is the |
Armin Ronacher | aa9a79d | 2012-10-06 14:03:24 +0200 | [diff] [blame] | 559 | equivalent to the Python expression ``operator.length_hint(o, default)``. |
| 560 | |
Armin Ronacher | 74b38b1 | 2012-10-07 10:29:32 +0200 | [diff] [blame] | 561 | .. versionadded:: 3.4 |
| 562 | |
Georg Brandl | df48b97 | 2014-03-24 09:06:18 +0100 | [diff] [blame] | 563 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 564 | .. c:function:: PyObject* PyObject_GetItem(PyObject *o, PyObject *key) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 565 | |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 566 | Return element of *o* corresponding to the object *key* or ``NULL`` on failure. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 567 | This is the equivalent of the Python expression ``o[key]``. |
| 568 | |
| 569 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 570 | .. c:function:: int PyObject_SetItem(PyObject *o, PyObject *key, PyObject *v) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 571 | |
Martin Panter | 45be8d6 | 2015-12-08 00:03:20 +0000 | [diff] [blame] | 572 | Map the object *key* to the value *v*. Raise an exception and |
| 573 | return ``-1`` on failure; return ``0`` on success. This is the |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 574 | equivalent of the Python statement ``o[key] = v``. |
| 575 | |
| 576 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 577 | .. c:function:: int PyObject_DelItem(PyObject *o, PyObject *key) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 578 | |
Serhiy Storchaka | f5b1183 | 2018-05-22 11:02:44 +0300 | [diff] [blame] | 579 | Remove the mapping for the object *key* from the object *o*. Return ``-1`` |
| 580 | on failure. This is equivalent to the Python statement ``del o[key]``. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 581 | |
| 582 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 583 | .. c:function:: PyObject* PyObject_Dir(PyObject *o) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 584 | |
| 585 | This is equivalent to the Python expression ``dir(o)``, returning a (possibly |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 586 | empty) list of strings appropriate for the object argument, or ``NULL`` if there |
| 587 | was an error. If the argument is ``NULL``, this is like the Python ``dir()``, |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 588 | returning the names of the current locals; in this case, if no execution frame |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 589 | 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] | 590 | |
| 591 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 592 | .. c:function:: PyObject* PyObject_GetIter(PyObject *o) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 593 | |
| 594 | This is equivalent to the Python expression ``iter(o)``. It returns a new |
| 595 | iterator for the object argument, or the object itself if the object is already |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 596 | an iterator. Raises :exc:`TypeError` and returns ``NULL`` if the object cannot be |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 597 | iterated. |