mirror of
https://github.com/python/cpython.git
synced 2025-08-03 00:23:06 +00:00
[3.10] bpo-46940: Don't override existing AttributeError suggestion information (GH-31710) (GH-31724)
When an exception is created in a nested call to PyObject_GetAttr, any
external calls will override the context information of the
AttributeError that we have already placed in the most internal call.
This will cause the suggestions we create to nor work properly as the
attribute name and object that we will be using are the incorrect ones.
To avoid this, we need to check first if these attributes are already
set and bail out if that's the case..
(cherry picked from commit 3b3be05a16
)
Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
This commit is contained in:
parent
8acbb93c07
commit
3594ebca2c
4 changed files with 48 additions and 15 deletions
|
@ -2201,6 +2201,24 @@ class AttributeErrorTests(unittest.TestCase):
|
||||||
|
|
||||||
self.assertNotIn("?", err.getvalue())
|
self.assertNotIn("?", err.getvalue())
|
||||||
|
|
||||||
|
def test_attribute_error_inside_nested_getattr(self):
|
||||||
|
class A:
|
||||||
|
bluch = 1
|
||||||
|
|
||||||
|
class B:
|
||||||
|
def __getattribute__(self, attr):
|
||||||
|
a = A()
|
||||||
|
return a.blich
|
||||||
|
|
||||||
|
try:
|
||||||
|
B().something
|
||||||
|
except AttributeError as exc:
|
||||||
|
with support.captured_stderr() as err:
|
||||||
|
sys.__excepthook__(*sys.exc_info())
|
||||||
|
|
||||||
|
self.assertIn("Did you mean", err.getvalue())
|
||||||
|
self.assertIn("bluch", err.getvalue())
|
||||||
|
|
||||||
|
|
||||||
class ImportErrorTests(unittest.TestCase):
|
class ImportErrorTests(unittest.TestCase):
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Avoid overriding :exc:`AttributeError` metadata information for nested
|
||||||
|
attribute access calls. Patch by Pablo Galindo.
|
|
@ -890,19 +890,29 @@ set_attribute_error_context(PyObject* v, PyObject* name)
|
||||||
assert(PyErr_Occurred());
|
assert(PyErr_Occurred());
|
||||||
_Py_IDENTIFIER(name);
|
_Py_IDENTIFIER(name);
|
||||||
_Py_IDENTIFIER(obj);
|
_Py_IDENTIFIER(obj);
|
||||||
// Intercept AttributeError exceptions and augment them to offer
|
if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
|
||||||
// suggestions later.
|
return 0;
|
||||||
if (PyErr_ExceptionMatches(PyExc_AttributeError)){
|
|
||||||
PyObject *type, *value, *traceback;
|
|
||||||
PyErr_Fetch(&type, &value, &traceback);
|
|
||||||
PyErr_NormalizeException(&type, &value, &traceback);
|
|
||||||
if (PyErr_GivenExceptionMatches(value, PyExc_AttributeError) &&
|
|
||||||
(_PyObject_SetAttrId(value, &PyId_name, name) ||
|
|
||||||
_PyObject_SetAttrId(value, &PyId_obj, v))) {
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
PyErr_Restore(type, value, traceback);
|
|
||||||
}
|
}
|
||||||
|
// Intercept AttributeError exceptions and augment them to offer suggestions later.
|
||||||
|
PyObject *type, *value, *traceback;
|
||||||
|
PyErr_Fetch(&type, &value, &traceback);
|
||||||
|
PyErr_NormalizeException(&type, &value, &traceback);
|
||||||
|
// Check if the normalized exception is indeed an AttributeError
|
||||||
|
if (!PyErr_GivenExceptionMatches(value, PyExc_AttributeError)) {
|
||||||
|
goto restore;
|
||||||
|
}
|
||||||
|
PyAttributeErrorObject* the_exc = (PyAttributeErrorObject*) value;
|
||||||
|
// Check if this exception was already augmented
|
||||||
|
if (the_exc->name || the_exc->obj) {
|
||||||
|
goto restore;
|
||||||
|
}
|
||||||
|
// Augment the exception with the name and object
|
||||||
|
if (_PyObject_SetAttrId(value, &PyId_name, name) ||
|
||||||
|
_PyObject_SetAttrId(value, &PyId_obj, v)) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
restore:
|
||||||
|
PyErr_Restore(type, value, traceback);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6261,9 +6261,12 @@ format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
|
||||||
PyErr_Fetch(&type, &value, &traceback);
|
PyErr_Fetch(&type, &value, &traceback);
|
||||||
PyErr_NormalizeException(&type, &value, &traceback);
|
PyErr_NormalizeException(&type, &value, &traceback);
|
||||||
if (PyErr_GivenExceptionMatches(value, PyExc_NameError)) {
|
if (PyErr_GivenExceptionMatches(value, PyExc_NameError)) {
|
||||||
// We do not care if this fails because we are going to restore the
|
PyNameErrorObject* exc = (PyNameErrorObject*) value;
|
||||||
// NameError anyway.
|
if (exc->name == NULL) {
|
||||||
(void)_PyObject_SetAttrId(value, &PyId_name, obj);
|
// We do not care if this fails because we are going to restore the
|
||||||
|
// NameError anyway.
|
||||||
|
(void)_PyObject_SetAttrId(value, &PyId_name, obj);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
PyErr_Restore(type, value, traceback);
|
PyErr_Restore(type, value, traceback);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue