blob: 258e3f1833e6b5fe9c0c6f5905bf8840154adc22 [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
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) {
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
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 Galindo37494b42021-04-14 02:36:07 +0100102 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
117static PyObject *
118offer_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 Galindo5bf8bf22021-04-14 15:10:33 +0100137
138static PyObject *
139offer_suggestions_for_name_error(PyNameErrorObject *exc) {
140 PyObject *name = exc->name; // borrowed reference
141 PyTracebackObject *traceback = (PyTracebackObject *) exc->traceback; // borrowed reference
Pablo Galindo3fc65b92021-04-15 00:03:43 +0100142 // 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 Galindo5bf8bf22021-04-14 15:10:33 +0100144 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 Galindo5bf8bf22021-04-14 15:10:33 +0100159 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 Galindo5bf8bf22021-04-14 15:10:33 +0100170 return NULL;
171 }
172 suggestions = calculate_suggestions(dir, name);
173 Py_DECREF(dir);
174
175 return suggestions;
176}
177
Pablo Galindo37494b42021-04-14 02:36:07 +0100178// Offer suggestions for a given exception. Returns a python string object containing the
Pablo Galindoe07f4ab2021-04-14 18:58:28 +0100179// suggestions. This function returns NULL if no suggestion was found or if an exception happened,
180// users must call PyErr_Occurred() to disambiguate.
Pablo Galindo37494b42021-04-14 02:36:07 +0100181PyObject *_Py_Offer_Suggestions(PyObject *exception) {
182 PyObject *result = NULL;
Pablo Galindoe07f4ab2021-04-14 18:58:28 +0100183 assert(!PyErr_Occurred());
Pablo Galindo37494b42021-04-14 02:36:07 +0100184 if (PyErr_GivenExceptionMatches(exception, PyExc_AttributeError)) {
185 result = offer_suggestions_for_attribute_error((PyAttributeErrorObject *) exception);
Pablo Galindo5bf8bf22021-04-14 15:10:33 +0100186 } else if (PyErr_GivenExceptionMatches(exception, PyExc_NameError)) {
187 result = offer_suggestions_for_name_error((PyNameErrorObject *) exception);
Pablo Galindo37494b42021-04-14 02:36:07 +0100188 }
Pablo Galindo37494b42021-04-14 02:36:07 +0100189 return result;
190}
191