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(PyObject *o, PyObject *name)¶
- Return value: New reference. Part of the Stable ABI.
Generic attribute getter function that is meant to be put into a type object’s
tp_getattroslot. It looks for a descriptor in the dictionary of classes in the object’s MRO as well as an attribute in the object’s__dict__(if present). As outlined in Implementing Descriptors, data descriptors take preference over instance attributes, while non-data descriptors don’t. Otherwise, anAttributeErroris raised.
-
int PyObject_SetAttr(PyObject *o, PyObject *attr_name, PyObject *v)¶
- Part of the Stable ABI.
Set the value of the attribute named attr_name, for object o, to the value v. Raise an exception and return
-1on failure; return0on success. This is the equivalent of the Python statemento.attr_name = v.If v is
NULL, the attribute is deleted. This behaviour is deprecated in favour of usingPyObject_DelAttr(), but there are currently no plans to remove it.The function must not be called with a
NULLv and an exception set. This case can arise from forgettingNULLchecks and would delete the attribute.Changed in version 3.15: Must not be called with NULL value if an exception is set.
-
int PyObject_SetAttrString(PyObject *o, const char *attr_name, PyObject *v)¶
- Part of the Stable ABI.
This is the same as
PyObject_SetAttr(), but attr_name is specified as a const char* UTF-8 encoded bytes string, rather than a PyObject*.If v is
NULL, the attribute is deleted, but this feature is deprecated in favour of usingPyObject_DelAttrString().The function must not be called with a
NULLv and an exception set. This case can arise from forgettingNULLchecks and would delete the attribute.The number of different attribute names passed to this function should be kept small, usually by using a statically allocated string as attr_name. For attribute names that aren’t known at compile time, prefer calling
PyUnicode_FromString()andPyObject_SetAttr()directly. For more details, see