Skip to content

Commit a27a0fd

Browse files
committed
Simplify implementation and rename public function
1 parent 8d3e34c commit a27a0fd

File tree

3 files changed

+27
-30
lines changed

3 files changed

+27
-30
lines changed

Include/internal/pycore_suggestions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# error "this header requires Py_BUILD_CORE define"
88
#endif
99

10-
int _Py_offer_suggestions(PyAttributeErrorObject* exception_value);
10+
int _Py_offer_suggestions_for_attribute_error(PyAttributeErrorObject* exception_value);
1111

1212

1313
#endif /* !Py_INTERNAL_SUGGESTIONS_H */

Python/pythonrun.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1011,7 +1011,7 @@ PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
10111011
}
10121012

10131013
if (PyErr_GivenExceptionMatches(exception, PyExc_AttributeError) &&
1014-
_Py_offer_suggestions((PyAttributeErrorObject*) value) != 0) {
1014+
_Py_offer_suggestions_for_attribute_error((PyAttributeErrorObject*) value) != 0) {
10151015
PyErr_Clear();
10161016
}
10171017
_PyErr_Display(file, exception, value, tb);

Python/suggestions.c

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -164,51 +164,48 @@ calculate_suggestions(PyObject* dir,
164164
}
165165

166166
int
167-
_Py_offer_suggestions(PyAttributeErrorObject* exc) {
167+
_Py_offer_suggestions_for_attribute_error(PyAttributeErrorObject* exc) {
168168
int return_val = 0;
169169

170170
PyObject* name = exc->name;
171171
PyObject* v = exc->obj;
172172

173+
// Aboirt if we don't have an attribute name or we have an invalid one
173174
if ((name == NULL) || (v == NULL) || !PyUnicode_CheckExact(name)) {
174-
return 0;
175+
return -1;
175176
}
176177

177178
PyObject* oldexceptionvalue = NULL;
178179
Py_ssize_t nargs = PyTuple_GET_SIZE(exc->args);
179-
180-
if (nargs > 1) {
181-
// Exceptions with more than 1 value in args are not
182-
// formatted using args, so no need to make a new suggestion.
183-
return 0;
184-
}
185-
186-
if (nargs == 1) {
187-
oldexceptionvalue = PyTuple_GET_ITEM(exc->args, 0);
188-
Py_INCREF(oldexceptionvalue);
189-
}
190-
191-
// Check that the the message is an uncode objects that we can use,
192-
// and if not, create an empty value.
193-
if (!oldexceptionvalue || !PyUnicode_CheckExact(oldexceptionvalue)) {
194-
Py_XDECREF(oldexceptionvalue);
195-
oldexceptionvalue = PyUnicode_New(0, 0);
180+
switch (nargs) {
181+
case 0:
182+
oldexceptionvalue = PyUnicode_New(0, 0);
183+
break;
184+
case 1:
185+
oldexceptionvalue = PyTuple_GET_ITEM(exc->args, 0);
186+
Py_INCREF(oldexceptionvalue);
187+
// Check that the the message is an uncode objects that we can use.
188+
if (!PyUnicode_CheckExact(oldexceptionvalue)) {
189+
return_val = -1;
190+
goto exit;
191+
}
192+
break;
193+
default:
194+
// Exceptions with more than 1 value in args are not
195+
// formatted using args, so no need to make a new suggestion.
196+
return 0;
196197
}
197198

198-
if (Py_EnterRecursiveCall(" while getting the __dir__ of an object")) {
199-
return_val = -1;
199+
PyObject* dir = PyObject_Dir(v);
200+
if (!dir) {
200201
goto exit;
201202
}
202-
PyObject* dir = PyObject_Dir(v);
203-
Py_LeaveRecursiveCall();
204203

205-
PyObject* newexceptionvalue = NULL;
206-
if (dir) {
207-
newexceptionvalue = calculate_suggestions(dir, name, oldexceptionvalue);
208-
Py_DECREF(dir);
209-
}
204+
PyObject* newexceptionvalue = calculate_suggestions(dir, name, oldexceptionvalue);
205+
Py_DECREF(dir);
210206

211207
if (newexceptionvalue == NULL) {
208+
// We did't find a suggestion :(
212209
goto exit;
213210
}
214211

0 commit comments

Comments
 (0)