Skip to content

bpo-38530: Cover more error paths in error suggestion functions #25462

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 17, 2021
Merged
Show file tree
Hide file tree
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
10 changes: 10 additions & 0 deletions Lib/test/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1741,6 +1741,16 @@ def __dir__(self):
self.assertNotIn("blech", err.getvalue())
self.assertNotIn("oh no!", err.getvalue())

def test_attribute_error_with_bad_name(self):
try:
raise AttributeError(name=12, obj=23)
except AttributeError as exc:
with support.captured_stderr() as err:
sys.__excepthook__(*sys.exc_info())

self.assertNotIn("?", err.getvalue())


class ImportErrorTests(unittest.TestCase):

def test_attributes(self):
Expand Down
7 changes: 5 additions & 2 deletions Python/suggestions.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#define MAX_STRING_SIZE 25

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

Expand All @@ -33,7 +33,7 @@ levenshtein_distance(const char *a, size_t a_size,

size_t *buffer = PyMem_Calloc(a_size, sizeof(size_t));
if (buffer == NULL) {
return 0;
return -1;
}

// Initialize the buffer row
Expand Down Expand Up @@ -99,6 +99,9 @@ calculate_suggestions(PyObject *dir,
}
Py_ssize_t current_distance = levenshtein_distance(
name_str, name_size, item_str, item_size);
if (current_distance == -1) {
return NULL;
}
if (current_distance == 0 || current_distance > MAX_DISTANCE) {
continue;
}
Expand Down