blob: a84235b7e32d2c7ae475902ed6615a237deec406 [file] [log] [blame]
Stéphane Wirtelcbb64842019-05-17 11:55:34 +02001.. highlight:: c
Georg Brandl54a3faa2008-01-20 09:30:57 +00002
3.. _object:
4
5Object Protocol
6===============
7
8
Brian Curtin49281072011-08-11 09:41:31 -05009.. 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 Brandl60203b42010-10-06 10:11:56 +000022.. c:function:: int PyObject_Print(PyObject *o, FILE *fp, int flags)
Georg Brandl54a3faa2008-01-20 09:30:57 +000023
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 Brandl60203b42010-10-06 10:11:56 +000030.. c:function:: int PyObject_HasAttr(PyObject *o, PyObject *attr_name)
Georg Brandl54a3faa2008-01-20 09:30:57 +000031
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 Storchaka3fcc1e02018-12-18 13:57:17 +020036 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 Brandl54a3faa2008-01-20 09:30:57 +000040
Georg Brandl60203b42010-10-06 10:11:56 +000041.. c:function:: int PyObject_HasAttrString(PyObject *o, const char *attr_name)
Georg Brandl54a3faa2008-01-20 09:30:57 +000042
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 Storchaka3fcc1e02018-12-18 13:57:17 +020047 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 Brandl54a3faa2008-01-20 09:30:57 +000052
Georg Brandl60203b42010-10-06 10:11:56 +000053.. c:function:: PyObject* PyObject_GetAttr(PyObject *o, PyObject *attr_name)
Georg Brandl54a3faa2008-01-20 09:30:57 +000054
55 Retrieve an attribute named *attr_name* from object *o*. Returns the attribute
56 value on success, or *NULL* on failure. This is the equivalent of the Python
57 expression ``o.attr_name``.
58
59
Georg Brandl60203b42010-10-06 10:11:56 +000060.. c:function:: PyObject* PyObject_GetAttrString(PyObject *o, const char *attr_name)
Georg Brandl54a3faa2008-01-20 09:30:57 +000061
62 Retrieve an attribute named *attr_name* from object *o*. Returns the attribute
63 value on success, or *NULL* on failure. This is the equivalent of the Python
64 expression ``o.attr_name``.
65
66
Georg Brandl60203b42010-10-06 10:11:56 +000067.. c:function:: PyObject* PyObject_GenericGetAttr(PyObject *o, PyObject *name)
Benjamin Petersond23f8222009-04-05 19:13:16 +000068
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 Storchaka0b68a2d2013-10-09 13:26:17 +030072 :attr:`~object.__dict__` (if present). As outlined in :ref:`descriptors`,
73 data descriptors take preference over instance attributes, while non-data
Benjamin Petersond23f8222009-04-05 19:13:16 +000074 descriptors don't. Otherwise, an :exc:`AttributeError` is raised.
75
76
Georg Brandl60203b42010-10-06 10:11:56 +000077.. c:function:: int PyObject_SetAttr(PyObject *o, PyObject *attr_name, PyObject *v)
Georg Brandl54a3faa2008-01-20 09:30:57 +000078
79 Set the value of the attribute named *attr_name*, for object *o*, to the value
Martin Panter45be8d62015-12-08 00:03:20 +000080 *v*. Raise an exception and return ``-1`` on failure;
81 return ``0`` on success. This is the equivalent of the Python statement
Georg Brandl54a3faa2008-01-20 09:30:57 +000082 ``o.attr_name = v``.
83
Martin Panter45be8d62015-12-08 00:03:20 +000084 If *v* is *NULL*, the attribute is deleted, however this feature is
85 deprecated in favour of using :c:func:`PyObject_DelAttr`.
86
Georg Brandl54a3faa2008-01-20 09:30:57 +000087
Georg Brandl60203b42010-10-06 10:11:56 +000088.. c:function:: int PyObject_SetAttrString(PyObject *o, const char *attr_name, PyObject *v)
Georg Brandl54a3faa2008-01-20 09:30:57 +000089
90 Set the value of the attribute named *attr_name*, for object *o*, to the value
Martin Panter45be8d62015-12-08 00:03:20 +000091 *v*. Raise an exception and return ``-1`` on failure;
92 return ``0`` on success. This is the equivalent of the Python statement
Georg Brandl54a3faa2008-01-20 09:30:57 +000093 ``o.attr_name = v``.
94
Martin Panter45be8d62015-12-08 00:03:20 +000095 If *v* is *NULL*, the attribute is deleted, however this feature is
96 deprecated in favour of using :c:func:`PyObject_DelAttrString`.
97
Georg Brandl54a3faa2008-01-20 09:30:57 +000098
Georg Brandl60203b42010-10-06 10:11:56 +000099.. c:function:: int PyObject_GenericSetAttr(PyObject *o, PyObject *name, PyObject *value)
Benjamin Petersond23f8222009-04-05 19:13:16 +0000100
Martin Panter45be8d62015-12-08 00:03:20 +0000101 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 Petersond23f8222009-04-05 19:13:16 +0000104 dictionary of classes in the object's MRO, and if found it takes preference
Martin Panter45be8d62015-12-08 00:03:20 +0000105 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 Petersond23f8222009-04-05 19:13:16 +0000109
110
Georg Brandl60203b42010-10-06 10:11:56 +0000111.. c:function:: int PyObject_DelAttr(PyObject *o, PyObject *attr_name)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000112
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 Brandl60203b42010-10-06 10:11:56 +0000117.. c:function:: int PyObject_DelAttrString(PyObject *o, const char *attr_name)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000118
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 Peterson1c262a62014-10-05 21:20:36 -0400123.. c:function:: PyObject* PyObject_GenericGetDict(PyObject *o, void *context)
Benjamin Peterson8eb12692012-02-19 19:59:10 -0500124
125 A generic implementation for the getter of a ``__dict__`` descriptor. It
126 creates the dictionary if necessary.
127
Benjamin Peterson43844352012-02-20 08:48:25 -0500128 .. versionadded:: 3.3
129
Benjamin Peterson8eb12692012-02-19 19:59:10 -0500130
Benjamin Peterson1c262a62014-10-05 21:20:36 -0400131.. c:function:: int PyObject_GenericSetDict(PyObject *o, void *context)
Benjamin Peterson8eb12692012-02-19 19:59:10 -0500132
133 A generic implementation for the setter of a ``__dict__`` descriptor. This
134 implementation does not allow the dictionary to be deleted.
135
Benjamin Peterson43844352012-02-20 08:48:25 -0500136 .. versionadded:: 3.3
137
Benjamin Peterson8eb12692012-02-19 19:59:10 -0500138
Georg Brandl60203b42010-10-06 10:11:56 +0000139.. c:function:: PyObject* PyObject_RichCompare(PyObject *o1, PyObject *o2, int opid)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000140
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
146 to *opid*. Returns the value of the comparison on success, or *NULL* on failure.
147
148
Georg Brandl60203b42010-10-06 10:11:56 +0000149.. c:function:: int PyObject_RichCompareBool(PyObject *o1, PyObject *o2, int opid)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000150
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 Benderskyad30c422011-01-15 10:23:34 +0000159.. 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 Brandl54a3faa2008-01-20 09:30:57 +0000162
Georg Brandl60203b42010-10-06 10:11:56 +0000163.. c:function:: PyObject* PyObject_Repr(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000164
165 .. index:: builtin: repr
166
167 Compute a string representation of object *o*. Returns the string
168 representation on success, *NULL* on failure. This is the equivalent of the
Georg Brandl559e5d72008-06-11 18:37:52 +0000169 Python expression ``repr(o)``. Called by the :func:`repr` built-in function.
170
Nick Coghlanc0bc0b42014-02-09 12:00:01 +1000171 .. 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 Brandl559e5d72008-06-11 18:37:52 +0000174
Georg Brandl60203b42010-10-06 10:11:56 +0000175.. c:function:: PyObject* PyObject_ASCII(PyObject *o)
Georg Brandl559e5d72008-06-11 18:37:52 +0000176
177 .. index:: builtin: ascii
178
Georg Brandl60203b42010-10-06 10:11:56 +0000179 As :c:func:`PyObject_Repr`, compute a string representation of object *o*, but
Georg Brandl559e5d72008-06-11 18:37:52 +0000180 escape the non-ASCII characters in the string returned by
Georg Brandl60203b42010-10-06 10:11:56 +0000181 :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 Brandl559e5d72008-06-11 18:37:52 +0000183 Called by the :func:`ascii` built-in function.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000184
Chris Jerdonekbb4e9412012-11-28 01:38:40 -0800185 .. index:: string; PyObject_Str (C function)
186
Georg Brandl54a3faa2008-01-20 09:30:57 +0000187
Georg Brandl60203b42010-10-06 10:11:56 +0000188.. c:function:: PyObject* PyObject_Str(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000189
Georg Brandl54a3faa2008-01-20 09:30:57 +0000190 Compute a string representation of object *o*. Returns the string
191 representation on success, *NULL* on failure. This is the equivalent of the
192 Python expression ``str(o)``. Called by the :func:`str` built-in function
193 and, therefore, by the :func:`print` function.
194
Nick Coghlan3d7b3642014-02-09 10:57:34 +1000195 .. versionchanged:: 3.4
Nick Coghlanc0bc0b42014-02-09 12:00:01 +1000196 This function now includes a debug assertion to help ensure that it
197 does not silently discard an active exception.
Nick Coghlan3d7b3642014-02-09 10:57:34 +1000198
Georg Brandl60203b42010-10-06 10:11:56 +0000199.. c:function:: PyObject* PyObject_Bytes(PyObject *o)
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000200
201 .. index:: builtin: bytes
202
Alexandre Vassalottieb6f8de2009-12-31 03:56:09 +0000203 Compute a bytes representation of object *o*. *NULL* is returned on
204 failure and a bytes object on success. This is equivalent to the Python
205 expression ``bytes(o)``, when *o* is not an integer. Unlike ``bytes(o)``,
206 a TypeError is raised when *o* is an integer instead of a zero-initialized
207 bytes object.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000208
Georg Brandl54a3faa2008-01-20 09:30:57 +0000209
Georg Brandl60203b42010-10-06 10:11:56 +0000210.. c:function:: int PyObject_IsSubclass(PyObject *derived, PyObject *cls)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000211
Georg Brandlf6d6dc22014-10-06 14:38:53 +0200212 Return ``1`` if the class *derived* is identical to or derived from the class
213 *cls*, otherwise return ``0``. In case of an error, return ``-1``.
214
215 If *cls* is a tuple, the check will be done against every entry in *cls*.
216 The result will be ``1`` when at least one of the checks returns ``1``,
217 otherwise it will be ``0``.
218
219 If *cls* has a :meth:`~class.__subclasscheck__` method, it will be called to
220 determine the subclass status as described in :pep:`3119`. Otherwise,
221 *derived* is a subclass of *cls* if it is a direct or indirect subclass,
222 i.e. contained in ``cls.__mro__``.
223
224 Normally only class objects, i.e. instances of :class:`type` or a derived
Serhiy Storchakaa7db0572015-05-02 19:24:41 +0300225 class, are considered classes. However, objects can override this by having
Georg Brandlf6d6dc22014-10-06 14:38:53 +0200226 a :attr:`__bases__` attribute (which must be a tuple of base classes).
227
228
229.. c:function:: int PyObject_IsInstance(PyObject *inst, PyObject *cls)
230
231 Return ``1`` if *inst* is an instance of the class *cls* or a subclass of
232 *cls*, or ``0`` if not. On error, returns ``-1`` and sets an exception.
233
234 If *cls* is a tuple, the check will be done against every entry in *cls*.
235 The result will be ``1`` when at least one of the checks returns ``1``,
236 otherwise it will be ``0``.
237
238 If *cls* has a :meth:`~class.__instancecheck__` method, it will be called to
239 determine the subclass status as described in :pep:`3119`. Otherwise, *inst*
240 is an instance of *cls* if its class is a subclass of *cls*.
241
242 An instance *inst* can override what is considered its class by having a
243 :attr:`__class__` attribute.
244
245 An object *cls* can override if it is considered a class, and what its base
246 classes are, by having a :attr:`__bases__` attribute (which must be a tuple
247 of base classes).
Georg Brandl54a3faa2008-01-20 09:30:57 +0000248
249
Georg Brandl60203b42010-10-06 10:11:56 +0000250.. c:function:: int PyCallable_Check(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000251
252 Determine if the object *o* is callable. Return ``1`` if the object is callable
253 and ``0`` otherwise. This function always succeeds.
254
255
Victor Stinner2ff58a22019-06-17 14:27:23 +0200256.. c:function:: PyObject* PyObject_CallNoArgs(PyObject *callable)
257
Victor Stinner5352cc42019-06-17 17:15:36 +0200258 Call a callable Python object *callable* without any arguments. It is the
259 most efficient way to call a callable Python object without any argument.
Victor Stinner2ff58a22019-06-17 14:27:23 +0200260
Victor Stinner1ce26562019-06-17 14:58:10 +0200261 Return the result of the call on success, or raise an exception and return
Victor Stinner2ff58a22019-06-17 14:27:23 +0200262 *NULL* on failure.
263
264 .. versionadded:: 3.9
265
266
Victor Stinner2d0eb652016-12-06 16:27:24 +0100267.. c:function:: PyObject* PyObject_Call(PyObject *callable, PyObject *args, PyObject *kwargs)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000268
Victor Stinner2d0eb652016-12-06 16:27:24 +0100269 Call a callable Python object *callable*, with arguments given by the
270 tuple *args*, and named arguments given by the dictionary *kwargs*.
271
272 *args* must not be *NULL*, use an empty tuple if no arguments are needed.
273 If no named arguments are needed, *kwargs* can be *NULL*.
274
Victor Stinner1ce26562019-06-17 14:58:10 +0200275 Return the result of the call on success, or raise an exception and return
276 *NULL* on failure.
Victor Stinner2d0eb652016-12-06 16:27:24 +0100277
278 This is the equivalent of the Python expression:
279 ``callable(*args, **kwargs)``.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000280
281
Victor Stinner2d0eb652016-12-06 16:27:24 +0100282.. c:function:: PyObject* PyObject_CallObject(PyObject *callable, PyObject *args)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000283
Victor Stinner2d0eb652016-12-06 16:27:24 +0100284 Call a callable Python object *callable*, with arguments given by the
285 tuple *args*. If no arguments are needed, then *args* can be *NULL*.
286
Victor Stinner1ce26562019-06-17 14:58:10 +0200287 Return the result of the call on success, or raise an exception and return
288 *NULL* on failure.
Victor Stinner2d0eb652016-12-06 16:27:24 +0100289
290 This is the equivalent of the Python expression: ``callable(*args)``.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000291
292
Serhiy Storchaka1cfebc72013-05-29 18:50:54 +0300293.. c:function:: PyObject* PyObject_CallFunction(PyObject *callable, const char *format, ...)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000294
295 Call a callable Python object *callable*, with a variable number of C arguments.
Georg Brandl60203b42010-10-06 10:11:56 +0000296 The C arguments are described using a :c:func:`Py_BuildValue` style format
Victor Stinner2d0eb652016-12-06 16:27:24 +0100297 string. The format can be *NULL*, indicating that no arguments are provided.
298
Victor Stinner1ce26562019-06-17 14:58:10 +0200299 Return the result of the call on success, or raise an exception and return
300 *NULL* on failure.
Victor Stinner2d0eb652016-12-06 16:27:24 +0100301
302 This is the equivalent of the Python expression: ``callable(*args)``.
303
304 Note that if you only pass :c:type:`PyObject \*` args,
305 :c:func:`PyObject_CallFunctionObjArgs` is a faster alternative.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000306
Serhiy Storchaka1cfebc72013-05-29 18:50:54 +0300307 .. versionchanged:: 3.4
308 The type of *format* was changed from ``char *``.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000309
Serhiy Storchaka1cfebc72013-05-29 18:50:54 +0300310
Victor Stinner2d0eb652016-12-06 16:27:24 +0100311.. c:function:: PyObject* PyObject_CallMethod(PyObject *obj, const char *name, const char *format, ...)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000312
Victor Stinner2d0eb652016-12-06 16:27:24 +0100313 Call the method named *name* of object *obj* with a variable number of C
Georg Brandl60203b42010-10-06 10:11:56 +0000314 arguments. The C arguments are described by a :c:func:`Py_BuildValue` format
Victor Stinner2d0eb652016-12-06 16:27:24 +0100315 string that should produce a tuple.
316
317 The format can be *NULL*, indicating that no arguments are provided.
318
Victor Stinner1ce26562019-06-17 14:58:10 +0200319 Return the result of the call on success, or raise an exception and return
320 *NULL* on failure.
Victor Stinner2d0eb652016-12-06 16:27:24 +0100321
322 This is the equivalent of the Python expression:
323 ``obj.name(arg1, arg2, ...)``.
324
Georg Brandl60203b42010-10-06 10:11:56 +0000325 Note that if you only pass :c:type:`PyObject \*` args,
326 :c:func:`PyObject_CallMethodObjArgs` is a faster alternative.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000327
Serhiy Storchaka1cfebc72013-05-29 18:50:54 +0300328 .. versionchanged:: 3.4
Victor Stinner2d0eb652016-12-06 16:27:24 +0100329 The types of *name* and *format* were changed from ``char *``.
Serhiy Storchaka1cfebc72013-05-29 18:50:54 +0300330
Georg Brandl54a3faa2008-01-20 09:30:57 +0000331
Georg Brandl60203b42010-10-06 10:11:56 +0000332.. c:function:: PyObject* PyObject_CallFunctionObjArgs(PyObject *callable, ..., NULL)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000333
334 Call a callable Python object *callable*, with a variable number of
Georg Brandl60203b42010-10-06 10:11:56 +0000335 :c:type:`PyObject\*` arguments. The arguments are provided as a variable number
Victor Stinner2d0eb652016-12-06 16:27:24 +0100336 of parameters followed by *NULL*.
337
Victor Stinner1ce26562019-06-17 14:58:10 +0200338 Return the result of the call on success, or raise an exception and return
339 *NULL* on failure.
Victor Stinner2d0eb652016-12-06 16:27:24 +0100340
341 This is the equivalent of the Python expression:
342 ``callable(arg1, arg2, ...)``.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000343
344
Victor Stinner2d0eb652016-12-06 16:27:24 +0100345.. c:function:: PyObject* PyObject_CallMethodObjArgs(PyObject *obj, PyObject *name, ..., NULL)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000346
Victor Stinner2d0eb652016-12-06 16:27:24 +0100347 Calls a method of the Python object *obj*, where the name of the method is given as a
Georg Brandl54a3faa2008-01-20 09:30:57 +0000348 Python string object in *name*. It is called with a variable number of
Georg Brandl60203b42010-10-06 10:11:56 +0000349 :c:type:`PyObject\*` arguments. The arguments are provided as a variable number
Victor Stinner1ce26562019-06-17 14:58:10 +0200350 of parameters followed by *NULL*.
351
352 Return the result of the call on success, or raise an exception and return
Georg Brandl54a3faa2008-01-20 09:30:57 +0000353 *NULL* on failure.
354
355
Jeroen Demeyer9e3e06e2019-06-03 01:43:13 +0200356.. c:function:: PyObject* _PyObject_Vectorcall(PyObject *callable, PyObject *const *args, size_t nargsf, PyObject *kwnames)
357
358 Call a callable Python object *callable*, using
359 :c:data:`vectorcall <PyTypeObject.tp_vectorcall_offset>` if possible.
360
361 *args* is a C array with the positional arguments.
362
363 *nargsf* is the number of positional arguments plus optionally the flag
364 :const:`PY_VECTORCALL_ARGUMENTS_OFFSET` (see below).
365 To get actual number of arguments, use
366 :c:func:`PyVectorcall_NARGS(nargsf) <PyVectorcall_NARGS>`.
367
368 *kwnames* can be either NULL (no keyword arguments) or a tuple of keyword
369 names. In the latter case, the values of the keyword arguments are stored
370 in *args* after the positional arguments.
371 The number of keyword arguments does not influence *nargsf*.
372
373 *kwnames* must contain only objects of type ``str`` (not a subclass),
374 and all keys must be unique.
375
Victor Stinner1ce26562019-06-17 14:58:10 +0200376 Return the result of the call on success, or raise an exception and return
377 *NULL* on failure.
Jeroen Demeyer9e3e06e2019-06-03 01:43:13 +0200378
379 This uses the vectorcall protocol if the callable supports it;
380 otherwise, the arguments are converted to use
381 :c:member:`~PyTypeObject.tp_call`.
382
383 .. note::
384
385 This function is provisional and expected to become public in Python 3.9,
386 with a different name and, possibly, changed semantics.
387 If you use the function, plan for updating your code for Python 3.9.
388
389 .. versionadded:: 3.8
390
391.. c:var:: PY_VECTORCALL_ARGUMENTS_OFFSET
392
393 If set in a vectorcall *nargsf* argument, the callee is allowed to
394 temporarily change ``args[-1]``. In other words, *args* points to
395 argument 1 (not 0) in the allocated vector.
396 The callee must restore the value of ``args[-1]`` before returning.
397
Jeroen Demeyerb1263d52019-06-28 11:49:00 +0200398 For :c:func:`_PyObject_VectorcallMethod`, this flag means instead that
399 ``args[0]`` may be changed.
400
Jeroen Demeyer9e3e06e2019-06-03 01:43:13 +0200401 Whenever they can do so cheaply (without additional allocation), callers
402 are encouraged to use :const:`PY_VECTORCALL_ARGUMENTS_OFFSET`.
403 Doing so will allow callables such as bound methods to make their onward
404 calls (which include a prepended *self* argument) cheaply.
405
406 .. versionadded:: 3.8
407
408.. c:function:: Py_ssize_t PyVectorcall_NARGS(size_t nargsf)
409
410 Given a vectorcall *nargsf* argument, return the actual number of
411 arguments.
412 Currently equivalent to ``nargsf & ~PY_VECTORCALL_ARGUMENTS_OFFSET``.
413
414 .. versionadded:: 3.8
415
416.. c:function:: PyObject* _PyObject_FastCallDict(PyObject *callable, PyObject *const *args, size_t nargsf, PyObject *kwdict)
417
418 Same as :c:func:`_PyObject_Vectorcall` except that the keyword arguments
419 are passed as a dictionary in *kwdict*. This may be *NULL* if there
420 are no keyword arguments.
421
422 For callables supporting :c:data:`vectorcall <PyTypeObject.tp_vectorcall_offset>`,
423 the arguments are internally converted to the vectorcall convention.
424 Therefore, this function adds some overhead compared to
425 :c:func:`_PyObject_Vectorcall`.
426 It should only be used if the caller already has a dictionary ready to use.
427
428 .. note::
429
430 This function is provisional and expected to become public in Python 3.9,
431 with a different name and, possibly, changed semantics.
432 If you use the function, plan for updating your code for Python 3.9.
433
434 .. versionadded:: 3.8
435
Jeroen Demeyerb1263d52019-06-28 11:49:00 +0200436.. c:function:: PyObject* _PyObject_VectorcallMethod(PyObject *name, PyObject *const *args, size_t nargsf, PyObject *kwnames)
437
438 Call a method using the vectorcall calling convention. The name of the method
439 is given as Python string *name*. The object whose method is called is
440 *args[0]* and the *args* array starting at *args[1]* represents the arguments
441 of the call. There must be at least one positional argument.
442 *nargsf* is the number of positional arguments including *args[0]*,
443 plus :const:`PY_VECTORCALL_ARGUMENTS_OFFSET` if the value of ``args[0]`` may
444 temporarily be changed. Keyword arguments can be passed just like in
445 :c:func:`_PyObject_Vectorcall`.
446
447 If the object has the :const:`Py_TPFLAGS_METHOD_DESCRIPTOR` feature,
448 this will actually call the unbound method object with the full
449 *args* vector as arguments.
450
451 Return the result of the call on success, or raise an exception and return
452 *NULL* on failure.
453
454 .. versionadded:: 3.9
Jeroen Demeyer9e3e06e2019-06-03 01:43:13 +0200455
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000456.. c:function:: Py_hash_t PyObject_Hash(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000457
458 .. index:: builtin: hash
459
460 Compute and return the hash value of an object *o*. On failure, return ``-1``.
461 This is the equivalent of the Python expression ``hash(o)``.
462
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000463 .. versionchanged:: 3.2
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000464 The return type is now Py_hash_t. This is a signed integer the same size
465 as Py_ssize_t.
466
467
468.. c:function:: Py_hash_t PyObject_HashNotImplemented(PyObject *o)
Nick Coghlan7a70a3a2008-08-18 13:18:16 +0000469
Benjamin Petersone5384b02008-10-04 22:00:42 +0000470 Set a :exc:`TypeError` indicating that ``type(o)`` is not hashable and return ``-1``.
Nick Coghlan7a70a3a2008-08-18 13:18:16 +0000471 This function receives special treatment when stored in a ``tp_hash`` slot,
Benjamin Petersonc4fe6f32008-08-19 18:57:56 +0000472 allowing a type to explicitly indicate to the interpreter that it is not
Nick Coghlan7a70a3a2008-08-18 13:18:16 +0000473 hashable.
474
Nick Coghlan7a70a3a2008-08-18 13:18:16 +0000475
Georg Brandl60203b42010-10-06 10:11:56 +0000476.. c:function:: int PyObject_IsTrue(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000477
478 Returns ``1`` if the object *o* is considered to be true, and ``0`` otherwise.
479 This is equivalent to the Python expression ``not not o``. On failure, return
480 ``-1``.
481
482
Georg Brandl60203b42010-10-06 10:11:56 +0000483.. c:function:: int PyObject_Not(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000484
485 Returns ``0`` if the object *o* is considered to be true, and ``1`` otherwise.
486 This is equivalent to the Python expression ``not o``. On failure, return
487 ``-1``.
488
489
Georg Brandl60203b42010-10-06 10:11:56 +0000490.. c:function:: PyObject* PyObject_Type(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000491
492 .. index:: builtin: type
493
494 When *o* is non-*NULL*, returns a type object corresponding to the object type
495 of object *o*. On failure, raises :exc:`SystemError` and returns *NULL*. This
496 is equivalent to the Python expression ``type(o)``. This function increments the
497 reference count of the return value. There's really no reason to use this
498 function instead of the common expression ``o->ob_type``, which returns a
Georg Brandl60203b42010-10-06 10:11:56 +0000499 pointer of type :c:type:`PyTypeObject\*`, except when the incremented reference
Georg Brandl54a3faa2008-01-20 09:30:57 +0000500 count is needed.
501
502
Georg Brandl60203b42010-10-06 10:11:56 +0000503.. c:function:: int PyObject_TypeCheck(PyObject *o, PyTypeObject *type)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000504
505 Return true if the object *o* is of type *type* or a subtype of *type*. Both
506 parameters must be non-*NULL*.
507
508
Serhiy Storchakaf5b11832018-05-22 11:02:44 +0300509.. c:function:: Py_ssize_t PyObject_Size(PyObject *o)
510 Py_ssize_t PyObject_Length(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000511
512 .. index:: builtin: len
513
514 Return the length of object *o*. If the object *o* provides either the sequence
515 and mapping protocols, the sequence length is returned. On error, ``-1`` is
516 returned. This is the equivalent to the Python expression ``len(o)``.
517
Georg Brandl54a3faa2008-01-20 09:30:57 +0000518
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200519.. c:function:: Py_ssize_t PyObject_LengthHint(PyObject *o, Py_ssize_t default)
520
Larry Hastings3732ed22014-03-15 21:13:56 -0700521 Return an estimated length for the object *o*. First try to return its
522 actual length, then an estimate using :meth:`~object.__length_hint__`, and
523 finally return the default value. On error return ``-1``. This is the
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200524 equivalent to the Python expression ``operator.length_hint(o, default)``.
525
Armin Ronacher74b38b12012-10-07 10:29:32 +0200526 .. versionadded:: 3.4
527
Georg Brandldf48b972014-03-24 09:06:18 +0100528
Georg Brandl60203b42010-10-06 10:11:56 +0000529.. c:function:: PyObject* PyObject_GetItem(PyObject *o, PyObject *key)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000530
531 Return element of *o* corresponding to the object *key* or *NULL* on failure.
532 This is the equivalent of the Python expression ``o[key]``.
533
534
Georg Brandl60203b42010-10-06 10:11:56 +0000535.. c:function:: int PyObject_SetItem(PyObject *o, PyObject *key, PyObject *v)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000536
Martin Panter45be8d62015-12-08 00:03:20 +0000537 Map the object *key* to the value *v*. Raise an exception and
538 return ``-1`` on failure; return ``0`` on success. This is the
Georg Brandl54a3faa2008-01-20 09:30:57 +0000539 equivalent of the Python statement ``o[key] = v``.
540
541
Georg Brandl60203b42010-10-06 10:11:56 +0000542.. c:function:: int PyObject_DelItem(PyObject *o, PyObject *key)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000543
Serhiy Storchakaf5b11832018-05-22 11:02:44 +0300544 Remove the mapping for the object *key* from the object *o*. Return ``-1``
545 on failure. This is equivalent to the Python statement ``del o[key]``.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000546
547
Georg Brandl60203b42010-10-06 10:11:56 +0000548.. c:function:: PyObject* PyObject_Dir(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000549
550 This is equivalent to the Python expression ``dir(o)``, returning a (possibly
551 empty) list of strings appropriate for the object argument, or *NULL* if there
552 was an error. If the argument is *NULL*, this is like the Python ``dir()``,
553 returning the names of the current locals; in this case, if no execution frame
Georg Brandl60203b42010-10-06 10:11:56 +0000554 is active then *NULL* is returned but :c:func:`PyErr_Occurred` will return false.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000555
556
Georg Brandl60203b42010-10-06 10:11:56 +0000557.. c:function:: PyObject* PyObject_GetIter(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000558
559 This is equivalent to the Python expression ``iter(o)``. It returns a new
560 iterator for the object argument, or the object itself if the object is already
561 an iterator. Raises :exc:`TypeError` and returns *NULL* if the object cannot be
562 iterated.