mirror of
https://github.com/python/cpython.git
synced 2025-08-24 02:35:59 +00:00
bpo-43857: Improve the AttributeError message when deleting a missing attribute (#25424)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
This commit is contained in:
parent
43b135f94e
commit
a95138b2c5
4 changed files with 62 additions and 5 deletions
|
@ -1382,7 +1382,7 @@ _PyObject_GenericSetAttrWithDict(PyObject *obj, PyObject *name,
|
|||
return -1;
|
||||
|
||||
Py_INCREF(name);
|
||||
|
||||
Py_INCREF(tp);
|
||||
descr = _PyType_Lookup(tp, name);
|
||||
|
||||
if (descr != NULL) {
|
||||
|
@ -1426,11 +1426,21 @@ _PyObject_GenericSetAttrWithDict(PyObject *obj, PyObject *name,
|
|||
res = PyDict_SetItem(dict, name, value);
|
||||
Py_DECREF(dict);
|
||||
}
|
||||
if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
|
||||
PyErr_SetObject(PyExc_AttributeError, name);
|
||||
|
||||
if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError)) {
|
||||
if (PyType_IsSubtype(tp, &PyType_Type)) {
|
||||
PyErr_Format(PyExc_AttributeError,
|
||||
"type object '%.50s' has no attribute '%U'",
|
||||
((PyTypeObject*)obj)->tp_name, name);
|
||||
}
|
||||
else {
|
||||
PyErr_Format(PyExc_AttributeError,
|
||||
"'%.100s' object has no attribute '%U'",
|
||||
tp->tp_name, name);
|
||||
}
|
||||
}
|
||||
done:
|
||||
Py_XDECREF(descr);
|
||||
Py_DECREF(tp);
|
||||
Py_DECREF(name);
|
||||
return res;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue