Skip to content

bpo-38530: Optimize the calculation of string sizes when offering suggestions #25412

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 14, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions Python/suggestions.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@

/* Calculate the Levenshtein distance between string1 and string2 */
static size_t
levenshtein_distance(const char *a, const char *b) {

const size_t a_size = strlen(a);
const size_t b_size = strlen(b);
levenshtein_distance(const char *a, size_t a_size,
const char *b, size_t b_size) {

if (a_size > MAX_STRING_SIZE || b_size > MAX_STRING_SIZE) {
return 0;
Expand Down Expand Up @@ -87,17 +85,20 @@ calculate_suggestions(PyObject *dir,

Py_ssize_t suggestion_distance = PyUnicode_GetLength(name);
PyObject *suggestion = NULL;
const char *name_str = PyUnicode_AsUTF8(name);
Py_ssize_t name_size;
const char *name_str = PyUnicode_AsUTF8AndSize(name, &name_size);
if (name_str == NULL) {
return NULL;
}
for (int i = 0; i < dir_size; ++i) {
PyObject *item = PyList_GET_ITEM(dir, i);
const char *item_str = PyUnicode_AsUTF8(item);
Py_ssize_t item_size;
const char *item_str = PyUnicode_AsUTF8AndSize(item, &item_size);
if (item_str == NULL) {
return NULL;
}
Py_ssize_t current_distance = levenshtein_distance(name_str, item_str);
Py_ssize_t current_distance = levenshtein_distance(
name_str, name_size, item_str, item_size);
if (current_distance == 0 || current_distance > MAX_DISTANCE) {
continue;
}
Expand Down Expand Up @@ -138,7 +139,8 @@ static PyObject *
offer_suggestions_for_name_error(PyNameErrorObject *exc) {
PyObject *name = exc->name; // borrowed reference
PyTracebackObject *traceback = (PyTracebackObject *) exc->traceback; // borrowed reference
// Abort if we don't have an attribute name or we have an invalid one
// Abort if we don't have a variable name or we have an invalid one
// or if we don't have a traceback to work with
if (name == NULL || traceback == NULL || !PyUnicode_CheckExact(name)) {
return NULL;
}
Expand Down