diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py index ac492782cb1a5c..b265abaeed380a 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -4619,7 +4619,37 @@ def test_levenshtein_distance_short_circuit(self): @cpython_only def test_suggestions_extension(self): # Check that the C extension is available - import _suggestions # noqa: F401 + import _suggestions + + self.assertEqual( + _suggestions._generate_suggestions( + ["hello", "world"], + "hell" + ), + "hello" + ) + self.assertEqual( + _suggestions._generate_suggestions( + ["hovercraft"], + "eels" + ), + None + ) + + # gh-131936: _Py_CalculateSuggestions wanted exactly a list + class MyList(list): + def __getitem__(self, *_): + # _Py_CalculateSuggestions uses the list macros, so this + # shouldn't be a problem. + raise RuntimeError("evil") + + self.assertEqual( + _suggestions._generate_suggestions( + MyList(["spanish", "inquisition"]), + "spani" + ), + "spanish" + ) class TestColorizedTraceback(unittest.TestCase): diff --git a/Python/suggestions.c b/Python/suggestions.c index 154a8ade1b0153..f74817ee92d5c7 100644 --- a/Python/suggestions.c +++ b/Python/suggestions.c @@ -129,7 +129,7 @@ _Py_CalculateSuggestions(PyObject *dir, PyObject *name) { assert(!PyErr_Occurred()); - assert(PyList_CheckExact(dir)); + assert(PyList_Check(dir)); Py_ssize_t dir_size = PyList_GET_SIZE(dir); if (dir_size >= MAX_CANDIDATE_ITEMS) {