]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-38530: Match exactly AttributeError and NameError when offering suggestions ...
authorPablo Galindo <Pablogsal@gmail.com>
Fri, 16 Apr 2021 16:12:03 +0000 (17:12 +0100)
committerGitHub <noreply@github.com>
Fri, 16 Apr 2021 16:12:03 +0000 (17:12 +0100)
Lib/test/test_exceptions.py
Python/suggestions.c

index ebeb67b4512822f84dd318e091607b72192ec535..bd20b232e541fe9ecd254e175fda6a3546384dad 100644 (file)
@@ -1537,6 +1537,21 @@ class NameErrorTests(unittest.TestCase):
 
         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.
index 258e3f1833e6b5fe9c0c6f5905bf8840154adc22..e42276092073904e9c1c25173f1cfc4e323ca749 100644 (file)
@@ -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;