From 8e821e72eb3c32fa4a6489fc9f777e8a41435cea Mon Sep 17 00:00:00 2001 From: devdanzin <74280297+devdanzin@users.noreply.github.com> Date: Sun, 2 Feb 2025 07:04:33 -0300 Subject: [PATCH 1/2] Check that suggestion candidates in calculate_suggestions are strings. --- Python/suggestions.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Python/suggestions.c b/Python/suggestions.c index ad58393490efc2..8469eb33031c56 100644 --- a/Python/suggestions.c +++ b/Python/suggestions.c @@ -151,6 +151,10 @@ calculate_suggestions(PyObject *dir, } for (int i = 0; i < dir_size; ++i) { PyObject *item = PyList_GET_ITEM(dir, i); + if (!PyUnicode_Check(item)) { + PyMem_Free(buffer); + return NULL; + } if (_PyUnicode_Equal(name, item)) { continue; } From 7c5bb5a3aedb3d7345c6352e628b11d967c01bfe Mon Sep 17 00:00:00 2001 From: devdanzin <74280297+devdanzin@users.noreply.github.com> Date: Sun, 2 Feb 2025 08:15:06 -0300 Subject: [PATCH 2/2] Add a test for the abort with non-string candidates in calculate_suggestions. --- Lib/test/test_traceback.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py index 86d509744517ad..5de904338a38cc 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -3166,6 +3166,17 @@ class A: actual = self.get_suggestion(A(), 'bluch') self.assertNotIn("blech", actual) + def test_suggestions_do_not_trigger_with_non_string_candidates(self): + class Parent: + def __dir__(self): + return [0] + + class WithNonStringAttrs(Parent): + blech = None + + result = self.get_suggestion(WithNonStringAttrs(), "bluch") + self.assertNotIn(result, "blech") + def test_getattr_suggestions_no_args(self): class A: blech = None