[Python-checkins] bpo-38530: Match exactly AttributeError and NameError when offering suggestions (GH-25443)

pablogsal webhook-mailer at python.org
Fri Apr 16 12:12:08 EDT 2021


https://github.com/python/cpython/commit/0ad81d4db2f409d72f469d0b74ab597be772a68e
commit: 0ad81d4db2f409d72f469d0b74ab597be772a68e
branch: master
author: Pablo Galindo <Pablogsal at gmail.com>
committer: pablogsal <Pablogsal at gmail.com>
date: 2021-04-16T17:12:03+01:00
summary:

bpo-38530: Match exactly AttributeError and NameError when offering suggestions (GH-25443)

files:
M Lib/test/test_exceptions.py
M Python/suggestions.c

diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py
index ebeb67b451282..bd20b232e541f 100644
--- a/Lib/test/test_exceptions.py
+++ b/Lib/test/test_exceptions.py
@@ -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.
diff --git a/Python/suggestions.c b/Python/suggestions.c
index 258e3f1833e6b..e422760920739 100644
--- a/Python/suggestions.c
+++ b/Python/suggestions.c
@@ -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;



More information about the Python-checkins mailing list