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