这个 AttributeError 消息在 CPython 中在哪里实现?
Where is this AttributeError message implemented in CPython?
我认为 Python 会话中的 AttributeError
消息
>>> class A: pass
...
>>> A().x
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'A' object has no attribute 'x'
在函数_PyObject_GenericGetAttrWithDict
at these lines in CPython:
中实现
if (!suppress) {
PyErr_Format(PyExc_AttributeError,
"'%.50s' object has no attribute '%U'",
tp->tp_name, name);
}
但是我找不到 AttributeError
消息在此 Python 会话中的位置
>>> class A: __slots__ = ('x',)
...
>>> A().x
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: x
在CPython中实现。您能否为 GitHub 存储库中的确切行提供 link?
是here:
case T_OBJECT_EX:
v = *(PyObject **)addr;
if (v == NULL)
PyErr_SetString(PyExc_AttributeError, l->name);
Py_XINCREF(v);
break;
这是 PyMember_GetOne
的摘录,其中实现了插槽描述符的 __get__
方法的大部分逻辑。
您可能还对该描述符如何到达那里感兴趣。 type.__new__
内部 translates __slots__
to an array of PyMemberDef
structs, one of the mechanisms classes written in C use to define access to their internal data. All PyMemberDef
structs generated this way are marked T_OBJECT_EX
, meaning they correspond to a PyObject *
in the instance memory layout, and if the pointer is null, access should raise an AttributeError. PyType_Ready
then generates 基于 PyMemberDef
数组的插槽描述符。
我认为 Python 会话中的 AttributeError
消息
>>> class A: pass
...
>>> A().x
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'A' object has no attribute 'x'
在函数_PyObject_GenericGetAttrWithDict
at these lines in CPython:
if (!suppress) {
PyErr_Format(PyExc_AttributeError,
"'%.50s' object has no attribute '%U'",
tp->tp_name, name);
}
但是我找不到 AttributeError
消息在此 Python 会话中的位置
>>> class A: __slots__ = ('x',)
...
>>> A().x
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: x
在CPython中实现。您能否为 GitHub 存储库中的确切行提供 link?
是here:
case T_OBJECT_EX:
v = *(PyObject **)addr;
if (v == NULL)
PyErr_SetString(PyExc_AttributeError, l->name);
Py_XINCREF(v);
break;
这是 PyMember_GetOne
的摘录,其中实现了插槽描述符的 __get__
方法的大部分逻辑。
您可能还对该描述符如何到达那里感兴趣。 type.__new__
内部 translates __slots__
to an array of PyMemberDef
structs, one of the mechanisms classes written in C use to define access to their internal data. All PyMemberDef
structs generated this way are marked T_OBJECT_EX
, meaning they correspond to a PyObject *
in the instance memory layout, and if the pointer is null, access should raise an AttributeError. PyType_Ready
then generates 基于 PyMemberDef
数组的插槽描述符。