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 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; }