| 1 |
.. highlight:: c |
| 2 |
|
| 3 |
.. _object: |
| 4 |
|
| 5 |
Object Protocol |
| 6 |
=============== |
| 7 |
|
| 8 |
|
| 9 |
.. c:function:: PyObject* Py_GetConstant(unsigned int constant_id) |
| 10 |
|
| 11 |
Get a :term:`strong reference` to a constant. |
| 12 |
|
| 13 |
Set an exception and return ``NULL`` if *constant_id* is invalid. |
| 14 |
|
| 15 |
*constant_id* must be one of these constant identifiers: |
| 16 |
|
| 17 |
.. c:namespace:: NULL |
| 18 |
|
| 19 |
======================================== ===== ========================= |
| 20 |
Constant Identifier Value Returned object |
| 21 |
======================================== ===== ========================= |
| 22 |
.. c:macro:: Py_CONSTANT_NONE ``0`` :py:data:`None` |
| 23 |
.. c:macro:: Py_CONSTANT_FALSE ``1`` :py:data:`False` |
| 24 |
.. c:macro:: Py_CONSTANT_TRUE ``2`` :py:data:`True` |
| 25 |
.. c:macro:: Py_CONSTANT_ELLIPSIS ``3`` :py:data:`Ellipsis` |
| 26 |
.. c:macro:: Py_CONSTANT_NOT_IMPLEMENTED ``4`` :py:data:`NotImplemented` |
| 27 |
.. c:macro:: Py_CONSTANT_ZERO ``5`` ``0`` |
| 28 |
.. c:macro:: Py_CONSTANT_ONE ``6`` ``1`` |
| 29 |
.. c:macro:: Py_CONSTANT_EMPTY_STR ``7`` ``''`` |
| 30 |
.. c:macro:: Py_CONSTANT_EMPTY_BYTES ``8`` ``b''`` |
| 31 |
.. c:macro:: Py_CONSTANT_EMPTY_TUPLE ``9`` ``()`` |
| 32 |
======================================== ===== ========================= |
| 33 |
|
| 34 |
Numeric values are only given for projects which cannot use the constant |
| 35 |
identifiers. |
| 36 |
|
| 37 |
|
| 38 |
.. versionadded:: 3.13 |
| 39 |
|
| 40 |
.. impl-detail:: |
| 41 |
|
| 42 |
In CPython, all of these constants are :term:`immortal`. |
| 43 |
|
| 44 |
|
| 45 |
.. c:function:: PyObject* Py_GetConstantBorrowed(unsigned int constant_id) |
| 46 |
|
| 47 |
Similar to :c:func:`Py_GetConstant`, but return a :term:`borrowed |
| 48 |
reference`. |
| 49 |
|
| 50 |
This function is primarily intended for backwards compatibility: |
| 51 |
using :c:func:`Py_GetConstant` is recommended for new code. |
| 52 |
|
| 53 |
The reference is borrowed from the interpreter, and is valid until the |
| 54 |
interpreter finalization. |
| 55 |
|
| 56 |
.. versionadded:: 3.13 |
| 57 |
|
| 58 |
|
| 59 |
.. c:var:: PyObject* Py_NotImplemented |
| 60 |
|
| 61 |
The ``NotImplemented`` singleton, used to signal that an operation is |
| 62 |
not implemented for the given type combination. |
| 63 |
|
| 64 |
|
| 65 |
.. c:macro:: Py_RETURN_NOTIMPLEMENTED |
| 66 |
|
| 67 |
Properly handle returning :c:data:`Py_NotImplemented` from within a C |
| 68 |
function (that is, create a new :term:`strong reference` |
| 69 |
to :const:`NotImplemented` and return it). |
| 70 |
|
| 71 |
|
| 72 |
.. c:macro:: Py_PRINT_RAW |
| 73 |
|
| 74 |
Flag to be used with multiple functions that print the object (like |
| 75 |
:c:func:`PyObject_Print` and :c:func:`PyFile_WriteObject`). |
| 76 |
If passed, these functions use the :func:`str` of the object |
| 77 |
instead of the :func:`repr`. |
| 78 |
|
| 79 |
|
| 80 |
.. c:function:: int PyObject_Print(PyObject *o, FILE *fp, int flags) |
| 81 |
|
| 82 |
Print an object *o*, on file *fp*. Returns ``-1`` on error. The flags argument |
| 83 |
is used to enable certain printing options. The only option currently supported |
| 84 |
is :c:macro:`Py_PRINT_RAW`; if given, the :func:`str` of the object is written |
| 85 |
instead of the :func:`repr`. |
| 86 |
|
| 87 |
|
| 88 |
.. c:function:: int PyObject_HasAttrWithError(PyObject *o, PyObject *attr_name) |
| 89 |
|
| 90 |
Returns ``1`` if *o* has the attribute *attr_name*, and ``0`` otherwise. |
| 91 |
This is equivalent to the Python expression ``hasattr(o, attr_name)``. |
| 92 |
On failure, return ``-1``. |
| 93 |
|
| 94 |
.. versionadded:: 3.13 |
| 95 |
|
| 96 |
|
| 97 |
.. c:function:: int PyObject_HasAttrStringWithError(PyObject *o, const char *attr_name) |
| 98 |
|
| 99 |
This is the same as :c:func:`PyObject_HasAttrWithError`, but *attr_name* is |
| 100 |
specified as a :c:expr:`const char*` UTF-8 encoded bytes string, |
| 101 |
rather than a :c:expr:`PyObject*`. |
| 102 |
|
| 103 |
.. versionadded:: 3.13 |
| 104 |
|
| 105 |
|
| 106 |
.. c:function:: int PyObject_HasAttr(PyObject *o, PyObject *attr_name) |
| 107 |
|
| 108 |
Returns ``1`` if *o* has the attribute *attr_name*, and ``0`` otherwise. |
| 109 |
This function always succeeds. |
| 110 |
|
| 111 |
.. note:: |
| 112 |
|
| 113 |
Exceptions that occur when this calls :meth:`~object.__getattr__` and |
| 114 |
:meth:`~object.__getattribute__` methods aren't propagated, |
| 115 |
but instead given to :func:`sys.unraisablehook`. |
| 116 |
For proper error handling, use :c:func:`PyObject_HasAttrWithError`, |
| 117 |
:c:func:`PyObject_GetOptionalAttr` or :c:func:`PyObject_GetAttr` instead. |
| 118 |
|
| 119 |
|
| 120 |
.. c:function:: int PyObject_HasAttrString(PyObject *o, const char *attr_name) |
| 121 |
|
| 122 |
This is the same as :c:func:`PyObject_HasAttr`, but *attr_name* is |
| 123 |
specified as a :c:expr:`const char*` UTF-8 encoded bytes string, |
| 124 |
rather than a :c:expr:`PyObject*`. |
| 125 |
|
| 126 |
.. note:: |
| 127 |
|
| 128 |
Exceptions that occur when this calls :meth:`~object.__getattr__` and |
| 129 |
:meth:`~object.__getattribute__` methods or while creating the temporary |
| 130 |
:class:`str` object are silently ignored. |
| 131 |
For proper error handling, use :c:func:`PyObject_HasAttrStringWithError`, |
| 132 |
:c:func:`PyObject_GetOptionalAttrString` |
| 133 |
or :c:func:`PyObject_GetAttrString` instead. |
| 134 |
|
| 135 |
|
| 136 |
.. c:function:: PyObject* PyObject_GetAttr(PyObject *o, PyObject *attr_name) |
| 137 |
|
| 138 |
Retrieve an attribute named *attr_name* from object *o*. Returns the attribute |
| 139 |
value on success, or ``NULL`` on failure. This is the equivalent of the Python |
| 140 |
expression ``o.attr_name``. |
| 141 |
|
| 142 |
If the missing attribute should not be treated as a failure, you can use |
| 143 |
:c:func:`PyObject_GetOptionalAttr` instead. |
| 144 |
|
| 145 |
|
| 146 |
.. c:function:: PyObject* PyObject_GetAttrString(PyObject *o, const char *attr_name) |
| 147 |
|
| 148 |
This is the same as :c:func:`PyObject_GetAttr`, but *attr_name* is |
| 149 |
specified as a :c:expr:`const char*` UTF-8 encoded bytes string, |
| 150 |
rather than a :c:expr:`PyObject*`. |
| 151 |
|
| 152 |
If the missing attribute should not be treated as a failure, you can use |
| 153 |
:c:func:`PyObject_GetOptionalAttrString` instead. |
| 154 |
|
| 155 |
|
| 156 |
.. c:function:: int PyObject_GetOptionalAttr(PyObject *obj, PyObject *attr_name, PyObject **result); |
| 157 |
|
| 158 |
Variant of :c:func:`PyObject_GetAttr` which doesn't raise |
| 159 |
:exc:`AttributeError` if the attribute is not found. |
| 160 |
|
| 161 |
If the attribute is found, return ``1`` and set *\*result* to a new |
| 162 |
:term:`strong reference` to the attribute. |
| 163 |
If the attribute is not found, return ``0`` and set *\*result* to ``NULL``; |
| 164 |
the :exc:`AttributeError` is silenced. |
| 165 |
If an error other than :exc:`AttributeError` is raised, return ``-1`` and |
| 166 |
set *\*result* to ``NULL``. |
| 167 |
|
| 168 |
.. versionadded:: 3.13 |
| 169 |
|
| 170 |
|
| 171 |
.. c:function:: int PyObject_GetOptionalAttrString(PyObject *obj, const char *attr_name, PyObject **result); |
| 172 |
|
| 173 |
This is the same as :c:func:`PyObject_GetOptionalAttr`, but *attr_name* is |
| 174 |
specified as a :c:expr:`const char*` UTF-8 encoded bytes string, |
| 175 |
rather than a :c:expr:`PyObject*`. |
| 176 |
|
| 177 |
.. versionadded:: 3.13 |
| 178 |
|
| 179 |
.. c:function:: PyObject* PyObject_GenericGetAttr(PyObject *o, PyObject *name) |
| 180 |
|
| 181 |
Generic attribute getter function that is meant to be put into a type |
| 182 |
object's ``tp_getattro`` slot. It looks for a descriptor in the dictionary |
| 183 |
of classes in the object's MRO as well as an attribute in the object's |
| 184 |
:attr:`~object.__dict__` (if present). As outlined in :ref:`descriptors`, |
| 185 |
data descriptors take preference over instance attributes, while non-data |
| 186 |
descriptors don't. Otherwise, an :exc:`AttributeError` is raised. |
| 187 |
|
| 188 |
|
| 189 |
.. c:function:: int PyObject_SetAttr(PyObject *o, PyObject *attr_name, PyObject *v) |
| 190 |
|
| 191 |
Set the value of the attribute named *attr_name*, for object *o*, to the value |
| 192 |
*v*. Raise an exception and return ``-1`` on failure; |
| 193 |
return ``0`` on success. This is the equivalent of the Python statement |
| 194 |
``o.attr_name = v``. |
| 195 |
|
| 196 |
If *v* is ``NULL``, the attribute is deleted. This behaviour is deprecated |
| 197 |
in favour of using :c:func:`PyObject_DelAttr`, but there are currently no |
| 198 |
plans to remove it. |
| 199 |
|
| 200 |
|
| 201 |
.. c:function:: int PyObject_SetAttrString(PyObject *o, const char *attr_name, PyObject *v) |
| 202 |
|
| 203 |
This is the same as :c:func:`PyObject_SetAttr`, but *attr_name* is |
| 204 |
specified as a :c:expr:`const char*` UTF-8 encoded bytes string, |
| 205 |
rather than a :c:expr:`PyObject*`. |
| 206 |
|
| 207 |
If *v* is ``NULL``, the attribute is deleted, but this feature is |
| 208 |
deprecated in favour of using :c:func:`PyObject_DelAttrString`. |
| 209 |
|
| 210 |
The number of different attribute names passed to this function |
| 211 |
should be kept small, usually by using a statically allocated string |
| 212 |
as *attr_name*. |
| 213 |
For attribute names that aren't known at compile time, prefer calling |
| 214 |
:c:func:`PyUnicode_FromString` and :c:func:`PyObject_SetAttr` directly. |
| 215 |
For more details, see :c:func:`PyUnicode_InternFromString`, which may be |
| 216 |
used internally to create a key object. |
| 217 |
|
| 218 |
.. c:function:: int PyObject_GenericSetAttr(PyObject *o, PyObject *name, PyObject *value) |
| 219 |
|
| 220 |
Generic attribute setter and deleter function that is meant |
| 221 |
to be put into a type object's :c:member:`~PyTypeObject.tp_setattro` |
| 222 |
slot. It looks for a data descriptor in the |
| 223 |
dictionary of classes in the object's MRO, and if found it takes preference |
| 224 |
over setting or deleting the attribute in the instance dictionary. Otherwise, the |
| 225 |
attribute is set or deleted in the object's :attr:`~object.__dict__` (if present). |
| 226 |
On success, ``0`` is returned, otherwise an :exc:`AttributeError` |
| 227 |
is raised and ``-1`` is returned. |
| 228 |
|
| 229 |
|
| 230 |
.. c:function:: int PyObject_DelAttr(PyObject *o, PyObject *attr_name) |
| 231 |
|
| 232 |
Delete attribute named *attr_name*, for object *o*. Returns ``-1`` on failure. |
| 233 |
This is the equivalent of the Python statement ``del o.attr_name``. |
| 234 |
|
| 235 |
|
| 236 |
.. c:function:: int PyObject_DelAttrString(PyObject *o, const char *attr_name) |
| 237 |
|
| 238 |
This is the same as :c:func:`PyObject_DelAttr`, but *attr_name* is |
| 239 |
specified as a :c:expr:`const char*` UTF-8 encoded bytes string, |
| 240 |
rather than a :c:expr:`PyObject*`. |
| 241 |
|
| 242 |
The number of different attribute names passed to this function |
| 243 |
should be kept small, usually by using a statically allocated string |
| 244 |
as *attr_name*. |
| 245 |
For attribute names that aren't known at compile time, prefer calling |
| 246 |
:c:func:`PyUnicode_FromString` and :c:func:`PyObject_DelAttr` directly. |
| 247 |
For more details, see :c:func:`PyUnicode_InternFromString`, which may be |
| 248 |
used internally to create a key object for lookup. |
| 249 |
|
| 250 |
|
| 251 |
.. c:function:: PyObject* PyObject_GenericGetDict(PyObject *o, void *context) |
| 252 |
|
| 253 |
A generic implementation for the getter of a ``__dict__`` descriptor. It |
| 254 |
creates the dictionary if necessary. |
| 255 |
|
| 256 |
This function may also be called to get the :py:attr:`~object.__dict__` |
| 257 |
of the object *o*. Pass ``NULL`` for *context* when calling it. |
| 258 |
Since this function may need to allocate memory for the |
| 259 |
dictionary, it may be more efficient to call :c:func:`PyObject_GetAttr` |
| 260 |
when accessing an attribute on the object. |
| 261 |
|
| 262 |
On failure, returns ``NULL`` with an exception set. |
| 263 |
|
| 264 |
.. versionadded:: 3.3 |
| 265 |
|
| 266 |
|
| 267 |
.. c:function:: int PyObject_GenericSetDict(PyObject *o, PyObject *value, void *context) |
| 268 |
|
| 269 |
A generic implementation for the setter of a ``__dict__`` descriptor. This |
| 270 |
implementation does not allow the dictionary to be deleted. |
| 271 |
|
| 272 |
.. versionadded:: 3.3 |
| 273 |
|
| 274 |
|
| 275 |
.. c:function:: PyObject** _PyObject_GetDictPtr(PyObject *obj) |
| 276 |
|
| 277 |
Return a pointer to :py:attr:`~object.__dict__` of the object *obj*. |
| 278 |
If there is no ``__dict__``, return ``NULL`` without setting an exception. |
| 279 |
|
| 280 |
This function may need to allocate memory for the |
| 281 |
dictionary, so it may be more efficient to call :c:func:`PyObject_GetAttr` |
| 282 |
when accessing an attribute on the object. |
| 283 |
|
| 284 |
|
| 285 |
.. c:function:: PyObject* PyObject_RichCompare(PyObject *o1, PyObject *o2, int opid) |
| 286 |
|
| 287 |
Compare the values of *o1* and *o2* using the operation specified by *opid*, |
| 288 |
which must be one of :c:macro:`Py_LT`, :c:macro:`Py_LE`, :c:macro:`Py_EQ`, |
| 289 |
:c:macro:`Py_NE`, :c:macro:`Py_GT`, or :c:macro:`Py_GE`, corresponding to ``<``, |
| 290 |
``<=``, ``==``, ``!=``, ``>``, or ``>=`` respectively. This is the equivalent of |
| 291 |
the Python expression ``o1 op o2``, where ``op`` is the operator corresponding |
| 292 |
to *opid*. Returns the value of the comparison on success, or ``NULL`` on failure. |
| 293 |
|
| 294 |
|
| 295 |
.. c:function:: int PyObject_RichCompareBool(PyObject *o1, PyObject *o2, int opid) |
| 296 |
|
| 297 |
Compare the values of *o1* and *o2* using the operation specified by *opid*, |
| 298 |
like :c:func:`PyObject_RichCompare`, but returns ``-1`` on error, ``0`` if |
| 299 |
the result is false, ``1`` otherwise. |
| 300 |
|
| 301 |
.. note:: |
| 302 |
If *o1* and *o2* are the same object, :c:func:`PyObject_RichCompareBool` |
| 303 |
will always return ``1`` for :c:macro:`Py_EQ` and ``0`` for :c:macro:`Py_NE`. |
| 304 |
|
| 305 |
.. c:function:: PyObject* PyObject_Format(PyObject *obj, PyObject *format_spec) |
| 306 |
|
| 307 |
Format *obj* using *format_spec*. This is equivalent to the Python |
| 308 |
expression ``format(obj, format_spec)``. |
| 309 |
|
| 310 |
*format_spec* may be ``NULL``. In this case the call is equivalent |
| 311 |
to ``format(obj)``. |
| 312 |
Returns the formatted string on success, ``NULL`` on failure. |
| 313 |
|
| 314 |
.. c:function:: PyObject* PyObject_Repr(PyObject *o) |
| 315 |
|
| 316 |
.. index:: pair: built-in function; repr |
| 317 |
|
| 318 |
Compute a string representation of object *o*. Returns the string |
| 319 |
representation on success, ``NULL`` on failure. This is the equivalent of the |
| 320 |
Python expression ``repr(o)``. Called by the :func:`repr` built-in function. |
| 321 |
|
| 322 |
.. versionchanged:: 3.4 |
| 323 |
This function now includes a debug assertion to help ensure that it |
| 324 |
does not silently discard an active exception. |
| 325 |
|
| 326 |
.. c:function:: PyObject* PyObject_ASCII(PyObject *o) |
| 327 |
|
| 328 |
.. index:: pair: built-in function; ascii |
| 329 |
|
| 330 |
As :c:func:`PyObject_Repr`, compute a string representation of object *o*, but |
| 331 |
escape the non-ASCII characters in the string returned by |
| 332 |
:c:func:`PyObject_Repr` with ``\x``, ``\u`` or ``\U`` escapes. This generates |
| 333 |
a string similar to that returned by :c:func:`PyObject_Repr` in Python 2. |
| 334 |
Called by the :func:`ascii` built-in function. |
| 335 |
|
| 336 |
.. index:: string; PyObject_Str (C function) |
| 337 |
|
| 338 |
|
| 339 |
.. c:function:: PyObject* PyObject_Str(PyObject *o) |
| 340 |
|
| 341 |
Compute a string representation of object *o*. Returns the string |
| 342 |
representation on success, ``NULL`` on failure. This is the equivalent of the |
| 343 |
Python expression ``str(o)``. Called by the :func:`str` built-in function |
| 344 |
and, therefore, by the :func:`print` function. |
| 345 |
|
| 346 |
.. versionchanged:: 3.4 |
| 347 |
This function now includes a debug assertion to help ensure that it |
| 348 |
does not silently discard an active exception. |
| 349 |
|
| 350 |
|
| 351 |
.. c:function:: PyObject* PyObject_Bytes(PyObject *o) |
| 352 |
|
| 353 |
.. index:: pair: built-in function; bytes |
| 354 |
|
| 355 |
Compute a bytes representation of object *o*. ``NULL`` is returned on |
| 356 |
failure and a bytes object on success. This is equivalent to the Python |
| 357 |
expression ``bytes(o)``, when *o* is not an integer. Unlike ``bytes(o)``, |
| 358 |
a TypeError is raised when *o* is an integer instead of a zero-initialized |
| 359 |
bytes object. |
| 360 |
|
| 361 |
|
| 362 |
.. c:function:: int PyObject_IsSubclass(PyObject *derived, PyObject *cls) |
| 363 |
|
| 364 |
Return ``1`` if the class *derived* is identical to or derived from the class |
| 365 |
*cls*, otherwise return ``0``. In case of an error, return ``-1``. |
| 366 |
|
| 367 |
If *cls* is a tuple, the check will be done against every entry in *cls*. |
| 368 |
The result will be ``1`` when at least one of the checks returns ``1``, |
| 369 |
otherwise it will be ``0``. |
| 370 |
|
| 371 |
If *cls* has a :meth:`~type.__subclasscheck__` method, it will be called to |
| 372 |
determine the subclass status as described in :pep:`3119`. Otherwise, |
| 373 |
*derived* is a subclass of *cls* if it is a direct or indirect subclass, |
| 374 |
i.e. contained in :attr:`cls.__mro__ <type.__mro__>`. |
| 375 |
|
| 376 |
Normally only class objects, i.e. instances of :class:`type` or a derived |
| 377 |
class, are considered classes. However, objects can override this by having |
| 378 |
a :attr:`~type.__bases__` attribute (which must be a tuple of base classes). |
| 379 |
|
| 380 |
|
| 381 |
.. c:function:: int PyObject_IsInstance(PyObject *inst, PyObject *cls) |
| 382 |
|
| 383 |
Return ``1`` if *inst* is an instance of the class *cls* or a subclass of |
| 384 |
*cls*, or ``0`` if not. On error, returns ``-1`` and sets an exception. |
| 385 |
|
| 386 |
If *cls* is a tuple, the check will be done against every entry in *cls*. |
| 387 |
The result will be ``1`` when at least one of the checks returns ``1``, |
| 388 |
otherwise it will be ``0``. |
| 389 |
|
| 390 |
If *cls* has a :meth:`~type.__instancecheck__` method, it will be called to |
| 391 |
determine the subclass status as described in :pep:`3119`. Otherwise, *inst* |
| 392 |
is an instance of *cls* if its class is a subclass of *cls*. |
| 393 |
|
| 394 |
An instance *inst* can override what is considered its class by having a |
| 395 |
:attr:`~object.__class__` attribute. |
| 396 |
|
| 397 |
An object *cls* can override if it is considered a class, and what its base |
| 398 |
classes are, by having a :attr:`~type.__bases__` attribute (which must be a tuple |
| 399 |
of base classes). |
| 400 |
|
| 401 |
|
| 402 |
.. c:function:: Py_hash_t PyObject_Hash(PyObject *o) |
| 403 |
|
| 404 |
.. index:: pair: built-in function; hash |
| 405 |
|
| 406 |
Compute and return the hash value of an object *o*. On failure, return ``-1``. |
| 407 |
This is the equivalent of the Python expression ``hash(o)``. |
| 408 |
|
| 409 |
.. versionchanged:: 3.2 |
| 410 |
The return type is now Py_hash_t. This is a signed integer the same size |
| 411 |
as :c:type:`Py_ssize_t`. |
| 412 |
|
| 413 |
|
| 414 |
.. c:function:: Py_hash_t PyObject_HashNotImplemented(PyObject *o) |
| 415 |
|
| 416 |
Set a :exc:`TypeError` indicating that ``type(o)`` is not :term:`hashable` and return ``-1``. |
| 417 |
This function receives special treatment when stored in a ``tp_hash`` slot, |
| 418 |
allowing a type to explicitly indicate to the interpreter that it is not |
| 419 |
hashable. |
| 420 |
|
| 421 |
|
| 422 |
.. c:function:: int PyObject_IsTrue(PyObject *o) |
| 423 |
|
| 424 |
Returns ``1`` if the object *o* is considered to be true, and ``0`` otherwise. |
| 425 |
This is equivalent to the Python expression ``not not o``. On failure, return |
| 426 |
``-1``. |
| 427 |
|
| 428 |
|
| 429 |
.. c:function:: int PyObject_Not(PyObject *o) |
| 430 |
|
| 431 |
Returns ``0`` if the object *o* is considered to be true, and ``1`` otherwise. |
| 432 |
This is equivalent to the Python expression ``not o``. On failure, return |
| 433 |
``-1``. |
| 434 |
|
| 435 |
|
| 436 |
.. c:function:: PyObject* PyObject_Type(PyObject *o) |
| 437 |
|
| 438 |
.. index:: pair: built-in function; type |
| 439 |
|
| 440 |
When *o* is non-``NULL``, returns a type object corresponding to the object type |
| 441 |
of object *o*. On failure, raises :exc:`SystemError` and returns ``NULL``. This |
| 442 |
is equivalent to the Python expression ``type(o)``. |
| 443 |
This function creates a new :term:`strong reference` to the return value. |
| 444 |
There's really no reason to use this |
| 445 |
function instead of the :c:func:`Py_TYPE()` function, which returns a |
| 446 |
pointer of type :c:expr:`PyTypeObject*`, except when a new |
| 447 |
:term:`strong reference` is needed. |
| 448 |
|
| 449 |
|
| 450 |
.. c:function:: int PyObject_TypeCheck(PyObject *o, PyTypeObject *type) |
| 451 |
|
| 452 |
Return non-zero if the object *o* is of type *type* or a subtype of *type*, and |
| 453 |
``0`` otherwise. Both parameters must be non-``NULL``. |
| 454 |
|
| 455 |
|
| 456 |
.. c:function:: Py_ssize_t PyObject_Size(PyObject *o) |
| 457 |
Py_ssize_t PyObject_Length(PyObject *o) |
| 458 |
|
| 459 |
.. index:: pair: built-in function; len |
| 460 |
|
| 461 |
Return the length of object *o*. If the object *o* provides either the sequence |
| 462 |
and mapping protocols, the sequence length is returned. On error, ``-1`` is |
| 463 |
returned. This is the equivalent to the Python expression ``len(o)``. |
| 464 |
|
| 465 |
|
| 466 |
.. c:function:: Py_ssize_t PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue) |
| 467 |
|
| 468 |
Return an estimated length for the object *o*. First try to return its |
| 469 |
actual length, then an estimate using :meth:`~object.__length_hint__`, and |
| 470 |
finally return the default value. On error return ``-1``. This is the |
| 471 |
equivalent to the Python expression ``operator.length_hint(o, defaultvalue)``. |
| 472 |
|
| 473 |
.. versionadded:: 3.4 |
| 474 |
|
| 475 |
|
| 476 |
.. c:function:: PyObject* PyObject_GetItem(PyObject *o, PyObject *key) |
| 477 |
|
| 478 |
Return element of *o* corresponding to the object *key* or ``NULL`` on failure. |
| 479 |
This is the equivalent of the Python expression ``o[key]``. |
| 480 |
|
| 481 |
|
| 482 |
.. c:function:: int PyObject_SetItem(PyObject *o, PyObject *key, PyObject *v) |
| 483 |
|
| 484 |
Map the object *key* to the value *v*. Raise an exception and |
| 485 |
return ``-1`` on failure; return ``0`` on success. This is the |
| 486 |
equivalent of the Python statement ``o[key] = v``. This function *does |
| 487 |
not* steal a reference to *v*. |
| 488 |
|
| 489 |
|
| 490 |
.. c:function:: int PyObject_DelItem(PyObject *o, PyObject *key) |
| 491 |
|
| 492 |
Remove the mapping for the object *key* from the object *o*. Return ``-1`` |
| 493 |
on failure. This is equivalent to the Python statement ``del o[key]``. |
| 494 |
|
| 495 |
|
| 496 |
.. c:function:: int PyObject_DelItemString(PyObject *o, const char *key) |
| 497 |
|
| 498 |
This is the same as :c:func:`PyObject_DelItem`, but *key* is |
| 499 |
specified as a :c:expr:`const char*` UTF-8 encoded bytes string, |
| 500 |
rather than a :c:expr:`PyObject*`. |
| 501 |
|
| 502 |
|
| 503 |
.. c:function:: PyObject* PyObject_Dir(PyObject *o) |
| 504 |
|
| 505 |
This is equivalent to the Python expression ``dir(o)``, returning a (possibly |
| 506 |
empty) list of strings appropriate for the object argument, or ``NULL`` if there |
| 507 |
was an error. If the argument is ``NULL``, this is like the Python ``dir()``, |
| 508 |
returning the names of the current locals; in this case, if no execution frame |
| 509 |
is active then ``NULL`` is returned but :c:func:`PyErr_Occurred` will return false. |
| 510 |
|
| 511 |
|
| 512 |
.. c:function:: PyObject* PyObject_GetIter(PyObject *o) |
| 513 |
|
| 514 |
This is equivalent to the Python expression ``iter(o)``. It returns a new |
| 515 |
iterator for the object argument, or the object itself if the object is already |
| 516 |
an iterator. Raises :exc:`TypeError` and returns ``NULL`` if the object cannot be |
| 517 |
iterated. |
| 518 |
|
| 519 |
|
| 520 |
.. c:function:: PyObject* PyObject_SelfIter(PyObject *obj) |
| 521 |
|
| 522 |
This is equivalent to the Python ``__iter__(self): return self`` method. |
| 523 |
It is intended for :term:`iterator` types, to be used in the :c:member:`PyTypeObject.tp_iter` slot. |
| 524 |
|
| 525 |
|
| 526 |
.. c:function:: PyObject* PyObject_GetAIter(PyObject *o) |
| 527 |
|
| 528 |
This is the equivalent to the Python expression ``aiter(o)``. Takes an |
| 529 |
:class:`AsyncIterable` object and returns an :class:`AsyncIterator` for it. |
| 530 |
This is typically a new iterator but if the argument is an |
| 531 |
:class:`AsyncIterator`, this returns itself. Raises :exc:`TypeError` and |
| 532 |
returns ``NULL`` if the object cannot be iterated. |
| 533 |
|
| 534 |
.. versionadded:: 3.10 |
| 535 |
|
| 536 |
.. c:function:: void *PyObject_GetTypeData(PyObject *o, PyTypeObject *cls) |
| 537 |
|
| 538 |
Get a pointer to subclass-specific data reserved for *cls*. |
| 539 |
|
| 540 |
The object *o* must be an instance of *cls*, and *cls* must have been |
| 541 |
created using negative :c:member:`PyType_Spec.basicsize`. |
| 542 |
Python does not check this. |
| 543 |
|
| 544 |
On error, set an exception and return ``NULL``. |
| 545 |
|
| 546 |
.. versionadded:: 3.12 |
| 547 |
|
| 548 |
.. c:function:: Py_ssize_t PyType_GetTypeDataSize(PyTypeObject *cls) |
| 549 |
|
| 550 |
Return the size of the instance memory space reserved for *cls*, i.e. the size of the |
| 551 |
memory :c:func:`PyObject_GetTypeData` returns. |
| 552 |
|
| 553 |
This may be larger than requested using :c:member:`-PyType_Spec.basicsize <PyType_Spec.basicsize>`; |
| 554 |
it is safe to use this larger size (e.g. with :c:func:`!memset`). |
| 555 |
|
| 556 |
The type *cls* **must** have been created using |
| 557 |
negative :c:member:`PyType_Spec.basicsize`. |
| 558 |
Python does not check this. |
| 559 |
|
| 560 |
On error, set an exception and return a negative value. |
| 561 |
|
| 562 |
.. versionadded:: 3.12 |
| 563 |
|
| 564 |
.. c:function:: void *PyObject_GetItemData(PyObject *o) |
| 565 |
|
| 566 |
Get a pointer to per-item data for a class with |
| 567 |
:c:macro:`Py_TPFLAGS_ITEMS_AT_END`. |
| 568 |
|
| 569 |
On error, set an exception and return ``NULL``. |
| 570 |
:py:exc:`TypeError` is raised if *o* does not have |
| 571 |
:c:macro:`Py_TPFLAGS_ITEMS_AT_END` set. |
| 572 |
|
| 573 |
.. versionadded:: 3.12 |
| 574 |
|
| 575 |
.. c:function:: int PyObject_VisitManagedDict(PyObject *obj, visitproc visit, void *arg) |
| 576 |
|
| 577 |
Visit the managed dictionary of *obj*. |
| 578 |
|
| 579 |
This function must only be called in a traverse function of the type which |
| 580 |
has the :c:macro:`Py_TPFLAGS_MANAGED_DICT` flag set. |
| 581 |
|
| 582 |
.. versionadded:: 3.13 |
| 583 |
|
| 584 |
.. c:function:: void PyObject_ClearManagedDict(PyObject *obj) |
| 585 |
|
| 586 |
Clear the managed dictionary of *obj*. |
| 587 |
|
| 588 |
This function must only be called in a clear function of the type which |
| 589 |
has the :c:macro:`Py_TPFLAGS_MANAGED_DICT` flag set. |
| 590 |
|
| 591 |
.. versionadded:: 3.13 |