Skip to content

Commit 726fc13

Browse files
Issue python#20440: More use of Py_SETREF.
This patch is manually crafted and contains changes that couldn't be handled automatically.
2 parents bdb908e + 191321d commit 726fc13

8 files changed

Lines changed: 40 additions & 63 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: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -965,9 +965,8 @@ element_setstate_from_attributes(ElementObject *self,
965965
return NULL;
966966
}
967967

968-
Py_CLEAR(self->tag);
969-
self->tag = tag;
970-
Py_INCREF(self->tag);
968+
Py_INCREF(tag);
969+
Py_SETREF(self->tag, tag);
971970

972971
_clear_joined_ptr(&self->text);
973972
self->text = text ? JOIN_SET(text, PyList_CheckExact(text)) : Py_None;
@@ -1010,9 +1009,8 @@ element_setstate_from_attributes(ElementObject *self,
10101009

10111010
/* Stash attrib. */
10121011
if (attrib) {
1013-
Py_CLEAR(self->extra->attrib);
1014-
self->extra->attrib = attrib;
10151012
Py_INCREF(attrib);
1013+
Py_SETREF(self->extra->attrib, attrib);
10161014
}
10171015

10181016
Py_RETURN_NONE;
@@ -1961,8 +1959,7 @@ element_tag_setter(ElementObject *self, PyObject *value, void *closure)
19611959
{
19621960
_VALIDATE_ATTR_VALUE(value);
19631961
Py_INCREF(value);
1964-
Py_DECREF(self->tag);
1965-
self->tag = value;
1962+
Py_SETREF(self->tag, value);
19661963
return 0;
19671964
}
19681965

@@ -1995,8 +1992,7 @@ element_attrib_setter(ElementObject *self, PyObject *value, void *closure)
19951992
return -1;
19961993
}
19971994
Py_INCREF(value);
1998-
Py_DECREF(self->extra->attrib);
1999-
self->extra->attrib = value;
1995+
Py_SETREF(self->extra->attrib, value);
20001996
return 0;
20011997
}
20021998

@@ -2533,13 +2529,10 @@ treebuilder_handle_start(TreeBuilderObject* self, PyObject* tag,
25332529
}
25342530
self->index++;
25352531

2536-
Py_DECREF(this);
25372532
Py_INCREF(node);
2538-
self->this = node;
2539-
2540-
Py_DECREF(self->last);
2533+
Py_SETREF(self->this, node);
25412534
Py_INCREF(node);
2542-
self->last = node;
2535+
Py_SETREF(self->last, node);
25432536

25442537
if (treebuilder_append_event(self, self->start_event_obj, node) < 0)
25452538
goto error;
@@ -2612,15 +2605,12 @@ treebuilder_handle_end(TreeBuilderObject* self, PyObject* tag)
26122605
return NULL;
26132606
}
26142607

2615-
self->index--;
2616-
2617-
item = PyList_GET_ITEM(self->stack, self->index);
2618-
Py_INCREF(item);
2619-
2620-
Py_DECREF(self->last);
2621-
2608+
item = self->last;
26222609
self->last = self->this;
2623-
self->this = item;
2610+
self->index--;
2611+
self->this = PyList_GET_ITEM(self->stack, self->index);
2612+
Py_INCREF(self->this);
2613+
Py_DECREF(item);
26242614

26252615
if (treebuilder_append_event(self, self->end_event_obj, self->last) < 0)
26262616
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
@@ -14443,8 +14443,8 @@ unicode_format_arg_parse(struct unicode_formatter_t *ctx,
1444314443
if (key == NULL)
1444414444
return -1;
1444514445
if (ctx->args_owned) {
14446-
Py_DECREF(ctx->args);
1444714446
ctx->args_owned = 0;
14447+
Py_DECREF(ctx->args);
1444814448
}
1444914449
ctx->args = PyObject_GetItem(ctx->dict, key);
1445014450
Py_DECREF(key);

Python/ast.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4496,8 +4496,7 @@ fstring_find_literal_and_expr(PyObject *str, Py_ssize_t *ofs, int recurse_lvl,
44964496
return 0;
44974497

44984498
error:
4499-
Py_XDECREF(*literal);
4500-
*literal = NULL;
4499+
Py_CLEAR(*literal);
45014500
return -1;
45024501
}
45034502

@@ -4692,11 +4691,8 @@ FstringParser_ConcatAndDel(FstringParser *state, PyObject *str)
46924691
state->last_str = str;
46934692
} else {
46944693
/* Concatenate this with the previous string. */
4695-
PyObject *temp = PyUnicode_Concat(state->last_str, str);
4696-
Py_DECREF(state->last_str);
4697-
Py_DECREF(str);
4698-
state->last_str = temp;
4699-
if (!temp)
4694+
PyUnicode_AppendAndDel(&state->last_str, str);
4695+
if (!state->last_str)
47004696
return -1;
47014697
}
47024698
FstringParser_check_invariants(state);

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)