Skip to content

bpo-38530: Match exactly AttributeError and NameError when offering suggestions #25443

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
Apr 16, 2021
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
15 changes: 15 additions & 0 deletions Lib/test/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1537,6 +1537,21 @@ def f():

self.assertNotIn("blech", err.getvalue())

def test_unbound_local_error_doesn_not_match(self):
def foo():
something = 3
print(somethong)
somethong = 3

try:
foo()
except UnboundLocalError as exc:
with support.captured_stderr() as err:
sys.__excepthook__(*sys.exc_info())

self.assertNotIn("something", err.getvalue())


class AttributeErrorTests(unittest.TestCase):
def test_attributes(self):
# Setting 'attr' should not be a problem.
Expand Down
4 changes: 2 additions & 2 deletions Python/suggestions.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,9 @@ offer_suggestions_for_name_error(PyNameErrorObject *exc) {
PyObject *_Py_Offer_Suggestions(PyObject *exception) {
PyObject *result = NULL;
assert(!PyErr_Occurred());
if (PyErr_GivenExceptionMatches(exception, PyExc_AttributeError)) {
if (Py_IS_TYPE(exception, (PyTypeObject*)PyExc_AttributeError)) {
result = offer_suggestions_for_attribute_error((PyAttributeErrorObject *) exception);
} else if (PyErr_GivenExceptionMatches(exception, PyExc_NameError)) {
} else if (Py_IS_TYPE(exception, (PyTypeObject*)PyExc_NameError)) {
result = offer_suggestions_for_name_error((PyNameErrorObject *) exception);
}
return result;
Expand Down