Skip to content

Commit 0ad81d4

Browse files
authored
bpo-38530: Match exactly AttributeError and NameError when offering suggestions (GH-25443)
1 parent 3b82cae commit 0ad81d4

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

Lib/test/test_exceptions.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,6 +1537,21 @@ def f():
15371537

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

1540+
def test_unbound_local_error_doesn_not_match(self):
1541+
def foo():
1542+
something = 3
1543+
print(somethong)
1544+
somethong = 3
1545+
1546+
try:
1547+
foo()
1548+
except UnboundLocalError as exc:
1549+
with support.captured_stderr() as err:
1550+
sys.__excepthook__(*sys.exc_info())
1551+
1552+
self.assertNotIn("something", err.getvalue())
1553+
1554+
15401555
class AttributeErrorTests(unittest.TestCase):
15411556
def test_attributes(self):
15421557
# Setting 'attr' should not be a problem.

Python/suggestions.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,9 @@ offer_suggestions_for_name_error(PyNameErrorObject *exc) {
181181
PyObject *_Py_Offer_Suggestions(PyObject *exception) {
182182
PyObject *result = NULL;
183183
assert(!PyErr_Occurred());
184-
if (PyErr_GivenExceptionMatches(exception, PyExc_AttributeError)) {
184+
if (Py_IS_TYPE(exception, (PyTypeObject*)PyExc_AttributeError)) {
185185
result = offer_suggestions_for_attribute_error((PyAttributeErrorObject *) exception);
186-
} else if (PyErr_GivenExceptionMatches(exception, PyExc_NameError)) {
186+
} else if (Py_IS_TYPE(exception, (PyTypeObject*)PyExc_NameError)) {
187187
result = offer_suggestions_for_name_error((PyNameErrorObject *) exception);
188188
}
189189
return result;

0 commit comments

Comments
 (0)