diff options
author | Géry Ogam <gery.ogam@gmail.com> | 2022-05-05 13:37:26 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-05 13:37:26 (GMT) |
commit | a95138b2c5a3ba3d9a1a635566e22e5843b6a45c (patch) | |
tree | 9df22636e46deb06e0c8897ed0d072c440c42a7b /Objects | |
parent | 43b135f94ebf3e6e84ddb0f75ed8510b96a610e4 (diff) | |
download | cpython-a95138b2c5a3ba3d9a1a635566e22e5843b6a45c.zip cpython-a95138b2c5a3ba3d9a1a635566e22e5843b6a45c.tar.gz cpython-a95138b2c5a3ba3d9a1a635566e22e5843b6a45c.tar.bz2 |
bpo-43857: Improve the AttributeError message when deleting a missing attribute (#25424)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/dictobject.c | 4 | ||||
-rw-r--r-- | Objects/object.c | 18 |
2 files changed, 17 insertions, 5 deletions
diff --git a/Objects/dictobject.c b/Objects/dictobject.c index 063fd24..ebbd22e 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -5472,7 +5472,9 @@ _PyObject_StoreInstanceAttribute(PyObject *obj, PyDictValues *values, values->values[ix] = value; if (old_value == NULL) { if (value == NULL) { - PyErr_SetObject(PyExc_AttributeError, name); + PyErr_Format(PyExc_AttributeError, + "'%.100s' object has no attribute '%U'", + Py_TYPE(obj)->tp_name, name); return -1; } _PyDictValues_AddToInsertionOrder(values, ix); diff --git a/Objects/object.c b/Objects/object.c index 6f2d9f8..d5f21b7 100644 --- a/Objects/object.c +++ b/Objects/object.c @@ -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; } |