Coverage Report

Created: 2025-11-02 06:30

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