@@ -246,17 +246,19 @@ sets all items of a list (actually, any mutable sequence) to a given item::
246246 int
247247 set_all(PyObject *target, PyObject *item)
248248 {
249- int i, n;
249+ Py_ssize_t i, n;
250250
251251 n = PyObject_Length(target);
252252 if (n < 0)
253253 return -1;
254254 for (i = 0; i < n; i++) {
255- PyObject *index = PyLong_FromLong (i);
255+ PyObject *index = PyLong_FromSsize_t (i);
256256 if (!index)
257257 return -1;
258- if (PyObject_SetItem(target, index, item) < 0)
258+ if (PyObject_SetItem(target, index, item) < 0) {
259+ Py_DECREF(index);
259260 return -1;
261+ }
260262 Py_DECREF(index);
261263 }
262264 return 0;
@@ -292,8 +294,8 @@ using :c:func:`PySequence_GetItem`. ::
292294 long
293295 sum_list(PyObject *list)
294296 {
295- int i, n;
296- long total = 0;
297+ Py_ssize_t i, n;
298+ long total = 0, value ;
297299 PyObject *item;
298300
299301 n = PyList_Size(list);
@@ -302,7 +304,11 @@ using :c:func:`PySequence_GetItem`. ::
302304 for (i = 0; i < n; i++) {
303305 item = PyList_GetItem(list, i); /* Can't fail */
304306 if (!PyLong_Check(item)) continue; /* Skip non-integers */
305- total += PyLong_AsLong(item);
307+ value = PyLong_AsLong(item);
308+ if (value == -1 && PyErr_Occurred())
309+ /* Integer too big to fit in a C long, bail out */
310+ return -1;
311+ total += value;
306312 }
307313 return total;
308314 }
@@ -314,8 +320,8 @@ using :c:func:`PySequence_GetItem`. ::
314320 long
315321 sum_sequence(PyObject *sequence)
316322 {
317- int i, n;
318- long total = 0;
323+ Py_ssize_t i, n;
324+ long total = 0, value ;
319325 PyObject *item;
320326 n = PySequence_Length(sequence);
321327 if (n < 0)
@@ -324,9 +330,17 @@ using :c:func:`PySequence_GetItem`. ::
324330 item = PySequence_GetItem(sequence, i);
325331 if (item == NULL)
326332 return -1; /* Not a sequence, or other failure */
327- if (PyLong_Check(item))
328- total += PyLong_AsLong(item);
329- Py_DECREF(item); /* Discard reference ownership */
333+ if (PyLong_Check(item)) {
334+ value = PyLong_AsLong(item);
335+ Py_DECREF(item);
336+ if (value == -1 && PyErr_Occurred())
337+ /* Integer too big to fit in a C long, bail out */
338+ return -1;
339+ total += value;
340+ }
341+ else {
342+ Py_DECREF(item); /* Discard reference ownership */
343+ }
330344 }
331345 return total;
332346 }
0 commit comments