]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-131936: Strengthen check in `_suggestions._generate_suggestions` (GH-131945...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Mon, 31 Mar 2025 17:54:20 +0000 (19:54 +0200)
committerGitHub <noreply@github.com>
Mon, 31 Mar 2025 17:54:20 +0000 (17:54 +0000)
gh-131936: Strengthen check in `_suggestions._generate_suggestions` (GH-131945)
(cherry picked from commit 511d3440a0bbb19731f4d96dde65dffbf85cdda5)

Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
Lib/test/test_traceback.py
Modules/_suggestions.c

index adc413d47d662ca6a7c19a0009537733667e0d4d..2dfee81681d42d9d7b9d0fd26c0799cd8f6aacde 100644 (file)
@@ -4615,7 +4615,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):
index 80c7179c4c251c822df8f2de8788e7b1e79261ed..b8bc6db24772811fd1926ed5150a26def8872caf 100644 (file)
@@ -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;
     }