此AttributeError消息在CPython中在哪里实现? [英] Where is this AttributeError message implemented in CPython?
问题描述
我认为此Python会话中的 AttributeError
消息
I think that the AttributeError
message in this Python session
>>> class A: pass
...
>>> A().x
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'A' object has no attribute 'x'
在
is implemented in the function _PyObject_GenericGetAttrWithDict
at these lines in CPython:
if (!suppress) {
PyErr_Format(PyExc_AttributeError,
"'%.50s' object has no attribute '%U'",
tp->tp_name, name);
}
但是我找不到此Python会话中 AttributeError
消息的位置
However I cannot find where the AttributeError
message in this Python session
>>> class A: __slots__ = ('x',)
...
>>> A().x
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: x
在CPython中实现.您能否提供指向GitHub存储库中确切行的链接?
is implemented in CPython. Could you provide a link to the exact lines in the GitHub repository?
推荐答案
它是此处:
case T_OBJECT_EX:
v = *(PyObject **)addr;
if (v == NULL)
PyErr_SetString(PyExc_AttributeError, l->name);
Py_XINCREF(v);
break;
That's an excerpt from PyMember_GetOne
, which is where most of the logic for the __get__
method of the slot descriptor is implemented.
您可能还对该描述符如何到达那里感兴趣.内部 type .__ new __
将 __ slots __
转换为生成基于 PyMemberDef
数组的广告位描述符.
You might also be interested in how that descriptor gets there. type.__new__
internally 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 the slot descriptors based on the PyMemberDef
array.
这篇关于此AttributeError消息在CPython中在哪里实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!