Object Protocol¶
-
PyObject *Py_GetConstant(unsigned int constant_id)¶
- Part of the Stable ABI since version 3.13.
Get a strong reference to a constant.
Set an exception and return
NULLif constant_id is invalid.constant_id must be one of these constant identifiers:
Constant Identifier
Value
Returned object
-
Py_CONSTANT_NONE¶
0-
Py_CONSTANT_FALSE¶
1-
Py_CONSTANT_TRUE¶
2-
Py_CONSTANT_ELLIPSIS¶
3-
Py_CONSTANT_NOT_IMPLEMENTED¶
4-
Py_CONSTANT_ZERO¶
50-
Py_CONSTANT_ONE¶
61-
Py_CONSTANT_EMPTY_STR¶
7''-
Py_CONSTANT_EMPTY_BYTES¶
8b''-
Py_CONSTANT_EMPTY_TUPLE¶
9()Numeric values are only given for projects which cannot use the constant identifiers.
Added in version 3.13.
CPython implementation detail: In CPython, all of these constants are immortal.
-
Py_CONSTANT_NONE¶
-
PyObject *Py_GetConstantBorrowed(unsigned int constant_id)¶
- Part of the Stable ABI since version 3.13.
Similar to
Py_GetConstant(), but return a borrowed reference.This function is primarily intended for backwards compatibility: using
Py_GetConstant()is recommended for new code.The reference is borrowed from the interpreter, and is valid until the interpreter finalization.
Added in version 3.13.
-
PyObject *Py_NotImplemented¶
The
NotImplementedsingleton, used to signal that an operation is not implemented for the given type combination.
-
Py_RETURN_NOTIMPLEMENTED¶
Properly handle returning
Py_NotImplementedfrom within a C function (that is, create a new strong reference toNotImplementedand return it).
-
Py_PRINT_RAW¶
Flag to be used with multiple functions that print the object (like
PyObject_Print()andPyFile_WriteObject()). If passed, these functions use thestr()of the object instead of therepr().
-
int PyObject_Print(PyObject *o, FILE *fp, int flags)¶
Print an object o, on file fp. Returns
-1on error. The flags argument is used to enable certain printing options. The only option currently supported isPy_PRINT_RAW; if given, thestr()of the object is written instead of therepr().
-
void PyObject_Dump(PyObject *op)¶
Dump an object op to
stderr. This should only be used for debugging.The output is intended to try dumping objects even after memory corruption:
Information is written starting with fields that are the least likely to crash when accessed.
This function can be called without an attached thread state, but it’s not recommended to do so: it can cause deadlocks.
An object that does not belong to the current interpreter may be dumped, but this may also cause crashes or unintended behavior.
Implement a heuristic to detect if the object memory has been freed. Don’t display the object contents in this case, only its memory address.
The output format may change at any time.
Example of output:
object address : 0x7f80124702c0 object refcount : 2 object type : 0x9902e0 object type name: str object repr : 'abcdef'
Added in version 3.15.
-
int PyObject_HasAttrWithError(PyObject *o, PyObject *attr_name)¶
- Part of the Stable ABI since version 3.13.
Returns
1if o has the attribute attr_name, and0otherwise. This is equivalent to the Python expressionhasattr(o, attr_name). On failure, return-1.Added in version 3.13.
-
int PyObject_HasAttrStringWithError(PyObject *o, const char *attr_name)¶
- Part of the Stable ABI since version 3.13.
This is the same as
PyObject_HasAttrWithError(), but attr_name is specified as a const char* UTF-8 encoded bytes string, rather than a PyObject*.Added in version 3.13.
-
int PyObject_HasAttr(PyObject *o, PyObject *attr_name)¶
- Part of the Stable ABI.
Returns
1if o has the attribute attr_name, and0otherwise. This function always succeeds.Note
Exceptions that occur when this calls
__getattr__()and__getattribute__()methods aren’t propagated, but instead given tosys.unraisablehook(). For proper error handling, usePyObject_HasAttrWithError(),PyObject_GetOptionalAttr()orPyObject_GetAttr()instead.
-
int PyObject_HasAttrString(PyObject *o, const char *attr_name)¶
- Part of the Stable ABI.
This is the same as
PyObject_HasAttr(), but attr_name is specified as a const char* UTF-8 encoded bytes string, rather than a PyObject*.Note
Exceptions that occur when this calls
__getattr__()and__getattribute__()methods or while creating the temporarystrobject are silently ignored. For proper error handling, usePyObject_HasAttrStringWithError(),PyObject_GetOptionalAttrString()orPyObject_GetAttrString()instead.
-
PyObject *PyObject_GetAttr(PyObject *o, PyObject *attr_name)¶
- Return value: New reference. Part of the Stable ABI.
Retrieve an attribute named attr_name from object o. Returns the attribute value on success, or
NULLon failure. This is the equivalent of the Python expressiono.attr_name.If the missing attribute should not be treated as a failure, you can use
PyObject_GetOptionalAttr()instead.
-
PyObject *PyObject_GetAttrString(PyObject *o, const char *attr_name)¶
- Return value: New reference. Part of the Stable ABI.
This is the same as
PyObject_GetAttr(), but attr_name is specified as a const char* UTF-8 encoded bytes string, rather than a PyObject*.If the missing attribute should not be treated as a failure, you can use
PyObject_GetOptionalAttrString()instead.
-
int PyObject_GetOptionalAttr(PyObject *obj, PyObject *attr_name, PyObject **result);¶
- Part of the Stable ABI since version 3.13.
Variant of
PyObject_GetAttr()which doesn’t raiseAttributeErrorif the attribute is not found.If the attribute is found, return
1and set *result to a new strong reference to the attribute. If the attribute is not found, return0and set *result toNULL; theAttributeErroris silenced. If an error other thanAttributeErroris raised, return-1and set *result toNULL.Added in version 3.13.
-
int PyObject_GetOptionalAttrString(PyObject *obj, const char *attr_name, PyObject **result);¶
- Part of the Stable ABI since version 3.13.
This is the same as
PyObject_GetOptionalAttr(), but attr_name is specified as a const char* UTF-8 encoded bytes string, rather than a PyObject*.Added in version 3.13.
- PyObject *PyObject_GenericGetAttr(