[2.7] bpo-35441: Remove dead and buggy code related to PyList_SetItem… · python/cpython@89b5ea2 (original) (raw)

6 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -73,20 +73,13 @@ copy_grouping(char* s)
73 73 do {
74 74 i++;
75 75 val = PyInt_FromLong(s[i]);
76 -if (!val)
77 -break;
78 -if (PyList_SetItem(result, i, val)) {
79 -Py_DECREF(val);
80 -val = NULL;
81 -break;
76 +if (val == NULL) {
77 +Py_DECREF(result);
78 +return NULL;
82 79 }
80 +PyList_SET_ITEM(result, i, val);
83 81 } while (s[i] != '\0' && s[i] != CHAR_MAX);
84 82
85 -if (!val) {
86 -Py_DECREF(result);
87 -return NULL;
88 - }
89 -
90 83 return result;
91 84 }
92 85
Original file line number Diff line number Diff line change
@@ -1344,7 +1344,7 @@ array_fromlist(arrayobject *self, PyObject *list)
1344 1344 Py_SIZE(self) += n;
1345 1345 self->allocated = Py_SIZE(self);
1346 1346 for (i = 0; i < n; i++) {
1347 -PyObject *v = PyList_GetItem(list, i);
1347 +PyObject *v = PyList_GET_ITEM(list, i);
1348 1348 if ((*self->ob_descr->setitem)(self,
1349 1349 Py_SIZE(self) - n + i, v) != 0) {
1350 1350 Py_SIZE(self) -= n;
@@ -1357,6 +1357,19 @@ array_fromlist(arrayobject *self, PyObject *list)
1357 1357 self->allocated = Py_SIZE(self);
1358 1358 return NULL;
1359 1359 }
1360 +if (n != PyList_GET_SIZE(list)) {
1361 +PyErr_SetString(PyExc_RuntimeError,
1362 +"list changed size during iteration");
1363 +Py_SIZE(self) -= n;
1364 +if (itemsize && (Py_SIZE(self) > PY_SSIZE_T_MAX / itemsize)) {
1365 +return PyErr_NoMemory();
1366 + }
1367 +PyMem_RESIZE(item, char,
1368 +Py_SIZE(self) * itemsize);
1369 +self->ob_item = item;
1370 +self->allocated = Py_SIZE(self);
1371 +return NULL;
1372 + }
1360 1373 }
1361 1374 }
1362 1375 Py_INCREF(Py_None);
@@ -1383,7 +1396,7 @@ array_tolist(arrayobject *self, PyObject *unused)
1383 1396 Py_DECREF(list);
1384 1397 return NULL;
1385 1398 }
1386 -PyList_SetItem(list, i, v);
1399 +PyList_SET_ITEM(list, i, v);
1387 1400 }
1388 1401 return list;
1389 1402 }
Original file line number Diff line number Diff line change
@@ -792,8 +792,7 @@ on_completion_display_matches_hook(char **matches,
792 792 s = PyString_FromString(matches[i+1]);
793 793 if (s == NULL)
794 794 goto error;
795 -if (PyList_SetItem(m, i, s) == -1)
796 - goto error;
795 +PyList_SET_ITEM(m, i, s);
797 796 }
798 797
799 798 r = PyObject_CallFunction(completion_display_matches_hook,
Original file line number Diff line number Diff line change
@@ -601,10 +601,7 @@ poll_poll(pollObject *self, PyObject *args)
601 601 goto error;
602 602 }
603 603 PyTuple_SET_ITEM(value, 1, num);
604 -if ((PyList_SetItem(result_list, j, value)) == -1) {
605 -Py_DECREF(value);
606 - goto error;
607 - }
604 +PyList_SET_ITEM(result_list, j, value);
608 605 i++;
609 606 }
610 607 }
Original file line number Diff line number Diff line change
@@ -5247,7 +5247,7 @@ getarray(long a[256])
5247 5247 Py_DECREF(l);
5248 5248 return NULL;
5249 5249 }
5250 -PyList_SetItem(l, i, x);
5250 +PyList_SET_ITEM(l, i, x);
5251 5251 }
5252 5252 for (i = 0; i < 256; i++)
5253 5253 a[i] = 0;
@@ -5269,7 +5269,7 @@ _Py_GetDXProfile(PyObject *self, PyObject *args)
5269 5269 Py_DECREF(l);
5270 5270 return NULL;
5271 5271 }
5272 -PyList_SetItem(l, i, x);
5272 +PyList_SET_ITEM(l, i, x);
5273 5273 }
5274 5274 return l;
5275 5275 #endif
Original file line number Diff line number Diff line change
@@ -1555,7 +1555,7 @@ makepathobject(char *path, int delim)
1555 1555 Py_DECREF(v);
1556 1556 return NULL;
1557 1557 }
1558 -PyList_SetItem(v, i, w);
1558 +PyList_SET_ITEM(v, i, w);
1559 1559 if (*p == '\0')
1560 1560 break;
1561 1561 path = p+1;