Skip to content

Commit 191321d

Browse files
Issue python#20440: More use of Py_SETREF.
This patch is manually crafted and contains changes that couldn't be handled automatically.
1 parent 4a1e70f commit 191321d

7 files changed

Lines changed: 39 additions & 58 deletions

File tree

Modules/_ctypes/_ctypes.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2983,10 +2983,9 @@ PyCFuncPtr_set_restype(PyCFuncPtrObject *self, PyObject *ob)
29832983
"restype must be a type, a callable, or None");
29842984
return -1;
29852985
}
2986-
Py_XDECREF(self->checker);
29872986
Py_INCREF(ob);
29882987
Py_SETREF(self->restype, ob);
2989-
self->checker = PyObject_GetAttrString(ob, "_check_retval_");
2988+
Py_SETREF(self->checker, PyObject_GetAttrString(ob, "_check_retval_"));
29902989
if (self->checker == NULL)
29912990
PyErr_Clear();
29922991
return 0;

Modules/_elementtree.c

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -935,9 +935,8 @@ element_setstate_from_attributes(ElementObject *self,
935935
return NULL;
936936
}
937937

938-
Py_CLEAR(self->tag);
939-
self->tag = tag;
940-
Py_INCREF(self->tag);
938+
Py_INCREF(tag);
939+
Py_SETREF(self->tag, tag);
941940

942941
_clear_joined_ptr(&self->text);
943942
self->text = text ? JOIN_SET(text, PyList_CheckExact(text)) : Py_None;
@@ -980,9 +979,8 @@ element_setstate_from_attributes(ElementObject *self,
980979

981980
/* Stash attrib. */
982981
if (attrib) {
983-
Py_CLEAR(self->extra->attrib);
984-
self->extra->attrib = attrib;
985982
Py_INCREF(attrib);
983+
Py_SETREF(self->extra->attrib, attrib);
986984
}
987985

988986
Py_RETURN_NONE;
@@ -1944,9 +1942,8 @@ element_setattro(ElementObject* self, PyObject* nameobj, PyObject* value)
19441942
return -1;
19451943

19461944
if (strcmp(name, "tag") == 0) {
1947-
Py_DECREF(self->tag);
1948-
self->tag = value;
1949-
Py_INCREF(self->tag);
1945+
Py_INCREF(value);
1946+
Py_SETREF(self->tag, value);
19501947
} else if (strcmp(name, "text") == 0) {
19511948
Py_DECREF(JOIN_OBJ(self->text));
19521949
self->text = value;
@@ -1960,9 +1957,8 @@ element_setattro(ElementObject* self, PyObject* nameobj, PyObject* value)
19601957
if (create_extra(self, NULL) < 0)
19611958
return -1;
19621959
}
1963-
Py_DECREF(self->extra->attrib);
1964-
self->extra->attrib = value;
1965-
Py_INCREF(self->extra->attrib);
1960+
Py_INCREF(value);
1961+
Py_SETREF(self->extra->attrib, value);
19661962
} else {
19671963
PyErr_SetString(PyExc_AttributeError,
19681964
"Can't set arbitrary attributes on Element");
@@ -2554,13 +2550,10 @@ treebuilder_handle_start(TreeBuilderObject* self, PyObject* tag,
25542550
}
25552551
self->index++;
25562552

2557-
Py_DECREF(this);
25582553
Py_INCREF(node);
2559-
self->this = node;
2560-
2561-
Py_DECREF(self->last);
2554+
Py_SETREF(self->this, node);
25622555
Py_INCREF(node);
2563-
self->last = node;
2556+
Py_SETREF(self->last, node);
25642557

25652558
if (treebuilder_append_event(self, self->start_event_obj, node) < 0)
25662559
goto error;
@@ -2633,15 +2626,12 @@ treebuilder_handle_end(TreeBuilderObject* self, PyObject* tag)
26332626
return NULL;
26342627
}
26352628

2636-
self->index--;
2637-
2638-
item = PyList_GET_ITEM(self->stack, self->index);
2639-
Py_INCREF(item);
2640-
2641-
Py_DECREF(self->last);
2642-
2629+
item = self->last;
26432630
self->last = self->this;
2644-
self->this = item;
2631+
self->index--;
2632+
self->this = PyList_GET_ITEM(self->stack, self->index);
2633+
Py_INCREF(self->this);
2634+
Py_DECREF(item);
26452635

26462636
if (treebuilder_append_event(self, self->end_event_obj, self->last) < 0)
26472637
return NULL;

Modules/_sqlite/cursor.c

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

525525
if (self->statement) {
526526
(void)pysqlite_statement_reset(self->statement);
527-
Py_DECREF(self->statement);
528527
}
529528

530-
self->statement = (pysqlite_Statement*)pysqlite_cache_get(self->connection->statement_cache, func_args);
529+
Py_SETREF(self->statement,
530+
(pysqlite_Statement *)pysqlite_cache_get(self->connection->statement_cache, func_args));
531531
Py_DECREF(func_args);
532532

533533
if (!self->statement) {

Modules/zlibmodule.c

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -961,14 +961,11 @@ zlib_Compress_copy_impl(compobject *self)
961961
goto error;
962962
}
963963
Py_INCREF(self->unused_data);
964+
Py_SETREF(retval->unused_data, self->unused_data);
964965
Py_INCREF(self->unconsumed_tail);
966+
Py_SETREF(retval->unconsumed_tail, self->unconsumed_tail);
965967
Py_XINCREF(self->zdict);
966-
Py_XDECREF(retval->unused_data);
967-
Py_XDECREF(retval->unconsumed_tail);
968-
Py_XDECREF(retval->zdict);
969-
retval->unused_data = self->unused_data;
970-
retval->unconsumed_tail = self->unconsumed_tail;
971-
retval->zdict = self->zdict;
968+
Py_SETREF(retval->zdict, self->zdict);
972969
retval->eof = self->eof;
973970

974971
/* Mark it as being initialized */
@@ -1020,14 +1017,11 @@ zlib_Decompress_copy_impl(compobject *self)
10201017
}
10211018

10221019
Py_INCREF(self->unused_data);
1020+
Py_SETREF(retval->unused_data, self->unused_data);
10231021
Py_INCREF(self->unconsumed_tail);
1022+
Py_SETREF(retval->unconsumed_tail, self->unconsumed_tail);
10241023
Py_XINCREF(self->zdict);
1025-
Py_XDECREF(retval->unused_data);
1026-
Py_XDECREF(retval->unconsumed_tail);
1027-
Py_XDECREF(retval->zdict);
1028-
retval->unused_data = self->unused_data;
1029-
retval->unconsumed_tail = self->unconsumed_tail;
1030-
retval->zdict = self->zdict;
1024+
Py_SETREF(retval->zdict, self->zdict);
10311025
retval->eof = self->eof;
10321026

10331027
/* Mark it as being initialized */

Objects/exceptions.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -561,12 +561,14 @@ SystemExit_init(PySystemExitObject *self, PyObject *args, PyObject *kwds)
561561

562562
if (size == 0)
563563
return 0;
564-
Py_CLEAR(self->code);
565-
if (size == 1)
566-
self->code = PyTuple_GET_ITEM(args, 0);
567-
else /* size > 1 */
568-
self->code = args;
569-
Py_INCREF(self->code);
564+
if (size == 1) {
565+
Py_INCREF(PyTuple_GET_ITEM(args, 0));
566+
Py_SETREF(self->code, PyTuple_GET_ITEM(args, 0));
567+
}
568+
else { /* size > 1 */
569+
Py_INCREF(args);
570+
Py_SETREF(self->code, args);
571+
}
570572
return 0;
571573
}
572574

@@ -625,9 +627,8 @@ ImportError_init(PyImportErrorObject *self, PyObject *args, PyObject *kwds)
625627
#define GET_KWD(kwd) { \
626628
kwd = PyDict_GetItemString(kwds, #kwd); \
627629
if (kwd) { \
628-
Py_CLEAR(self->kwd); \
629-
self->kwd = kwd; \
630-
Py_INCREF(self->kwd);\
630+
Py_INCREF(kwd); \
631+
Py_SETREF(self->kwd, kwd); \
631632
if (PyDict_DelItemString(kwds, #kwd)) \
632633
return -1; \
633634
} \

Objects/unicodeobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14191,8 +14191,8 @@ unicode_format_arg_parse(struct unicode_formatter_t *ctx,
1419114191
if (key == NULL)
1419214192
return -1;
1419314193
if (ctx->args_owned) {
14194-
Py_DECREF(ctx->args);
1419514194
ctx->args_owned = 0;
14195+
Py_DECREF(ctx->args);
1419614196
}
1419714197
ctx->args = PyObject_GetItem(ctx->dict, key);
1419814198
Py_DECREF(key);

Python/errors.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -315,14 +315,11 @@ PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb)
315315
tstate = PyThreadState_GET();
316316
if (++tstate->recursion_depth > Py_GetRecursionLimit()) {
317317
--tstate->recursion_depth;
318-
/* throw away the old exception... */
319-
Py_DECREF(*exc);
320-
Py_DECREF(*val);
321-
/* ... and use the recursion error instead */
322-
*exc = PyExc_RecursionError;
323-
*val = PyExc_RecursionErrorInst;
324-
Py_INCREF(*exc);
325-
Py_INCREF(*val);
318+
/* throw away the old exception and use the recursion error instead */
319+
Py_INCREF(PyExc_RecursionError);
320+
Py_SETREF(*exc, PyExc_RecursionError);
321+
Py_INCREF(PyExc_RecursionErrorInst);
322+
Py_SETREF(*val, PyExc_RecursionErrorInst);
326323
/* just keeping the old traceback */
327324
return;
328325
}

0 commit comments

Comments
 (0)