diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py index b730858a997354..d1e1b196c42789 100644 --- a/Lib/test/test_exceptions.py +++ b/Lib/test/test_exceptions.py @@ -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): diff --git a/Python/suggestions.c b/Python/suggestions.c index aa4870d13e1abf..d4e9dc22bbc7b9 100644 --- a/Python/suggestions.c +++ b/Python/suggestions.c @@ -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) { @@ -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 @@ -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; }