Skip to content

Commit 0b1c169

Browse files
authored
bpo-38530: Cover more error paths in error suggestion functions (GH-25462)
1 parent 8bf274a commit 0b1c169

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

Lib/test/test_exceptions.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1741,6 +1741,16 @@ def __dir__(self):
17411741
self.assertNotIn("blech", err.getvalue())
17421742
self.assertNotIn("oh no!", err.getvalue())
17431743

1744+
def test_attribute_error_with_bad_name(self):
1745+
try:
1746+
raise AttributeError(name=12, obj=23)
1747+
except AttributeError as exc:
1748+
with support.captured_stderr() as err:
1749+
sys.__excepthook__(*sys.exc_info())
1750+
1751+
self.assertNotIn("?", err.getvalue())
1752+
1753+
17441754
class ImportErrorTests(unittest.TestCase):
17451755

17461756
def test_attributes(self):

Python/suggestions.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#define MAX_STRING_SIZE 25
99

1010
/* Calculate the Levenshtein distance between string1 and string2 */
11-
static size_t
11+
static Py_ssize_t
1212
levenshtein_distance(const char *a, size_t a_size,
1313
const char *b, size_t b_size) {
1414

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

3434
size_t *buffer = PyMem_Calloc(a_size, sizeof(size_t));
3535
if (buffer == NULL) {
36-
return 0;
36+
return -1;
3737
}
3838

3939
// Initialize the buffer row
@@ -99,6 +99,9 @@ calculate_suggestions(PyObject *dir,
9999
}
100100
Py_ssize_t current_distance = levenshtein_distance(
101101
name_str, name_size, item_str, item_size);
102+
if (current_distance == -1) {
103+
return NULL;
104+
}
102105
if (current_distance == 0 || current_distance > MAX_DISTANCE) {
103106
continue;
104107
}

0 commit comments

Comments
 (0)