Skip to content
Open
7 changes: 7 additions & 0 deletions Lib/test/test_traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -4740,6 +4740,13 @@ def test_suggestions_extension(self):
None
)

self.assertRaises(
TypeError,
_suggestions._generate_suggestions,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_suggestions._generate_suggestions() checks if all elements are strings before calling _Py_CalculateSuggestions(). So this test doesn't check changed code.

["hello", "world", 0, 1.1],
"hell",
)

# gh-131936: _generate_suggestions() doesn't accept list subclasses
class MyList(list):
pass
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Avoid possible abort when getting suggestions and there are non-string candidates.
4 changes: 4 additions & 0 deletions Python/suggestions.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Loading