]> granicus.if.org Git - python/commitdiff
Issue #20440: More use of Py_SETREF.
authorSerhiy Storchaka <storchaka@gmail.com>
Sun, 27 Dec 2015 13:41:58 +0000 (15:41 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Sun, 27 Dec 2015 13:41:58 +0000 (15:41 +0200)
This patch is manually crafted and contains changes that couldn't be handled
automatically.

Modules/_ctypes/_ctypes.c
Modules/_elementtree.c
Modules/_sqlite/cursor.c
Modules/zlibmodule.c
Objects/exceptions.c
Python/errors.c

index 8e7e6e486ea865aecb20fbc2bd93c07d0844888a..c533c3b3a7b12d8ff24e41d2babecbcd9b1fdc85 100644 (file)
@@ -3081,10 +3081,9 @@ PyCFuncPtr_set_restype(PyCFuncPtrObject *self, PyObject *ob)
                         "restype must be a type, a callable, or None");
         return -1;
     }
-    Py_XDECREF(self->checker);
     Py_INCREF(ob);
     Py_SETREF(self->restype, ob);
-    self->checker = PyObject_GetAttrString(ob, "_check_retval_");
+    Py_SETREF(self->checker, PyObject_GetAttrString(ob, "_check_retval_"));
     if (self->checker == NULL)
         PyErr_Clear();
     return 0;
index 263d70a7a4612dd3b56b1c8f630513c43dc58eab..a035c5792026deb7091b71429aff61564e073460 100644 (file)
@@ -1573,9 +1573,8 @@ element_setattr(ElementObject* self, const char* name, PyObject* value)
     }
 
     if (strcmp(name, "tag") == 0) {
-        Py_DECREF(self->tag);
-        self->tag = value;
-        Py_INCREF(self->tag);
+        Py_INCREF(value);
+        Py_SETREF(self->tag, value);
     } else if (strcmp(name, "text") == 0) {
         Py_DECREF(JOIN_OBJ(self->text));
         self->text = value;
@@ -1587,9 +1586,8 @@ element_setattr(ElementObject* self, const char* name, PyObject* value)
     } else if (strcmp(name, "attrib") == 0) {
         if (!self->extra)
             element_new_extra(self, NULL);
-        Py_DECREF(self->extra->attrib);
-        self->extra->attrib = value;
-        Py_INCREF(self->extra->attrib);
+        Py_INCREF(value);
+        Py_SETREF(self->extra->attrib, value);
     } else {
         PyErr_SetString(PyExc_AttributeError, name);
         return -1;
@@ -1800,13 +1798,11 @@ treebuilder_handle_start(TreeBuilderObject* self, PyObject* tag,
     }
     self->index++;
 
-    Py_DECREF(this);
     Py_INCREF(node);
-    self->this = (ElementObject*) node;
+    Py_SETREF(self->this, (ElementObject*) node);
 
-    Py_DECREF(self->last);
     Py_INCREF(node);
-    self->last = (ElementObject*) node;
+    Py_SETREF(self->last, (ElementObject*) node);
 
     if (treebuilder_append_event(self, self->start_event_obj, node) < 0)
         goto error;
@@ -1857,7 +1853,7 @@ treebuilder_handle_data(TreeBuilderObject* self, PyObject* data)
 LOCAL(PyObject*)
 treebuilder_handle_end(TreeBuilderObject* self, PyObject* tag)
 {
-    PyObject* item;
+    ElementObject *item;
 
     if (self->data) {
         if (self->this == self->last) {
@@ -1882,15 +1878,12 @@ treebuilder_handle_end(TreeBuilderObject* self, PyObject* tag)
         return NULL;
     }
 
+    item = self->last;
+    self->last = self->this;
     self->index--;
-
-    item = PyList_GET_ITEM(self->stack, self->index);
-    Py_INCREF(item);
-
-    Py_DECREF(self->last);
-
-    self->last = (ElementObject*) self->this;
-    self->this = (ElementObject*) item;
+    self->this = (ElementObject *) PyList_GET_ITEM(self->stack, self->index);
+    Py_INCREF(self->this);
+    Py_DECREF(item);
 
     if (treebuilder_append_event(self, self->end_event_obj, (PyObject*)self->last) < 0)
         return NULL;
index 81db98862b71b5b244f49342d219b93054a73e04..3d5b8f396ae6c64b62b003e2f6c59fb6b47a2a6b 100644 (file)
@@ -558,10 +558,10 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject*
 
     if (self->statement) {
         (void)pysqlite_statement_reset(self->statement);
-        Py_DECREF(self->statement);
     }
 
-    self->statement = (pysqlite_Statement*)pysqlite_cache_get(self->connection->statement_cache, func_args);
+    Py_SETREF(self->statement,
+              (pysqlite_Statement *)pysqlite_cache_get(self->connection->statement_cache, func_args));
     Py_DECREF(func_args);
 
     if (!self->statement) {
index b31d5211f9c3253fd1ca2f81a7c6a771512f6924..74aad7cf0b1224b7933274b4110ab9f69482a56b 100644 (file)
@@ -731,11 +731,9 @@ PyZlib_copy(compobject *self)
     }
 
     Py_INCREF(self->unused_data);
+    Py_SETREF(retval->unused_data, self->unused_data);
     Py_INCREF(self->unconsumed_tail);
-    Py_XDECREF(retval->unused_data);
-    Py_XDECREF(retval->unconsumed_tail);
-    retval->unused_data = self->unused_data;
-    retval->unconsumed_tail = self->unconsumed_tail;
+    Py_SETREF(retval->unconsumed_tail, self->unconsumed_tail);
 
     /* Mark it as being initialized */
     retval->is_initialised = 1;
@@ -782,11 +780,9 @@ PyZlib_uncopy(compobject *self)
     }
 
     Py_INCREF(self->unused_data);
+    Py_SETREF(retval->unused_data, self->unused_data);
     Py_INCREF(self->unconsumed_tail);
-    Py_XDECREF(retval->unused_data);
-    Py_XDECREF(retval->unconsumed_tail);
-    retval->unused_data = self->unused_data;
-    retval->unconsumed_tail = self->unconsumed_tail;
+    Py_SETREF(retval->unconsumed_tail, self->unconsumed_tail);
 
     /* Mark it as being initialized */
     retval->is_initialised = 1;
index 8edc6c645a284d8ea4b11ad32c6389e55f96949a..8f2c7a6789fb687d66606cc156f5dc411d334a4e 100644 (file)
@@ -517,12 +517,14 @@ SystemExit_init(PySystemExitObject *self, PyObject *args, PyObject *kwds)
 
     if (size == 0)
         return 0;
-    Py_CLEAR(self->code);
-    if (size == 1)
-        self->code = PyTuple_GET_ITEM(args, 0);
-    else if (size > 1)
-        self->code = args;
-    Py_INCREF(self->code);
+    if (size == 1) {
+        Py_INCREF(PyTuple_GET_ITEM(args, 0));
+        Py_SETREF(self->code, PyTuple_GET_ITEM(args, 0));
+    }
+    else { /* size > 1 */
+        Py_INCREF(args);
+        Py_SETREF(self->code, args);
+    }
     return 0;
 }
 
index 00dfd3ec540ebabe7c3ecde41a558bdc4fce6130..5cbf5c969809cd14a696dc13bfa7ba1a78aae70a 100644 (file)
@@ -227,14 +227,11 @@ finally:
     tstate = PyThreadState_GET();
     if (++tstate->recursion_depth > Py_GetRecursionLimit()) {
         --tstate->recursion_depth;
-        /* throw away the old exception... */
-        Py_DECREF(*exc);
-        Py_DECREF(*val);
-        /* ... and use the recursion error instead */
-        *exc = PyExc_RuntimeError;
-        *val = PyExc_RecursionErrorInst;
-        Py_INCREF(*exc);
-        Py_INCREF(*val);
+        /* throw away the old exception and use the recursion error instead */
+        Py_INCREF(PyExc_RuntimeError);
+        Py_SETREF(*exc, PyExc_RuntimeError);
+        Py_INCREF(PyExc_RecursionErrorInst);
+        Py_SETREF(*val, PyExc_RecursionErrorInst);
         /* just keeping the old traceback */
         return;
     }