Skip to content

Commit c014325

Browse files
ZeroIntensityseehwan80
authored andcommitted
pythongh-131936: Strengthen check in _suggestions._generate_suggestions (python#131945)
1 parent 7030343 commit c014325

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

Lib/test/test_traceback.py

+25-1
Original file line numberDiff line numberDiff line change
@@ -4619,7 +4619,31 @@ def test_levenshtein_distance_short_circuit(self):
46194619
@cpython_only
46204620
def test_suggestions_extension(self):
46214621
# Check that the C extension is available
4622-
import _suggestions # noqa: F401
4622+
import _suggestions
4623+
4624+
self.assertEqual(
4625+
_suggestions._generate_suggestions(
4626+
["hello", "world"],
4627+
"hell"
4628+
),
4629+
"hello"
4630+
)
4631+
self.assertEqual(
4632+
_suggestions._generate_suggestions(
4633+
["hovercraft"],
4634+
"eels"
4635+
),
4636+
None
4637+
)
4638+
4639+
# gh-131936: _generate_suggestions() doesn't accept list subclasses
4640+
class MyList(list):
4641+
pass
4642+
4643+
with self.assertRaises(TypeError):
4644+
_suggestions._generate_suggestions(MyList(), "")
4645+
4646+
46234647

46244648

46254649
class TestColorizedTraceback(unittest.TestCase):

Modules/_suggestions.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ _suggestions__generate_suggestions_impl(PyObject *module,
2121
/*[clinic end generated code: output=79be7b653ae5e7ca input=ba2a8dddc654e33a]*/
2222
{
2323
// Check if dir is a list
24-
if (!PyList_Check(candidates)) {
24+
if (!PyList_CheckExact(candidates)) {
2525
PyErr_SetString(PyExc_TypeError, "candidates must be a list");
2626
return NULL;
2727
}

0 commit comments

Comments
 (0)