summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPablo Galindo <Pablogsal@gmail.com>2021-04-17 22:28:45 (GMT)
committerGitHub <noreply@github.com>2021-04-17 22:28:45 (GMT)
commit0b1c169c4a009e1094fe5df938d2367e63ebeea0 (patch)
treec76957f0ecef70034069d510eec79bcdd85d18f1
parent8bf274a5002c013dd4086f6390fa19452b63ac1d (diff)
downloadcpython-0b1c169c4a009e1094fe5df938d2367e63ebeea0.zip
cpython-0b1c169c4a009e1094fe5df938d2367e63ebeea0.tar.gz
cpython-0b1c169c4a009e1094fe5df938d2367e63ebeea0.tar.bz2
bpo-38530: Cover more error paths in error suggestion functions (GH-25462)
-rw-r--r--Lib/test/test_exceptions.py10
-rw-r--r--Python/suggestions.c7
2 files changed, 15 insertions, 2 deletions
diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py
index b730858..d1e1b19 100644
--- a/Lib/test/test_exceptions.py
+++ b/Lib/test/test_exceptions.py
@@ -1741,6 +1741,16 @@ class AttributeErrorTests(unittest.TestCase):
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 aa4870d..d4e9dc2 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;
}