]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #27128: Cleanup slot_sq_item()
authorVictor Stinner <victor.stinner@gmail.com>
Fri, 19 Aug 2016 16:17:37 +0000 (18:17 +0200)
committerVictor Stinner <victor.stinner@gmail.com>
Fri, 19 Aug 2016 16:17:37 +0000 (18:17 +0200)
* Invert condition of test to avoid levels of indentation
* Remove useless Py_XDECREF(args) in the error block
* Replace Py_XDECREF(func) with Py_DECREF(func) in the error block: func cannot
  be NULL when reaching the error block

Objects/typeobject.c

index 4f01da01b9567db70e215aab4d162e03fd8b186d..ff9f0204e90357b0fb93e6c7139f922aeb5567a3 100644 (file)
@@ -5808,38 +5808,46 @@ slot_sq_length(PyObject *self)
 static PyObject *
 slot_sq_item(PyObject *self, Py_ssize_t i)
 {
-    PyObject *func, *args = NULL, *ival = NULL, *retval = NULL;
+    PyObject *func, *ival = NULL, *args, *retval = NULL;
     descrgetfunc f;
 
     func = _PyType_LookupId(Py_TYPE(self), &PyId___getitem__);
-    if (func != NULL) {
-        if ((f = Py_TYPE(func)->tp_descr_get) == NULL)
-            Py_INCREF(func);
-        else {
-            func = f(func, self, (PyObject *)(Py_TYPE(self)));
-            if (func == NULL) {
-                return NULL;
-            }
-        }
-        ival = PyLong_FromSsize_t(i);
-        if (ival != NULL) {
-            args = PyTuple_New(1);
-            if (args != NULL) {
-                PyTuple_SET_ITEM(args, 0, ival);
-                retval = PyObject_Call(func, args, NULL);
-                Py_DECREF(args);
-                Py_DECREF(func);
-                return retval;
-            }
-        }
-    }
-    else {
+    if (func == NULL) {
         PyObject *getitem_str = _PyUnicode_FromId(&PyId___getitem__);
         PyErr_SetObject(PyExc_AttributeError, getitem_str);
+        return NULL;
+    }
+
+    f = Py_TYPE(func)->tp_descr_get;
+    if (f == NULL) {
+        Py_INCREF(func);
     }
-    Py_XDECREF(args);
+    else {
+        func = f(func, self, (PyObject *)(Py_TYPE(self)));
+        if (func == NULL) {
+            return NULL;
+        }
+    }
+
+    ival = PyLong_FromSsize_t(i);
+    if (ival == NULL) {
+        goto error;
+    }
+
+    args = PyTuple_New(1);
+    if (args == NULL) {
+        goto error;
+    }
+
+    PyTuple_SET_ITEM(args, 0, ival);
+    retval = PyObject_Call(func, args, NULL);
+    Py_DECREF(func);
+    Py_DECREF(args);
+    return retval;
+
+error:
+    Py_DECREF(func);
     Py_XDECREF(ival);
-    Py_XDECREF(func);
     return NULL;
 }