对象协议¶
-
Py_RETURN_NOTIMPLEMENTED¶
正确处理从 C 语言函数中返回
Py_NotImplemented的问题(即新建一个指向 NotImplemented 的 strong reference 并返回它)。
-
int PyObject_Print(PyObject *o, FILE *fp, int flags)¶
打印对象 o 到文件 fp。 出错时返回
-1。 flags 参数被用于启用特定的打印选项。 目前唯一支持的选项是Py_PRINT_RAW;如果给出该选项,则将写入对象的str()而不是repr()。
-
int PyObject_HasAttrWithError(PyObject *o, const char *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.在 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*.在 3.13 版本加入.
-
int PyObject_HasAttr(PyObject *o, PyObject *attr_name)¶
- Part of the Stable ABI.
如果 o 带有属性 attr_name,则返回
1,否则返回0。这相当于 Python 表达式hasattr(o, attr_name)。 此函数总是成功。备注
Exceptions that occur when this calls
__getattr__()and__getattribute__()methods are silently ignored. 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.
这与
PyObject_HasAttr()相同,但 attr_name 被指定为 const char* UTF-8 编码的字节串,而不是 PyObject*。备注
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)¶
- 返回值:新的引用。 Part of the Stable ABI.
从对象 o 中读取名为 attr_name 的属性。成功返回属性值,失败则返回
NULL。 这相当于 Python 表达式o.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)¶
- 返回值:新的引用。 Part of the Stable ABI.
这与
PyObject_GetAttr()相同,但 attr_name 被指定为 const char* UTF-8 编码的字节串,而不是 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.在 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*.在 3.13 版本加入.
-
PyObject *PyObject_GenericGetAttr(PyObject *o, PyObject *name)¶
- 返回值:新的引用。 Part of the Stable ABI.
通用的属性获取函数,用于放入类型对象的
tp_getattro槽中。它在类的字典中(位于对象的 MRO 中)查找某个描述符,并在对象的__dict__中查找某个属性。正如 实现描述器 所述,数据描述符优先于实例属性,而非数据描述符则不优先。失败则会触发AttributeError。
-
int PyObject_SetAttr(PyObject *o, PyObject *attr_name, PyObject *v)¶
- Part of the Stable ABI.
将对象 o 中名为 attr_name 的属性值设为 v 。失败时引发异常并返回
-1;成功时返 回0。这相当于 Python 语句o.attr_name = v。如果 v 为
NULL,该属性将被删除。 此行为已被弃用而应改用PyObject_DelAttr(),但目前还没有移除它的计划。
-
int PyObject_SetAttrString(PyObject *o, const char *attr_name, PyObject *v)¶
- Part of the Stable ABI.
这与
PyObject_SetAttr()相同,但 attr_name 被指定为 const char* UTF-8 编码的字节串,而不是 PyObject*。如果 v 为
NULL,该属性将被删除,但是此功能已被弃用而应改用PyObject_DelAttrString()。
-
int PyObject_GenericSetAttr(PyObject *o, PyObject *name, PyObject *value)¶
- Part of the Stable ABI.
通用的属性设置和删除函数,用于放入类型对象的
tp_setattro槽。它在类的字典中(位于对象的MRO中)查找数据描述器,如果找到,则将比在实例字典中设置或删除属性优先执行。否则,该属性将在对象的__dict__中设置或删除。如果成功将返回0,否则将引发AttributeError并返回-1。
-
int PyObject_DelAttr(PyObject *o, PyObject *attr_name)¶
- Part of the Stable ABI since version 3.13.
删除对象 o 中名为 attr_name 的属性。失败时返回
-1。这相当于 Python 语句del o.attr_name。
-
int PyObject_DelAttrString(PyObject *o, const char *attr_name)¶
- Part of the Stable ABI since version 3.13.
这与
PyObject_DelAttr()相同,但 attr_name 被指定为 const char* UTF-8 编码的字节串,而不是 PyObject*。
-
PyObject *PyObject_GenericGetDict(PyObject *o, void *context)¶
- 返回值:新的引用。 Part of the Stable ABI since version 3.10.
__dict__描述符的获取函数的一种通用实现。必要时会创建该字典。此函数还可能会被调用以获取对象 o 的
__dict__。 当调用它时可传入NULL作为 context。 由于此函数可能需要为字典分配内存,所以在访问对象上的属性时调用PyObject_GetAttr()可能会更为高效。当失败时,将返回
NULL并设置一个异常。在 3.3 版本加入.
-
int PyObject_GenericSetDict(PyObject *o, PyObject *value, void *context)¶
- Part of the Stable ABI since version 3.7.
__dict__描述符设置函数的一种通用实现。这里不允许删除该字典。在 3.3 版本加入.
-
PyObject **_PyObject_GetDictPtr(PyObject *obj)¶
返回一个指向对象 obj 的
__dict__的指针。 如果不存在__dict__,则返回NULL并且不设置异常。此函数可能需要为字典分配内存,所以在访问对象上的属性时调用
PyObject_GetAttr()可能会更为高效。
-
PyObject *PyObject_RichCompare(PyObject *o1, PyObject *o2, int opid)¶
- 返回值:新的引用。 Part of the Stable ABI.
使用由 opid 指定的操作来比较 o1 和 o2 的值,操作必须为
Py_LT,Py_LE,Py_EQ,Py_NE,Py_GT或Py_GE中的一个,分别对应于<,<=,==,!=,>或>=。 这等价于 Python 表达式o1 op o2,其中op是与 opid 对应的运算符。 成功时返回比较结果值,失败时返回NULL。
-
int PyObject_RichCompareBool(PyObject *o1, PyObject *o2, int opid)¶
- Part of the Stable ABI.
使用由 opid 指定的操作来比较 o1 和 o2 的值,操作必须为
Py_LT,Py_LE,Py_EQ,Py_NE,Py_GT或Py_GE中的一个,分别对应于<,<=,==,!=,>或>=。 失败时返回-1,结果为假值时返回0,否则返回1。 这等价于 Python 表达式o1 op o2,其中op是与 opid 对应的运算符。
备注
如果 o1 和 o2 是同一个对象,PyObject_RichCompareBool() 将总是为 Py_EQ 返回 1 并为 Py_NE 返回 0。
-
PyObject *PyObject_Format(PyObject *obj, PyObject *format_spec)¶
- Part of the Stable ABI.
格式 obj 使用 format_spec。 这等价于 Python 表达式
format(obj, format_spec)。format_spec 可以为
NULL。 在此情况下调用将等价于format(obj)。 成功时返回已格式化的字符串,失败时返回NULL。
-
PyObject *PyObject_Repr(PyObject *o)¶
- 返回值:新的引用。 Part of the Stable ABI.
计算对象 o 的字符串形式。 成功时返回字符串,失败时返回
NULL。 这相当于 Python 表达式repr(o)。 由内置函数repr()调用。在 3.4 版本发生变更: 该函数现在包含一个调试断言,用以确保不会静默地丢弃活动的异常。
-
PyObject *PyObject_ASCII(PyObject *o)¶
- 返回值:新的引用。 Part of the Stable ABI.
与
PyObject_Repr()一样,计算对象 o 的字符串形式,但在PyObject_Repr()返回的字符串中用\x、\u或\U转义非 ASCII 字符。这将生成一个类似于 Python 2 中由PyObject_Repr()返回的字符串。由内置函数ascii()调用。
-
PyObject *PyObject_Str(PyObject *o)¶
- 返回值:新的引用。 Part of the Stable ABI.
计算对象 o 的字符串形式。 成功时返回字符串,失败时返回
NULL。 这相当于 Python 表达式str(o)。由内置函数str()调用,因此也由print()函数调用。在 3.4 版本发生变更: 该函数现在包含一个调试断言,用以确保不会静默地丢弃活动的异常。
-
PyObject *PyObject_Bytes(PyObject *o)¶
- 返回值:新的引用。 Part of the Stable ABI.
计算对象 o 的字节形式。失败时返回
NULL,成功时返回一个字节串对象。这相当于 o 不是整数时的 Python 表达式bytes(o)。与bytes(o)不同的是,当 o 是整数而不是初始为 0 的字节串对象时,会触发 TypeError。
-
int PyObject_IsSubclass(PyObject *derived, PyObject *cls)¶
- Part of the Stable ABI.
如果 derived 类与 cls 类相同或为其派生类,则返回
1,否则返回0。 如果出错则返回-1。如果 cls 是元组,则会对 cls 进行逐项检测。如果至少有一次检测返回
1,结果将为1,否则将是0。正如 PEP 3119 所述,如果 cls 带有
__subclasscheck__()方法,将会被调用以确定子类的状态。 否则,如果 derived 是个直接或间接子类,即包含在cls.__mro__中,那么它就是 cls 的一个子类。通常只有类对象(即
type或派生类的实例)才被视为类。 但是,对象可以通过设置__bases__属性(必须是基类的元组)来覆盖这一点。
-
int PyObject_IsInstance(PyObject *inst, PyObject *cls)¶
- Part of the Stable ABI.
如果 inst 是 cls 类或其子类的实例,则返回
1,如果不是则返回0。 如果出错则返回-1并设置一个异常。如果 cls 是元组,则会对 cls 进行逐项检测。如果至少有一次检测返回
1,结果将为1,否则将是0。正如 PEP 3119 所述,如果 cls 带有
__subclasscheck__()方法,将会被调用以确定子类的状态。 否则,如果 derived 是 cls 的子类,那么它就是 cls 的一个实例。实例 inst 可以通过
__class__属性来覆盖其所属的类。对象 cls 可以通过设置
__bases__属性(该属性必须是基类的元组)来覆盖其是否会被视为类,及其有哪些基类。
-
Py_hash_t PyObject_Hash(PyObject *o)¶
- Part of the Stable ABI.
计算并返回对象的哈希值 o。 失败时返回
-1。这相当于 Python 表达式hash(o)。在 3.2 版本发生变更: 现在的返回类型是 Py_hash_t。 这是一个大小与
Py_ssize_t相同的有符号整数。
-
Py_hash_t PyObject_HashNotImplemented(PyObject *o)¶
- Part of the Stable ABI.
设置一个
TypeError来指明type(o)不是 hashable 并返回-1。 此函数在存储于tp_hash槽位内时会获得特别对待,允许某个类型显式地向解释器指明它是不可哈希对象。
-
int PyObject_IsTrue(PyObject *o)¶
- Part of the Stable ABI.
如果对象 o 被认为是 true,则返回
1,否则返回0。这相当于 Python 表达式not not o。 失败则返回-1。
-
int PyObject_Not(PyObject *o)¶
- Part of the Stable ABI.
如果对象 o 被认为是 true,则返回
1,否则返回0。这相当于 Python 表达式not not o。 失败则返回-1。
-
PyObject *PyObject_Type(PyObject *o)¶
- 返回值:新的引用。 Part of the Stable ABI.
当 o 不为
NULL时,返回一个与对象 o 的类型相对应的类型对象。 当失败时,将引发SystemError并返回NULL。 这等同于 Python 表达式type(o)。 该函数会新建一个指向返回值的 strong reference。 实际上没有多少理由使用此函数来替代Py_TYPE()函数,后者将返回一个 PyTypeObject* 类型的指针,除非是需要一个新的 strong reference。
-
int PyObject_TypeCheck(PyObject *o, PyTypeObject *type)¶
如果对象 o 是 type 类型或其子类型,则返回非零,否则返回
0。两个参数都必须非NULL。
-
Py_ssize_t PyObject_Size(PyObject *o)¶
-
Py_ssize_t PyObject_Length(PyObject *o)¶
- Part of the Stable ABI.
返回对象 o 的长度。 如果对象 o 支持序列和映射协议,则返回序列长度。 出错时返回
-1。这等同于 Python 表达式len(o)。
-
Py_ssize_t PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue)¶
返回对象 o 的估计长度。首先尝试返回实际长度,然后用
__length_hint__()进行估计,最后返回默认值。出错时返回-1。这等同于 Python 表达式operator.length_hint(o, defaultvalue)。在 3.4 版本加入.
-
PyObject *PyObject_GetItem(PyObject *o, PyObject *key)¶
- 返回值:新的引用。 Part of the Stable ABI.
返回对象 key 对应的 o 元素,或在失败时返回
NULL。这等同于 Python 表达式o[key]。
-
int PyObject_SetItem(PyObject *o, PyObject *key, PyObject *v)¶
- Part of the Stable ABI.
将对象 key 映射到值 v。 失败时引发异常并返回
-1;成功时返回0。 这相当于 Python 语句o[key] = v。该函数 不会 偷取 v 的引用计数。
-
int PyObject_DelItem(PyObject *o, PyObject *key)¶
- Part of the Stable ABI.
从对象 o 中移除对象 key 的映射。失败时返回
-1。 这相当于 Python 语句del o[key]。
-
PyObject *PyObject_Dir(PyObject *o)¶
- 返回值:新的引用。 Part of the Stable ABI.
相当于 Python 表达式
dir(o),返回一个(可能为空)适合对象参数的字符串列表,如果出错则返回NULL。 如果参数为NULL,类似 Python 的dir(),则返回当前 locals 的名字;这时如果没有活动的执行框架,则返回NULL,但PyErr_Occurred()将返回 false。
-
PyObject *PyObject_GetIter(PyObject *o)¶
- 返回值:新的引用。 Part of the Stable ABI.
等同于 Python 表达式
iter(o)。为对象参数返回一个新的迭代器,如果该对象已经是一个迭代器,则返回对象本身。如果对象不能被迭代,会引发TypeError,并返回NULL。
-
PyObject *PyObject_GetAIter(PyObject *o)¶
- 返回值:新的引用。 Part of the Stable ABI since version 3.10.
等同于 Python 表达式
aiter(o)。接受一个AsyncIterable对象,并为其返回一个AsyncIterator。通常返回的是一个新迭代器,但如果参数是一个AsyncIterator,将返回其自身。如果该对象不能被迭代,会引发TypeError,并返回NULL。在 3.10 版本加入.
-
void *PyObject_GetTypeData(PyObject *o, PyTypeObject *cls)¶
- Part of the Stable ABI since version 3.12.
获取一个指向为 cls 保留的子类专属数据的指针。
对象 o 必须为 cls 的实例,而 cls 必须使用负的
PyType_Spec.basicsize来创建。 Python 不会检查这一点。发生错误时,将设置异常并返回
NULL。在 3.12 版本加入.
-
Py_ssize_t PyType_GetTypeDataSize(PyTypeObject *cls)¶
- Part of the Stable ABI since version 3.12.
返回为 cls 保留的实例内存空间大小,即
PyObject_GetTypeData()所返回的内存大小。这可能会大于使用
-PyType_Spec.basicsize请求到的大小;可以安全地使用这个更大的值 (例如通过memset())。类型 cls 必须 使用负的
PyType_Spec.basicsize来创建。 Python 不会检查这一点。当失败时,将设置异常并返回一个负值。
在 3.12 版本加入.
-
void *PyObject_GetItemData(PyObject *o)¶
使用
Py_TPFLAGS_ITEMS_AT_END获取一个指向类的单独条目数据的指针。出错时,将设置异常并返回
NULL。 如果 o 没有设置Py_TPFLAGS_ITEMS_AT_END则会引发TypeError。在 3.12 版本加入.
-
int PyObject_VisitManagedDict(PyObject *obj, visitproc visit, void *arg)¶
Visit the managed dictionary of obj.
This function must only be called in a traverse function of the type which has the
Py_TPFLAGS_MANAGED_DICTflag set.在 3.13 版本加入.
-
void PyObject_ClearManagedDict(PyObject *obj)¶
Clear the managed dictionary of obj.
This function must only be called in a traverse function of the type which has the
Py_TPFLAGS_MANAGED_DICTflag set.在 3.13 版本加入.