blob: d4e9dc22bbc7b9ebd1b2b6a2e206ff830a663d09 [file] [log] [blame]
Pablo Galindo37494b42021-04-14 02:36:07 +01001#include "Python.h"
Pablo Galindo5bf8bf22021-04-14 15:10:33 +01002#include "frameobject.h"
Pablo Galindo37494b42021-04-14 02:36:07 +01003
4#include "pycore_pyerrors.h"
5
6#define MAX_DISTANCE 3
Pablo Galindo3ab4bea2021-04-17 22:26:54 +01007#define MAX_CANDIDATE_ITEMS 160
Pablo Galindo5bf8bf22021-04-14 15:10:33 +01008#define MAX_STRING_SIZE 25
Pablo Galindo37494b42021-04-14 02:36:07 +01009
10/* Calculate the Levenshtein distance between string1 and string2 */
Pablo Galindo0b1c1692021-04-17 23:28:45 +010011static Py_ssize_t
Pablo Galindo3fc65b92021-04-15 00:03:43 +010012levenshtein_distance(const char *a, size_t a_size,
13 const char *b, size_t b_size) {
Pablo Galindo37494b42021-04-14 02:36:07 +010014
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) {
Pablo Galindo0b1c1692021-04-17 23:28:45 +010036 return -1;
Pablo Galindo37494b42021-04-14 02:36:07 +010037 }
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
75static inline PyObject *
76calculate_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 Galindo3fc65b92021-04-15 00:03:43 +010088 Py_ssize_t name_size;
89 const char *name_str = PyUnicode_AsUTF8AndSize(name, &name_size);
Pablo Galindo5bf8bf22021-04-14 15:10:33 +010090 if (name_str == NULL) {
Pablo Galindo5bf8bf22021-04-14 15:10:33 +010091 return NULL;
92 }
Pablo Galindo37494b42021-04-14 02:36:07 +010093 for (int i = 0; i < dir_size; ++i) {
94 PyObject *item = PyList_GET_ITEM(dir, i);
Pablo Galindo3fc65b92021-04-15 00:03:43 +010095 Py_ssize_t item_size;
96 const char *item_str = PyUnicode_AsUTF8AndSize(item, &item_size);
Pablo Galindo5bf8bf22021-04-14 15:10:33 +010097 if (item_str == NULL) {
Pablo Galindo5bf8bf22021-04-14 15:10:33 +010098 return NULL;
Pablo Galindo37494b42021-04-14 02:36:07 +010099 }
Pablo Galindo3fc65b92021-04-15 00:03:43 +0100100 Py_ssize_t current_distance = levenshtein_distance(
101 name_str, name_size, item_str, item_size);
Pablo Galindo0b1c1692021-04-17 23:28:45 +0100102 if (current_distance == -1) {
103 return NULL;
104 }
Pablo Galindo37494b42021-04-14 02:36:07 +0100105 if (current_distance == 0 || current_distance > MAX_DISTANCE) {
106 continue;
107 }
108 if (!suggestion || current_distance < suggestion_distance) {
109 suggestion = item;
110 suggestion_distance = current_distance;
111 }
112 }
113 if (!suggestion) {
114 return NULL;
115 }
116 Py_INCREF(suggestion);
117 return suggestion;
118}
119
120static PyObject *
121offer_suggestions_for_attribute_error(PyAttributeErrorObject *exc) {
122 PyObject *name = exc->name; // borrowed reference
123 PyObject *obj = exc->obj; // borrowed reference
124
125 // Abort if we don't have an attribute name or we have an invalid one
126 if (name == NULL || obj == NULL || !PyUnicode_CheckExact(name)) {
127 return NULL;
128 }
129
130 PyObject *dir = PyObject_Dir(obj);
131 if (dir == NULL) {
132 return NULL;
133 }
134
135 PyObject *suggestions = calculate_suggestions(dir, name);
136 Py_DECREF(dir);
137 return suggestions;
138}
139
Pablo Galindo5bf8bf22021-04-14 15:10:33 +0100140
141static PyObject *
142offer_suggestions_for_name_error(PyNameErrorObject *exc) {
143 PyObject *name = exc->name; // borrowed reference
144 PyTracebackObject *traceback = (PyTracebackObject *) exc->traceback; // borrowed reference
Pablo Galindo3fc65b92021-04-15 00:03:43 +0100145 // Abort if we don't have a variable name or we have an invalid one
146 // or if we don't have a traceback to work with
Pablo Galindo5bf8bf22021-04-14 15:10:33 +0100147 if (name == NULL || traceback == NULL || !PyUnicode_CheckExact(name)) {
148 return NULL;
149 }
150
151 // Move to the traceback of the exception
152 while (traceback->tb_next != NULL) {
153 traceback = traceback->tb_next;
154 }
155
156 PyFrameObject *frame = traceback->tb_frame;
157 assert(frame != NULL);
158 PyCodeObject *code = frame->f_code;
159 assert(code != NULL && code->co_varnames != NULL);
160 PyObject *dir = PySequence_List(code->co_varnames);
161 if (dir == NULL) {
Pablo Galindo5bf8bf22021-04-14 15:10:33 +0100162 return NULL;
163 }
164
165 PyObject *suggestions = calculate_suggestions(dir, name);
166 Py_DECREF(dir);
167 if (suggestions != NULL) {
168 return suggestions;
169 }
170
171 dir = PySequence_List(frame->f_globals);
172 if (dir == NULL) {
Pablo Galindo5bf8bf22021-04-14 15:10:33 +0100173 return NULL;
174 }
175 suggestions = calculate_suggestions(dir, name);
176 Py_DECREF(dir);
Pablo Galindo3ab4bea2021-04-17 22:26:54 +0100177 if (suggestions != NULL) {
178 return suggestions;
179 }
180
181 dir = PySequence_List(frame->f_builtins);
182 if (dir == NULL) {
183 return NULL;
184 }
185 suggestions = calculate_suggestions(dir, name);
186 Py_DECREF(dir);
Pablo Galindo5bf8bf22021-04-14 15:10:33 +0100187
188 return suggestions;
189}
190
Pablo Galindo37494b42021-04-14 02:36:07 +0100191// Offer suggestions for a given exception. Returns a python string object containing the
Pablo Galindoe07f4ab2021-04-14 18:58:28 +0100192// suggestions. This function returns NULL if no suggestion was found or if an exception happened,
193// users must call PyErr_Occurred() to disambiguate.
Pablo Galindo37494b42021-04-14 02:36:07 +0100194PyObject *_Py_Offer_Suggestions(PyObject *exception) {
195 PyObject *result = NULL;
Pablo Galindoe07f4ab2021-04-14 18:58:28 +0100196 assert(!PyErr_Occurred());
Pablo Galindo0ad81d42021-04-16 17:12:03 +0100197 if (Py_IS_TYPE(exception, (PyTypeObject*)PyExc_AttributeError)) {
Pablo Galindo37494b42021-04-14 02:36:07 +0100198 result = offer_suggestions_for_attribute_error((PyAttributeErrorObject *) exception);
Pablo Galindo0ad81d42021-04-16 17:12:03 +0100199 } else if (Py_IS_TYPE(exception, (PyTypeObject*)PyExc_NameError)) {
Pablo Galindo5bf8bf22021-04-14 15:10:33 +0100200 result = offer_suggestions_for_name_error((PyNameErrorObject *) exception);
Pablo Galindo37494b42021-04-14 02:36:07 +0100201 }
Pablo Galindo37494b42021-04-14 02:36:07 +0100202 return result;
203}
204