Skip to content

bpo-46940: Don't override existing AttributeError suggestion information #31710

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Lib/test/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2272,6 +2272,24 @@ def test_attribute_error_with_bad_name(self):

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):

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Avoid overriding :exc:`AttributeError` metadata information for nested
attribute access calls. Patch by Pablo Galindo.
34 changes: 22 additions & 12 deletions Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -875,19 +875,29 @@ static inline int
set_attribute_error_context(PyObject* v, PyObject* name)
{
assert(PyErr_Occurred());
// Intercept AttributeError exceptions and augment them to offer
// suggestions later.
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_SetAttr(value, &_Py_ID(name), name) ||
PyObject_SetAttr(value, &_Py_ID(obj), v))) {
return 1;
}
PyErr_Restore(type, value, traceback);
if (!PyErr_ExceptionMatches(PyExc_AttributeError)){
return 0;
}
// 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_SetAttr(value, &_Py_ID(name), name) ||
PyObject_SetAttr(value, &_Py_ID(obj), v)) {
return 1;
}
restore:
PyErr_Restore(type, value, traceback);
return 0;
}

Expand Down
9 changes: 6 additions & 3 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -7676,9 +7676,12 @@ format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
PyErr_Fetch(&type, &value, &traceback);
PyErr_NormalizeException(&type, &value, &traceback);
if (PyErr_GivenExceptionMatches(value, PyExc_NameError)) {
// We do not care if this fails because we are going to restore the
// NameError anyway.
(void)PyObject_SetAttr(value, &_Py_ID(name), obj);
PyNameErrorObject* exc = (PyNameErrorObject*) value;
if (exc->name == NULL) {
// We do not care if this fails because we are going to restore the
// NameError anyway.
(void)PyObject_SetAttr(value, &_Py_ID(name), obj);
}
}
PyErr_Restore(type, value, traceback);
}
Expand Down