Pablo Galindo | 37494b4 | 2021-04-14 02:36:07 +0100 | [diff] [blame] | 1 | #include "Python.h" |
Pablo Galindo | 5bf8bf2 | 2021-04-14 15:10:33 +0100 | [diff] [blame^] | 2 | #include "frameobject.h" |
Pablo Galindo | 37494b4 | 2021-04-14 02:36:07 +0100 | [diff] [blame] | 3 | |
| 4 | #include "pycore_pyerrors.h" |
| 5 | |
| 6 | #define MAX_DISTANCE 3 |
| 7 | #define MAX_CANDIDATE_ITEMS 100 |
Pablo Galindo | 5bf8bf2 | 2021-04-14 15:10:33 +0100 | [diff] [blame^] | 8 | #define MAX_STRING_SIZE 25 |
Pablo Galindo | 37494b4 | 2021-04-14 02:36:07 +0100 | [diff] [blame] | 9 | |
| 10 | /* Calculate the Levenshtein distance between string1 and string2 */ |
| 11 | static size_t |
| 12 | levenshtein_distance(const char *a, const char *b) { |
Pablo Galindo | 37494b4 | 2021-04-14 02:36:07 +0100 | [diff] [blame] | 13 | |
| 14 | const size_t a_size = strlen(a); |
| 15 | const size_t b_size = strlen(b); |
| 16 | |
| 17 | if (a_size > MAX_STRING_SIZE || b_size > MAX_STRING_SIZE) { |
| 18 | return 0; |
| 19 | } |
| 20 | |
| 21 | // Both strings are the same (by identity) |
| 22 | if (a == b) { |
| 23 | return 0; |
| 24 | } |
| 25 | |
| 26 | // The first string is empty |
| 27 | if (a_size == 0) { |
| 28 | return b_size; |
| 29 | } |
| 30 | |
| 31 | // The second string is empty |
| 32 | if (b_size == 0) { |
| 33 | return a_size; |
| 34 | } |
| 35 | |
| 36 | size_t *buffer = PyMem_Calloc(a_size, sizeof(size_t)); |
| 37 | if (buffer == NULL) { |
| 38 | return 0; |
| 39 | } |
| 40 | |
| 41 | // Initialize the buffer row |
| 42 | size_t index = 0; |
| 43 | while (index < a_size) { |
| 44 | buffer[index] = index + 1; |
| 45 | index++; |
| 46 | } |
| 47 | |
| 48 | size_t b_index = 0; |
| 49 | size_t result = 0; |
| 50 | while (b_index < b_size) { |
| 51 | char code = b[b_index]; |
| 52 | size_t distance = result = b_index++; |
| 53 | index = SIZE_MAX; |
| 54 | while (++index < a_size) { |
| 55 | size_t b_distance = code == a[index] ? distance : distance + 1; |
| 56 | distance = buffer[index]; |
| 57 | if (distance > result) { |
| 58 | if (b_distance > result) { |
| 59 | result = result + 1; |
| 60 | } else { |
| 61 | result = b_distance; |
| 62 | } |
| 63 | } else { |
| 64 | if (b_distance > distance) { |
| 65 | result = distance + 1; |
| 66 | } else { |
| 67 | result = b_distance; |
| 68 | } |
| 69 | } |
| 70 | buffer[index] = result; |
| 71 | } |
| 72 | } |
| 73 | PyMem_Free(buffer); |
| 74 | return result; |
| 75 | } |
| 76 | |
| 77 | static inline PyObject * |
| 78 | calculate_suggestions(PyObject *dir, |
| 79 | PyObject *name) { |
| 80 | assert(!PyErr_Occurred()); |
| 81 | assert(PyList_CheckExact(dir)); |
| 82 | |
| 83 | Py_ssize_t dir_size = PyList_GET_SIZE(dir); |
| 84 | if (dir_size >= MAX_CANDIDATE_ITEMS) { |
| 85 | return NULL; |
| 86 | } |
| 87 | |
| 88 | Py_ssize_t suggestion_distance = PyUnicode_GetLength(name); |
| 89 | PyObject *suggestion = NULL; |
Pablo Galindo | 5bf8bf2 | 2021-04-14 15:10:33 +0100 | [diff] [blame^] | 90 | const char *name_str = PyUnicode_AsUTF8(name); |
| 91 | if (name_str == NULL) { |
| 92 | PyErr_Clear(); |
| 93 | return NULL; |
| 94 | } |
Pablo Galindo | 37494b4 | 2021-04-14 02:36:07 +0100 | [diff] [blame] | 95 | for (int i = 0; i < dir_size; ++i) { |
| 96 | PyObject *item = PyList_GET_ITEM(dir, i); |
Pablo Galindo | 5bf8bf2 | 2021-04-14 15:10:33 +0100 | [diff] [blame^] | 97 | const char *item_str = PyUnicode_AsUTF8(item); |
| 98 | if (item_str == NULL) { |
Pablo Galindo | 37494b4 | 2021-04-14 02:36:07 +0100 | [diff] [blame] | 99 | PyErr_Clear(); |
Pablo Galindo | 5bf8bf2 | 2021-04-14 15:10:33 +0100 | [diff] [blame^] | 100 | return NULL; |
Pablo Galindo | 37494b4 | 2021-04-14 02:36:07 +0100 | [diff] [blame] | 101 | } |
Pablo Galindo | 5bf8bf2 | 2021-04-14 15:10:33 +0100 | [diff] [blame^] | 102 | Py_ssize_t current_distance = levenshtein_distance(name_str, item_str); |
Pablo Galindo | 37494b4 | 2021-04-14 02:36:07 +0100 | [diff] [blame] | 103 | if (current_distance == 0 || current_distance > MAX_DISTANCE) { |
| 104 | continue; |
| 105 | } |
| 106 | if (!suggestion || current_distance < suggestion_distance) { |
| 107 | suggestion = item; |
| 108 | suggestion_distance = current_distance; |
| 109 | } |
| 110 | } |
| 111 | if (!suggestion) { |
| 112 | return NULL; |
| 113 | } |
| 114 | Py_INCREF(suggestion); |
| 115 | return suggestion; |
| 116 | } |
| 117 | |
| 118 | static PyObject * |
| 119 | offer_suggestions_for_attribute_error(PyAttributeErrorObject *exc) { |
| 120 | PyObject *name = exc->name; // borrowed reference |
| 121 | PyObject *obj = exc->obj; // borrowed reference |
| 122 | |
| 123 | // Abort if we don't have an attribute name or we have an invalid one |
| 124 | if (name == NULL || obj == NULL || !PyUnicode_CheckExact(name)) { |
| 125 | return NULL; |
| 126 | } |
| 127 | |
| 128 | PyObject *dir = PyObject_Dir(obj); |
| 129 | if (dir == NULL) { |
| 130 | return NULL; |
| 131 | } |
| 132 | |
| 133 | PyObject *suggestions = calculate_suggestions(dir, name); |
| 134 | Py_DECREF(dir); |
| 135 | return suggestions; |
| 136 | } |
| 137 | |
Pablo Galindo | 5bf8bf2 | 2021-04-14 15:10:33 +0100 | [diff] [blame^] | 138 | |
| 139 | static PyObject * |
| 140 | offer_suggestions_for_name_error(PyNameErrorObject *exc) { |
| 141 | PyObject *name = exc->name; // borrowed reference |
| 142 | PyTracebackObject *traceback = (PyTracebackObject *) exc->traceback; // borrowed reference |
| 143 | // Abort if we don't have an attribute name or we have an invalid one |
| 144 | if (name == NULL || traceback == NULL || !PyUnicode_CheckExact(name)) { |
| 145 | return NULL; |
| 146 | } |
| 147 | |
| 148 | // Move to the traceback of the exception |
| 149 | while (traceback->tb_next != NULL) { |
| 150 | traceback = traceback->tb_next; |
| 151 | } |
| 152 | |
| 153 | PyFrameObject *frame = traceback->tb_frame; |
| 154 | assert(frame != NULL); |
| 155 | PyCodeObject *code = frame->f_code; |
| 156 | assert(code != NULL && code->co_varnames != NULL); |
| 157 | PyObject *dir = PySequence_List(code->co_varnames); |
| 158 | if (dir == NULL) { |
| 159 | PyErr_Clear(); |
| 160 | return NULL; |
| 161 | } |
| 162 | |
| 163 | PyObject *suggestions = calculate_suggestions(dir, name); |
| 164 | Py_DECREF(dir); |
| 165 | if (suggestions != NULL) { |
| 166 | return suggestions; |
| 167 | } |
| 168 | |
| 169 | dir = PySequence_List(frame->f_globals); |
| 170 | if (dir == NULL) { |
| 171 | PyErr_Clear(); |
| 172 | return NULL; |
| 173 | } |
| 174 | suggestions = calculate_suggestions(dir, name); |
| 175 | Py_DECREF(dir); |
| 176 | |
| 177 | return suggestions; |
| 178 | } |
| 179 | |
Pablo Galindo | 37494b4 | 2021-04-14 02:36:07 +0100 | [diff] [blame] | 180 | // Offer suggestions for a given exception. Returns a python string object containing the |
| 181 | // suggestions. This function does not raise exceptions and returns NULL if no suggestion was found. |
| 182 | PyObject *_Py_Offer_Suggestions(PyObject *exception) { |
| 183 | PyObject *result = NULL; |
| 184 | assert(!PyErr_Occurred()); // Check that we are not going to clean any existing exception |
| 185 | if (PyErr_GivenExceptionMatches(exception, PyExc_AttributeError)) { |
| 186 | result = offer_suggestions_for_attribute_error((PyAttributeErrorObject *) exception); |
Pablo Galindo | 5bf8bf2 | 2021-04-14 15:10:33 +0100 | [diff] [blame^] | 187 | } else if (PyErr_GivenExceptionMatches(exception, PyExc_NameError)) { |
| 188 | result = offer_suggestions_for_name_error((PyNameErrorObject *) exception); |
Pablo Galindo | 37494b4 | 2021-04-14 02:36:07 +0100 | [diff] [blame] | 189 | } |
| 190 | assert(!PyErr_Occurred()); |
| 191 | return result; |
| 192 | } |
| 193 | |