@@ -59,7 +59,7 @@ distance(const char *string1, const char *string2)
59
59
/* initalize first row */
60
60
row = (size_t * )PyMem_Malloc (len2 * sizeof (size_t ));
61
61
if (!row ) {
62
- return (size_t )(-1 );
62
+ return (Py_ssize_t )(-1 );
63
63
}
64
64
end = row + len2 - 1 ;
65
65
for (i = 0 ; i < len2 - half ; i ++ ) {
@@ -72,7 +72,7 @@ distance(const char *string1, const char *string2)
72
72
* necessary */
73
73
row [0 ] = len1 - half - 1 ;
74
74
for (i = 1 ; i < len1 ; i ++ ) {
75
- size_t * p ;
75
+ size_t * scan_ptr ;
76
76
const char char1 = string1 [i - 1 ];
77
77
const char * char2p ;
78
78
size_t D , x ;
@@ -82,17 +82,18 @@ distance(const char *string1, const char *string2)
82
82
size_t c3 ;
83
83
84
84
char2p = string2 + offset ;
85
- p = row + offset ;
86
- c3 = * (p ++ ) + (char1 != * (char2p ++ ));
87
- x = * p ;
85
+ scan_ptr = row + offset ;
86
+ c3 = * (scan_ptr ++ ) + (char1 != * (char2p ++ ));
87
+ x = * scan_ptr ;
88
88
x ++ ;
89
89
D = x ;
90
- if (x > c3 )
90
+ if (x > c3 ) {
91
91
x = c3 ;
92
- * (p ++ ) = x ;
92
+ }
93
+ * (scan_ptr ++ ) = x ;
93
94
}
94
95
else {
95
- p = row + 1 ;
96
+ scan_ptr = row + 1 ;
96
97
char2p = string2 ;
97
98
D = x = i ;
98
99
}
@@ -101,24 +102,26 @@ distance(const char *string1, const char *string2)
101
102
end = row + len2 + i - half - 2 ;
102
103
}
103
104
/* main */
104
- while (p <= end ) {
105
+ while (scan_ptr <= end ) {
105
106
size_t c3 = -- D + (char1 != * (char2p ++ ));
106
107
x ++ ;
107
- if (x > c3 )
108
+ if (x > c3 ) {
108
109
x = c3 ;
109
- D = * p ;
110
+ }
111
+ D = * scan_ptr ;
110
112
D ++ ;
111
113
if (x > D )
112
114
x = D ;
113
- * (p ++ ) = x ;
115
+ * (scan_ptr ++ ) = x ;
114
116
}
115
117
/* lower triangle sentinel */
116
118
if (i <= half ) {
117
119
size_t c3 = -- D + (char1 != * char2p );
118
120
x ++ ;
119
- if (x > c3 )
121
+ if (x > c3 ) {
120
122
x = c3 ;
121
- * p = x ;
123
+ }
124
+ * scan_ptr = x ;
122
125
}
123
126
}
124
127
i = * end ;
@@ -131,6 +134,7 @@ calculate_suggestions(PyObject* dir,
131
134
PyObject * name ,
132
135
PyObject * oldexceptionvalue )
133
136
{
137
+ assert (!PyErr_Occurred ());
134
138
assert (PyList_CheckExact (dir ));
135
139
136
140
Py_ssize_t dir_size = PyList_GET_SIZE (dir );
@@ -166,13 +170,13 @@ calculate_suggestions(PyObject* dir,
166
170
167
171
static int
168
172
offer_suggestions_for_attribute_error (PyAttributeErrorObject * exc ) {
169
- int return_val = 0 ;
173
+ int return_val = -1 ;
170
174
171
- PyObject * name = exc -> name ;
172
- PyObject * v = exc -> obj ;
175
+ PyObject * name = exc -> name ; // borrowed reference
176
+ PyObject * obj = exc -> obj ; // borrowed reference
173
177
174
- // Aboirt if we don't have an attribute name or we have an invalid one
175
- if (( name == NULL ) || ( v == NULL ) || !PyUnicode_CheckExact (name )) {
178
+ // Abort if we don't have an attribute name or we have an invalid one
179
+ if (name == NULL || obj == NULL || !PyUnicode_CheckExact (name )) {
176
180
return -1 ;
177
181
}
178
182
@@ -181,13 +185,15 @@ offer_suggestions_for_attribute_error(PyAttributeErrorObject* exc) {
181
185
switch (nargs ) {
182
186
case 0 :
183
187
oldexceptionvalue = PyUnicode_New (0 , 0 );
188
+ if (oldexceptionvalue == NULL ) {
189
+ return -1 ;
190
+ }
184
191
break ;
185
192
case 1 :
186
193
oldexceptionvalue = PyTuple_GET_ITEM (exc -> args , 0 );
187
194
Py_INCREF (oldexceptionvalue );
188
- // Check that the the message is an uncode objects that we can use.
195
+ // Check that the the message is an unicode objects that we can use.
189
196
if (!PyUnicode_CheckExact (oldexceptionvalue )) {
190
- return_val = -1 ;
191
197
goto exit ;
192
198
}
193
199
break ;
@@ -197,8 +203,8 @@ offer_suggestions_for_attribute_error(PyAttributeErrorObject* exc) {
197
203
return 0 ;
198
204
}
199
205
200
- PyObject * dir = PyObject_Dir (v );
201
- if (! dir ) {
206
+ PyObject * dir = PyObject_Dir (obj );
207
+ if (dir == NULL ) {
202
208
goto exit ;
203
209
}
204
210
@@ -210,15 +216,14 @@ offer_suggestions_for_attribute_error(PyAttributeErrorObject* exc) {
210
216
goto exit ;
211
217
}
212
218
213
- PyObject * old_args = exc -> args ;
214
219
PyObject * new_args = PyTuple_Pack (1 , newexceptionvalue );
215
220
Py_DECREF (newexceptionvalue );
216
221
if (new_args == NULL ) {
217
- return_val = -1 ;
218
222
goto exit ;
219
223
}
224
+ Py_SETREF (exc -> args , new_args );
220
225
exc -> args = new_args ;
221
- Py_DECREF ( old_args ) ;
226
+ return_val = 0 ;
222
227
223
228
exit :
224
229
Py_DECREF (oldexceptionvalue );
@@ -229,7 +234,7 @@ offer_suggestions_for_attribute_error(PyAttributeErrorObject* exc) {
229
234
}
230
235
231
236
232
- int _Py_offer_suggestions (PyObject * exception , PyObject * value ) {
237
+ int _Py_Offer_Suggestions (PyObject * exception , PyObject * value ) {
233
238
if (PyErr_GivenExceptionMatches (exception , PyExc_AttributeError ) &&
234
239
offer_suggestions_for_attribute_error ((PyAttributeErrorObject * ) value ) != 0 ) {
235
240
PyErr_Clear ();
0 commit comments