]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-44655: Don't include suggestions for attributes that are the same as the missing...
authorPablo Galindo Salgado <Pablogsal@gmail.com>
Fri, 16 Jul 2021 20:58:21 +0000 (21:58 +0100)
committerGitHub <noreply@github.com>
Fri, 16 Jul 2021 20:58:21 +0000 (21:58 +0100)
Lib/test/test_exceptions.py
Misc/NEWS.d/next/Core and Builtins/2021-07-16-20-25-37.bpo-44655.I3wRjL.rst [new file with mode: 0644]
Python/suggestions.c

index 2b00147dc90a47150675cea16fb31f36e06565a2..31051d4a8d0e041ee14ea27dd1d189ae32b25900 100644 (file)
@@ -1916,6 +1916,18 @@ class AttributeErrorTests(unittest.TestCase):
 
             self.assertIn("blech", err.getvalue())
 
+    def test_getattr_suggestions_for_same_name(self):
+        class A:
+            def __dir__(self):
+                return ['blech']
+        try:
+            A().blech
+        except AttributeError as exc:
+            with support.captured_stderr() as err:
+                sys.__excepthook__(*sys.exc_info())
+
+        self.assertNotIn("Did you mean", err.getvalue())
+
     def test_attribute_error_with_failing_dict(self):
         class T:
             bluch = 1
diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-07-16-20-25-37.bpo-44655.I3wRjL.rst b/Misc/NEWS.d/next/Core and Builtins/2021-07-16-20-25-37.bpo-44655.I3wRjL.rst
new file mode 100644 (file)
index 0000000..4ea4a6d
--- /dev/null
@@ -0,0 +1,2 @@
+Don't include a missing attribute with the same name as the failing one when
+offering suggestions for missing attributes. Patch by Pablo Galindo
index 3dfcbfe3ab0c37df5007e85553bfdfc848d40bae..77bdb6c1658618bcc7b9f5e992194fa020a14dd6 100644 (file)
@@ -151,6 +151,9 @@ calculate_suggestions(PyObject *dir,
         if (item_str == NULL) {
             return NULL;
         }
+        if (PyUnicode_CompareWithASCIIString(name, item_str) == 0) {
+            continue;
+        }
         // No more than 1/3 of the involved characters should need changed.
         Py_ssize_t max_distance = (name_size + item_size + 3) * MOVE_COST / 6;
         // Don't take matches we've already beaten.