|
| 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 | +{ |
| 23 | + // Check if dir is a list |
| 24 | + if (!PyList_Check(candidates)) { |
| 25 | + PyErr_SetString(PyExc_TypeError, "candidates must be a list"); |
| 26 | + return NULL; |
| 27 | + } |
| 28 | + |
| 29 | + // Check if all elements in the list are Unicode |
| 30 | + Py_ssize_t size = PyList_Size(candidates); |
| 31 | + for (Py_ssize_t i = 0; i < size; ++i) { |
| 32 | + PyObject *elem = PyList_GetItem(candidates, i); |
| 33 | + if (!PyUnicode_Check(elem)) { |
| 34 | + PyErr_SetString(PyExc_TypeError, "all elements in 'candidates' must be strings"); |
| 35 | + return NULL; |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + PyObject* result = _Py_CalculateSuggestions(candidates, item); |
| 40 | + if (!result && !PyErr_Occurred()) { |
| 41 | + Py_RETURN_NONE; |
| 42 | + } |
| 43 | + return result; |
| 44 | +} |
| 45 | + |
| 46 | + |
| 47 | +static PyMethodDef module_methods[] = { |
| 48 | + _SUGGESTIONS__GENERATE_SUGGESTIONS_METHODDEF |
| 49 | + {NULL, NULL, 0, NULL} // Sentinel |
| 50 | +}; |
| 51 | + |
| 52 | +static struct PyModuleDef suggestions_module = { |
| 53 | + PyModuleDef_HEAD_INIT, |
| 54 | + "_suggestions", |
| 55 | + NULL, |
| 56 | + -1, |
| 57 | + module_methods |
| 58 | +}; |
| 59 | + |
| 60 | +PyMODINIT_FUNC PyInit__suggestions(void) { |
| 61 | + return PyModule_Create(&suggestions_module); |
| 62 | +} |
| 63 | + |
0 commit comments