[Python-checkins] bpo-38530: Cover more error paths in error suggestion functions (GH-25462)
pablogsal
webhook-mailer at python.org
Sat Apr 17 18:28:54 EDT 2021
https://github.com/python/cpython/commit/0b1c169c4a009e1094fe5df938d2367e63ebeea0
commit: 0b1c169c4a009e1094fe5df938d2367e63ebeea0
branch: master
author: Pablo Galindo <Pablogsal at gmail.com>
committer: pablogsal <Pablogsal at gmail.com>
date: 2021-04-17T23:28:45+01:00
summary:
bpo-38530: Cover more error paths in error suggestion functions (GH-25462)
files:
M Lib/test/test_exceptions.py
M Python/suggestions.c
diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py
index b730858a99735..d1e1b196c4278 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 aa4870d13e1ab..d4e9dc22bbc7b 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;
}
More information about the Python-checkins
mailing list