bpo-35441: Remove dead and buggy code related to PyList_SetItem(). (GH-11033)


In _localemodule.c and selectmodule.c, remove dead code that would
cause double decrefs if run.

In addition, replace PyList_SetItem() with PyList_SET_ITEM() in cases
where a new list is populated and there is no possibility of an error.

In addition, check if the list changed size in the loop in array_array_fromlist().
(cherry picked from commit 99d56b53560b3867844472ae381fb3f858760621)

Co-authored-by: Zackery Spytz <zspytz@gmail.com>
diff --git a/Modules/_localemodule.c b/Modules/_localemodule.c
index 716a730..8efda35 100644
--- a/Modules/_localemodule.c
+++ b/Modules/_localemodule.c
@@ -70,20 +70,13 @@
     do {
         i++;
         val = PyLong_FromLong(s[i]);
-        if (!val)
-            break;
-        if (PyList_SetItem(result, i, val)) {
-            Py_DECREF(val);
-            val = NULL;
-            break;
+        if (val == NULL) {
+            Py_DECREF(result);
+            return NULL;
         }
+        PyList_SET_ITEM(result, i, val);
     } while (s[i] != '\0' && s[i] != CHAR_MAX);
 
-    if (!val) {
-        Py_DECREF(result);
-        return NULL;
-    }
-
     return result;
 }
 
diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c
index 900c374..7b4a4a3 100644
--- a/Modules/arraymodule.c
+++ b/Modules/arraymodule.c
@@ -1544,12 +1544,18 @@
         if (array_resize(self, old_size + n) == -1)
             return NULL;
         for (i = 0; i < n; i++) {
-            PyObject *v = PyList_GetItem(list, i);
+            PyObject *v = PyList_GET_ITEM(list, i);
             if ((*self->ob_descr->setitem)(self,
                             Py_SIZE(self) - n + i, v) != 0) {
                 array_resize(self, old_size);
                 return NULL;
             }
+            if (n != PyList_GET_SIZE(list)) {
+                PyErr_SetString(PyExc_RuntimeError,
+                                "list changed size during iteration");
+                array_resize(self, old_size);
+                return NULL;
+            }
         }
     }
     Py_RETURN_NONE;
@@ -1574,8 +1580,7 @@
         PyObject *v = getarrayitem((PyObject *)self, i);
         if (v == NULL)
             goto error;
-        if (PyList_SetItem(list, i, v) < 0)
-            goto error;
+        PyList_SET_ITEM(list, i, v);
     }
     return list;
 
diff --git a/Modules/readline.c b/Modules/readline.c
index 7756e6b..fa7e7d1 100644
--- a/Modules/readline.c
+++ b/Modules/readline.c
@@ -936,8 +936,7 @@
         s = decode(matches[i+1]);
         if (s == NULL)
             goto error;
-        if (PyList_SetItem(m, i, s) == -1)
-            goto error;
+        PyList_SET_ITEM(m, i, s);
     }
     sub = decode(matches[0]);
     r = PyObject_CallFunction(readlinestate_global->completion_display_matches_hook,
diff --git a/Modules/selectmodule.c b/Modules/selectmodule.c
index 88679e8..93d896a 100644
--- a/Modules/selectmodule.c
+++ b/Modules/selectmodule.c
@@ -652,10 +652,7 @@
             goto error;
         }
         PyTuple_SET_ITEM(value, 1, num);
-        if ((PyList_SetItem(result_list, j, value)) == -1) {
-            Py_DECREF(value);
-            goto error;
-        }
+        PyList_SET_ITEM(result_list, j, value);
         i++;
     }
     return result_list;
@@ -981,10 +978,7 @@
         Py_DECREF(num2);
         if (value == NULL)
             goto error;
-        if ((PyList_SetItem(result_list, i, value)) == -1) {
-            Py_DECREF(value);
-            goto error;
-        }
+        PyList_SET_ITEM(result_list, i, value);
     }
 
     return result_list;