blob: 058294fc8b6b72f17b0d7dc4b69fbd39e6abebe9 [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
7#define MAX_CANDIDATE_ITEMS 100
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 */
11static size_t
12levenshtein_distance(const char *a, const char *b) {
Pablo Galindo37494b42021-04-14 02:36:07 +010013
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
77static inline PyObject *
78calculate_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 Galindo5bf8bf22021-04-14 15:10:33 +010090 const char *name_str = PyUnicode_AsUTF8(name);
91 if (name_str == NULL) {
92 PyErr_Clear();
93 return NULL;
94 }
Pablo Galindo37494b42021-04-14 02:36:07 +010095 for (int i = 0; i < dir_size; ++i) {
96 PyObject *item = PyList_GET_ITEM(dir, i);
Pablo Galindo5bf8bf22021-04-14 15:10:33 +010097 const char *item_str = PyUnicode_AsUTF8(item);
98 if (item_str == NULL) {
Pablo Galindo37494b42021-04-14 02:36:07 +010099 PyErr_Clear();
Pablo Galindo5bf8bf22021-04-14 15:10:33 +0100100 return NULL;
Pablo Galindo37494b42021-04-14 02:36:07 +0100101 }
Pablo Galindo5bf8bf22021-04-14 15:10:33 +0100102 Py_ssize_t current_distance = levenshtein_distance(name_str, item_str);
Pablo Galindo37494b42021-04-14 02:36:07 +0100103 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
118static PyObject *
119offer_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 Galindo5bf8bf22021-04-14 15:10:33 +0100138
139static PyObject *
140offer_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 Galindo37494b42021-04-14 02:36:07 +0100180// 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.
182PyObject *_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 Galindo5bf8bf22021-04-14 15:10:33 +0100187 } else if (PyErr_GivenExceptionMatches(exception, PyExc_NameError)) {
188 result = offer_suggestions_for_name_error((PyNameErrorObject *) exception);
Pablo Galindo37494b42021-04-14 02:36:07 +0100189 }
190 assert(!PyErr_Occurred());
191 return result;
192}
193