diff options
author | Peter Bierma <zintensitydev@gmail.com> | 2025-03-31 17:30:29 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-03-31 17:30:29 (GMT) |
commit | 511d3440a0bbb19731f4d96dde65dffbf85cdda5 (patch) | |
tree | 563d1a562506345b1b6d559eae7f2eabc0b00c3b | |
parent | 0f511d8b44dd9993474402411af8c83f4964bc95 (diff) | |
download | cpython-511d3440a0bbb19731f4d96dde65dffbf85cdda5.zip cpython-511d3440a0bbb19731f4d96dde65dffbf85cdda5.tar.gz cpython-511d3440a0bbb19731f4d96dde65dffbf85cdda5.tar.bz2 |
gh-131936: Strengthen check in `_suggestions._generate_suggestions` (#131945)
-rw-r--r-- | Lib/test/test_traceback.py | 26 | ||||
-rw-r--r-- | Modules/_suggestions.c | 2 |
2 files changed, 26 insertions, 2 deletions
diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py index ac49278..6ae8cf5 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -4619,7 +4619,31 @@ class MiscTest(unittest.TestCase): @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: _generate_suggestions() doesn't accept list subclasses + class MyList(list): + pass + + with self.assertRaises(TypeError): + _suggestions._generate_suggestions(MyList(), "") + + class TestColorizedTraceback(unittest.TestCase): diff --git a/Modules/_suggestions.c b/Modules/_suggestions.c index 80c7179..b8bc6db 100644 --- a/Modules/_suggestions.c +++ b/Modules/_suggestions.c @@ -21,7 +21,7 @@ _suggestions__generate_suggestions_impl(PyObject *module, /*[clinic end generated code: output=79be7b653ae5e7ca input=ba2a8dddc654e33a]*/ { // Check if dir is a list - if (!PyList_Check(candidates)) { + if (!PyList_CheckExact(candidates)) { PyErr_SetString(PyExc_TypeError, "candidates must be a list"); return NULL; } |