@@ -89,14 +89,12 @@ calculate_suggestions(PyObject *dir,
89
89
PyObject * suggestion = NULL ;
90
90
const char * name_str = PyUnicode_AsUTF8 (name );
91
91
if (name_str == NULL ) {
92
- PyErr_Clear ();
93
92
return NULL ;
94
93
}
95
94
for (int i = 0 ; i < dir_size ; ++ i ) {
96
95
PyObject * item = PyList_GET_ITEM (dir , i );
97
96
const char * item_str = PyUnicode_AsUTF8 (item );
98
97
if (item_str == NULL ) {
99
- PyErr_Clear ();
100
98
return NULL ;
101
99
}
102
100
Py_ssize_t current_distance = levenshtein_distance (name_str , item_str );
@@ -156,7 +154,6 @@ offer_suggestions_for_name_error(PyNameErrorObject *exc) {
156
154
assert (code != NULL && code -> co_varnames != NULL );
157
155
PyObject * dir = PySequence_List (code -> co_varnames );
158
156
if (dir == NULL ) {
159
- PyErr_Clear ();
160
157
return NULL ;
161
158
}
162
159
@@ -168,7 +165,6 @@ offer_suggestions_for_name_error(PyNameErrorObject *exc) {
168
165
169
166
dir = PySequence_List (frame -> f_globals );
170
167
if (dir == NULL ) {
171
- PyErr_Clear ();
172
168
return NULL ;
173
169
}
174
170
suggestions = calculate_suggestions (dir , name );
@@ -178,16 +174,15 @@ offer_suggestions_for_name_error(PyNameErrorObject *exc) {
178
174
}
179
175
180
176
// 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.
177
+ // suggestions. This function returns NULL if no suggestion was found or if an exception happened,
178
+ // users must call PyErr_Occurred() to disambiguate.
182
179
PyObject * _Py_Offer_Suggestions (PyObject * exception ) {
183
180
PyObject * result = NULL ;
184
- assert (!PyErr_Occurred ()); // Check that we are not going to clean any existing exception
185
181
if (PyErr_GivenExceptionMatches (exception , PyExc_AttributeError )) {
186
182
result = offer_suggestions_for_attribute_error ((PyAttributeErrorObject * ) exception );
187
183
} else if (PyErr_GivenExceptionMatches (exception , PyExc_NameError )) {
188
184
result = offer_suggestions_for_name_error ((PyNameErrorObject * ) exception );
189
185
}
190
- assert (!PyErr_Occurred ());
191
186
return result ;
192
187
}
193
188
0 commit comments