diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py index 6b2271f5d5ba8d..e0e904c1231ac3 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -4740,6 +4740,13 @@ def test_suggestions_extension(self): None ) + self.assertRaises( + TypeError, + _suggestions._generate_suggestions, + ["hello", "world", 0, 1.1], + "hell", + ) + # gh-131936: _generate_suggestions() doesn't accept list subclasses class MyList(list): pass diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-06-02-12-36-52.gh-issue-129573.zY2VO9.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-06-02-12-36-52.gh-issue-129573.zY2VO9.rst new file mode 100644 index 00000000000000..9290236ca32370 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-06-02-12-36-52.gh-issue-129573.zY2VO9.rst @@ -0,0 +1 @@ +Avoid possible abort when getting suggestions and there are non-string candidates. diff --git a/Python/suggestions.c b/Python/suggestions.c index 154a8ade1b0153..891eea54fc9d9f 100644 --- a/Python/suggestions.c +++ b/Python/suggestions.c @@ -149,6 +149,10 @@ _Py_CalculateSuggestions(PyObject *dir, } for (Py_ssize_t 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; }