Skip to content

Commit 2e6c829

Browse files
Issue #20440: More use of Py_SETREF.
This patch is manually crafted and contains changes that couldn't be handled automatically.
1 parent 8688aca commit 2e6c829

6 files changed

Lines changed: 32 additions & 45 deletions

File tree

Modules/_ctypes/_ctypes.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3081,10 +3081,9 @@ PyCFuncPtr_set_restype(PyCFuncPtrObject *self, PyObject *ob)
30813081
"restype must be a type, a callable, or None");
30823082
return -1;
30833083
}
3084-
Py_XDECREF(self->checker);
30853084
Py_INCREF(ob);
30863085
Py_SETREF(self->restype, ob);
3087-
self->checker = PyObject_GetAttrString(ob, "_check_retval_");
3086+
Py_SETREF(self->checker, PyObject_GetAttrString(ob, "_check_retval_"));
30883087
if (self->checker == NULL)
30893088
PyErr_Clear();
30903089
return 0;

Modules/_elementtree.c

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1573,9 +1573,8 @@ element_setattr(ElementObject* self, const char* name, PyObject* value)
15731573
}
15741574

15751575
if (strcmp(name, "tag") == 0) {
1576-
Py_DECREF(self->tag);
1577-
self->tag = value;
1578-
Py_INCREF(self->tag);
1576+
Py_INCREF(value);
1577+
Py_SETREF(self->tag, value);
15791578
} else if (strcmp(name, "text") == 0) {
15801579
Py_DECREF(JOIN_OBJ(self->text));
15811580
self->text = value;
@@ -1587,9 +1586,8 @@ element_setattr(ElementObject* self, const char* name, PyObject* value)
15871586
} else if (strcmp(name, "attrib") == 0) {
15881587
if (!self->extra)
15891588
element_new_extra(self, NULL);
1590-
Py_DECREF(self->extra->attrib);
1591-
self->extra->attrib = value;
1592-
Py_INCREF(self->extra->attrib);
1589+
Py_INCREF(value);
1590+
Py_SETREF(self->extra->attrib, value);
15931591
} else {
15941592
PyErr_SetString(PyExc_AttributeError, name);
15951593
return -1;
@@ -1800,13 +1798,11 @@ treebuilder_handle_start(TreeBuilderObject* self, PyObject* tag,
18001798
}
18011799
self->index++;
18021800

1803-
Py_DECREF(this);
18041801
Py_INCREF(node);
1805-
self->this = (ElementObject*) node;
1802+
Py_SETREF(self->this, (ElementObject*) node);
18061803

1807-
Py_DECREF(self->last);
18081804
Py_INCREF(node);
1809-
self->last = (ElementObject*) node;
1805+
Py_SETREF(self->last, (ElementObject*) node);
18101806

18111807
if (treebuilder_append_event(self, self->start_event_obj, node) < 0)
18121808
goto error;
@@ -1857,7 +1853,7 @@ treebuilder_handle_data(TreeBuilderObject* self, PyObject* data)
18571853
LOCAL(PyObject*)
18581854
treebuilder_handle_end(TreeBuilderObject* self, PyObject* tag)
18591855
{
1860-
PyObject* item;
1856+
ElementObject *item;
18611857

18621858
if (self->data) {
18631859
if (self->this == self->last) {
@@ -1882,15 +1878,12 @@ treebuilder_handle_end(TreeBuilderObject* self, PyObject* tag)
18821878
return NULL;
18831879
}
18841880

1881+
item = self->last;
1882+
self->last = self->this;
18851883
self->index--;
1886-
1887-
item = PyList_GET_ITEM(self->stack, self->index);
1888-
Py_INCREF(item);
1889-
1890-
Py_DECREF(self->last);
1891-
1892-
self->last = (ElementObject*) self->this;
1893-
self->this = (ElementObject*) item;
1884+
self->this = (ElementObject *) PyList_GET_ITEM(self->stack, self->index);
1885+
Py_INCREF(self->this);
1886+
Py_DECREF(item);
18941887

18951888
if (treebuilder_append_event(self, self->end_event_obj, (PyObject*)self->last) < 0)
18961889
return NULL;

Modules/_sqlite/cursor.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -558,10 +558,10 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject*
558558

559559
if (self->statement) {
560560
(void)pysqlite_statement_reset(self->statement);
561-
Py_DECREF(self->statement);
562561
}
563562

564-
self->statement = (pysqlite_Statement*)pysqlite_cache_get(self->connection->statement_cache, func_args);
563+
Py_SETREF(self->statement,
564+
(pysqlite_Statement *)pysqlite_cache_get(self->connection->statement_cache, func_args));
565565
Py_DECREF(func_args);
566566

567567
if (!self->statement) {

Modules/zlibmodule.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -731,11 +731,9 @@ PyZlib_copy(compobject *self)
731731
}
732732

733733
Py_INCREF(self->unused_data);
734+
Py_SETREF(retval->unused_data, self->unused_data);
734735
Py_INCREF(self->unconsumed_tail);
735-
Py_XDECREF(retval->unused_data);
736-
Py_XDECREF(retval->unconsumed_tail);
737-
retval->unused_data = self->unused_data;
738-
retval->unconsumed_tail = self->unconsumed_tail;
736+
Py_SETREF(retval->unconsumed_tail, self->unconsumed_tail);
739737

740738
/* Mark it as being initialized */
741739
retval->is_initialised = 1;
@@ -782,11 +780,9 @@ PyZlib_uncopy(compobject *self)
782780
}
783781

784782
Py_INCREF(self->unused_data);
783+
Py_SETREF(retval->unused_data, self->unused_data);
785784
Py_INCREF(self->unconsumed_tail);
786-
Py_XDECREF(retval->unused_data);
787-
Py_XDECREF(retval->unconsumed_tail);
788-
retval->unused_data = self->unused_data;
789-
retval->unconsumed_tail = self->unconsumed_tail;
785+
Py_SETREF(retval->unconsumed_tail, self->unconsumed_tail);
790786

791787
/* Mark it as being initialized */
792788
retval->is_initialised = 1;

Objects/exceptions.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -517,12 +517,14 @@ SystemExit_init(PySystemExitObject *self, PyObject *args, PyObject *kwds)
517517

518518
if (size == 0)
519519
return 0;
520-
Py_CLEAR(self->code);
521-
if (size == 1)
522-
self->code = PyTuple_GET_ITEM(args, 0);
523-
else if (size > 1)
524-
self->code = args;
525-
Py_INCREF(self->code);
520+
if (size == 1) {
521+
Py_INCREF(PyTuple_GET_ITEM(args, 0));
522+
Py_SETREF(self->code, PyTuple_GET_ITEM(args, 0));
523+
}
524+
else { /* size > 1 */
525+
Py_INCREF(args);
526+
Py_SETREF(self->code, args);
527+
}
526528
return 0;
527529
}
528530

Python/errors.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -227,14 +227,11 @@ PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb)
227227
tstate = PyThreadState_GET();
228228
if (++tstate->recursion_depth > Py_GetRecursionLimit()) {
229229
--tstate->recursion_depth;
230-
/* throw away the old exception... */
231-
Py_DECREF(*exc);
232-
Py_DECREF(*val);
233-
/* ... and use the recursion error instead */
234-
*exc = PyExc_RuntimeError;
235-
*val = PyExc_RecursionErrorInst;
236-
Py_INCREF(*exc);
237-
Py_INCREF(*val);
230+
/* throw away the old exception and use the recursion error instead */
231+
Py_INCREF(PyExc_RuntimeError);
232+
Py_SETREF(*exc, PyExc_RuntimeError);
233+
Py_INCREF(PyExc_RecursionErrorInst);
234+
Py_SETREF(*val, PyExc_RecursionErrorInst);
238235
/* just keeping the old traceback */
239236
return;
240237
}

0 commit comments

Comments
 (0)