Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion Lib/test/test_traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, *_):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just:

Suggested change
def __getitem__(self, *_):
def __getitem__(self, item):

# _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):
Expand Down
2 changes: 1 addition & 1 deletion Python/suggestions.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading