From e0efc44a536b794fbe28ea6a2a240677e6bc3460 Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Mon, 31 Mar 2025 11:13:32 -0400 Subject: [PATCH 1/4] Relax the assertion. --- Python/suggestions.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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) { From bf0e8aa3c09c390fc0281ebd3f09ad031ab437bd Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Mon, 31 Mar 2025 11:19:59 -0400 Subject: [PATCH 2/4] Add some tests. --- Lib/test/test_traceback.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py index ac492782cb1a5c..8363b50c6fb641 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -4619,7 +4619,20 @@ 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, *_): + raise RuntimeError("evil") + + self.assertEqual(_suggestions._generate_suggestions(MyList(["spanish", "inquisition"]), "spani"), + "spanish") class TestColorizedTraceback(unittest.TestCase): From 0ba58a39659d5975707ae9fdfb7c8d2b87e500ce Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Mon, 31 Mar 2025 11:20:39 -0400 Subject: [PATCH 3/4] Add a comment. --- Lib/test/test_traceback.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py index 8363b50c6fb641..65e7fa2379014a 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -4629,6 +4629,8 @@ def test_suggestions_extension(self): # 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"), From 4e1f93b095de353158655a43195a8be851673dbb Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Mon, 31 Mar 2025 11:24:36 -0400 Subject: [PATCH 4/4] Do some reformatting. --- Lib/test/test_traceback.py | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py index 65e7fa2379014a..b265abaeed380a 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -4621,10 +4621,20 @@ def test_suggestions_extension(self): # Check that the C extension is available import _suggestions - self.assertEqual(_suggestions._generate_suggestions(["hello", "world"], "hell"), - "hello") - self.assertEqual(_suggestions._generate_suggestions(["hovercraft"], "eels"), - None) + 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): @@ -4633,8 +4643,13 @@ def __getitem__(self, *_): # shouldn't be a problem. raise RuntimeError("evil") - self.assertEqual(_suggestions._generate_suggestions(MyList(["spanish", "inquisition"]), "spani"), - "spanish") + self.assertEqual( + _suggestions._generate_suggestions( + MyList(["spanish", "inquisition"]), + "spani" + ), + "spanish" + ) class TestColorizedTraceback(unittest.TestCase):