Skip to content

Commit 26d87fd

Browse files
[3.11] gh-106030: Miscellaneous fixes in Python/suggestions.c (GH-106… (GH-106039)
* PyUnicode_CompareWithASCIIString() only works if the second argument is ASCII string. * Refleak in get_suggestions_for_name_error. * Add some missing error checks. (cherry picked from commit c8c162e)
1 parent 15f4bba commit 26d87fd

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

Python/suggestions.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,14 @@ calculate_suggestions(PyObject *dir,
147147

148148
for (int i = 0; i < dir_size; ++i) {
149149
PyObject *item = PyList_GET_ITEM(dir, i);
150+
if (_PyUnicode_Equal(name, item)) {
151+
continue;
152+
}
150153
Py_ssize_t item_size;
151154
const char *item_str = PyUnicode_AsUTF8AndSize(item, &item_size);
152155
if (item_str == NULL) {
153156
return NULL;
154157
}
155-
if (PyUnicode_CompareWithASCIIString(name, item_str) == 0) {
156-
continue;
157-
}
158158
// No more than 1/3 of the involved characters should need changed.
159159
Py_ssize_t max_distance = (name_size + item_size + 3) * MOVE_COST / 6;
160160
// Don't take matches we've already beaten.
@@ -225,19 +225,19 @@ offer_suggestions_for_name_error(PyNameErrorObject *exc)
225225
PyCodeObject *code = PyFrame_GetCode(frame);
226226
assert(code != NULL && code->co_localsplusnames != NULL);
227227
PyObject *varnames = _PyCode_GetVarnames(code);
228+
Py_DECREF(code);
228229
if (varnames == NULL) {
229230
return NULL;
230231
}
231232
PyObject *dir = PySequence_List(varnames);
232233
Py_DECREF(varnames);
233-
Py_DECREF(code);
234234
if (dir == NULL) {
235235
return NULL;
236236
}
237237

238238
PyObject *suggestions = calculate_suggestions(dir, name);
239239
Py_DECREF(dir);
240-
if (suggestions != NULL) {
240+
if (suggestions != NULL|| PyErr_Occurred()) {
241241
return suggestions;
242242
}
243243

@@ -247,7 +247,7 @@ offer_suggestions_for_name_error(PyNameErrorObject *exc)
247247
}
248248
suggestions = calculate_suggestions(dir, name);
249249
Py_DECREF(dir);
250-
if (suggestions != NULL) {
250+
if (suggestions != NULL || PyErr_Occurred()) {
251251
return suggestions;
252252
}
253253

0 commit comments

Comments
 (0)