Coverage Report

Created: 2025-07-04 06:49

/src/cpython/Modules/_suggestions.c
Line
Count
Source (jump to first uncovered line)
1
#include "Python.h"
2
#include "pycore_pyerrors.h"
3
#include "clinic/_suggestions.c.h"
4
5
/*[clinic input]
6
module _suggestions
7
[clinic start generated code]*/
8
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=e58d81fafad5637b]*/
9
10
/*[clinic input]
11
_suggestions._generate_suggestions
12
    candidates: object
13
    item: unicode
14
    /
15
Returns the candidate in candidates that's closest to item
16
[clinic start generated code]*/
17
18
static PyObject *
19
_suggestions__generate_suggestions_impl(PyObject *module,
20
                                        PyObject *candidates, PyObject *item)
21
/*[clinic end generated code: output=79be7b653ae5e7ca input=ba2a8dddc654e33a]*/
22
0
{
23
   // Check if dir is a list
24
0
    if (!PyList_CheckExact(candidates)) {
25
0
        PyErr_SetString(PyExc_TypeError, "candidates must be a list");
26
0
        return NULL;
27
0
    }
28
29
    // Check if all elements in the list are Unicode
30
0
    Py_ssize_t size = PyList_Size(candidates);
31
0
    for (Py_ssize_t i = 0; i < size; ++i) {
32
0
        PyObject *elem = PyList_GetItem(candidates, i);
33
0
        if (!PyUnicode_Check(elem)) {
34
0
            PyErr_SetString(PyExc_TypeError, "all elements in 'candidates' must be strings");
35
0
            return NULL;
36
0
        }
37
0
    }
38
39
0
    PyObject* result =  _Py_CalculateSuggestions(candidates, item);
40
0
    if (!result && !PyErr_Occurred()) {
41
0
        Py_RETURN_NONE;
42
0
    }
43
0
    return result;
44
0
}
45
46
47
static PyMethodDef module_methods[] = {
48
    _SUGGESTIONS__GENERATE_SUGGESTIONS_METHODDEF
49
    {NULL, NULL, 0, NULL} // Sentinel
50
};
51
52
static PyModuleDef_Slot module_slots[] = {
53
    {Py_mod_multiple_interpreters, Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED},
54
    {Py_mod_gil, Py_MOD_GIL_NOT_USED},
55
    {0, NULL},
56
};
57
58
static struct PyModuleDef suggestions_module = {
59
    PyModuleDef_HEAD_INIT,
60
    "_suggestions",
61
    NULL,
62
    0,
63
    module_methods,
64
    module_slots,
65
};
66
67
0
PyMODINIT_FUNC PyInit__suggestions(void) {
68
0
    return PyModuleDef_Init(&suggestions_module);
69
0
}