@@ -131,8 +131,7 @@ distance(const char *string1, const char *string2)
131
131
132
132
static inline PyObject *
133
133
calculate_suggestions (PyObject * dir ,
134
- PyObject * name ,
135
- PyObject * oldexceptionvalue )
134
+ PyObject * name )
136
135
{
137
136
assert (!PyErr_Occurred ());
138
137
assert (PyList_CheckExact (dir ));
@@ -164,81 +163,37 @@ calculate_suggestions(PyObject* dir,
164
163
if (!suggestion ) {
165
164
return NULL ;
166
165
}
167
- return PyUnicode_FromFormat ("%S. Did you mean: %U?" ,
168
- oldexceptionvalue , suggestion );
166
+ return PyUnicode_FromFormat ("Did you mean: %U?" , suggestion );
169
167
}
170
168
171
- static int
169
+ static PyObject *
172
170
offer_suggestions_for_attribute_error (PyAttributeErrorObject * exc ) {
173
- int return_val = -1 ;
174
-
175
171
PyObject * name = exc -> name ; // borrowed reference
176
172
PyObject * obj = exc -> obj ; // borrowed reference
177
173
178
174
// Abort if we don't have an attribute name or we have an invalid one
179
175
if (name == NULL || obj == NULL || !PyUnicode_CheckExact (name )) {
180
- return -1 ;
181
- }
182
-
183
- PyObject * oldexceptionvalue = NULL ;
184
- Py_ssize_t nargs = PyTuple_GET_SIZE (exc -> args );
185
- switch (nargs ) {
186
- case 0 :
187
- oldexceptionvalue = PyUnicode_New (0 , 0 );
188
- if (oldexceptionvalue == NULL ) {
189
- return -1 ;
190
- }
191
- break ;
192
- case 1 :
193
- oldexceptionvalue = PyTuple_GET_ITEM (exc -> args , 0 );
194
- Py_INCREF (oldexceptionvalue );
195
- // Check that the the message is an unicode objects that we can use.
196
- if (!PyUnicode_CheckExact (oldexceptionvalue )) {
197
- goto exit ;
198
- }
199
- break ;
200
- default :
201
- // Exceptions with more than 1 value in args are not
202
- // formatted using args, so no need to make a new suggestion.
203
- return 0 ;
176
+ return NULL ;
204
177
}
205
178
206
179
PyObject * dir = PyObject_Dir (obj );
207
180
if (dir == NULL ) {
208
- goto exit ;
181
+ return NULL ;
209
182
}
210
183
211
- PyObject * newexceptionvalue = calculate_suggestions (dir , name , oldexceptionvalue );
184
+ PyObject * suggestions = calculate_suggestions (dir , name );
212
185
Py_DECREF (dir );
213
-
214
- if (newexceptionvalue == NULL ) {
215
- // We did't find a suggestion :(
216
- goto exit ;
217
- }
218
-
219
- PyObject * new_args = PyTuple_Pack (1 , newexceptionvalue );
220
- Py_DECREF (newexceptionvalue );
221
- if (new_args == NULL ) {
222
- goto exit ;
223
- }
224
- Py_SETREF (exc -> args , new_args );
225
- exc -> args = new_args ;
226
- return_val = 0 ;
227
-
228
- exit :
229
- Py_DECREF (oldexceptionvalue );
230
-
231
- // Clear any error that may have happened.
232
- PyErr_Clear ();
233
- return return_val ;
186
+ return suggestions ;
234
187
}
235
188
236
189
237
- int _Py_Offer_Suggestions (PyObject * exception , PyObject * value ) {
238
- if (PyErr_GivenExceptionMatches (exception , PyExc_AttributeError ) &&
239
- offer_suggestions_for_attribute_error ((PyAttributeErrorObject * ) value ) != 0 ) {
240
- PyErr_Clear ();
190
+ // Offer suggestions for a given exception. This function does not raise exceptions
191
+ // and returns NULL if no exception was found.
192
+ PyObject * _Py_Offer_Suggestions (PyObject * exception ) {
193
+ assert (!PyErr_Occurred ());
194
+ if (PyErr_GivenExceptionMatches (exception , PyExc_AttributeError )) {
195
+ return offer_suggestions_for_attribute_error ((PyAttributeErrorObject * ) exception );
241
196
}
242
- return 0 ;
197
+ return NULL ;
243
198
}
244
199
0 commit comments