diff --git a/Doc/includes/custom2.c b/Doc/includes/custom2.c index aee9e1bb7f2d74..7757ea69cc30c4 100644 --- a/Doc/includes/custom2.c +++ b/Doc/includes/custom2.c @@ -50,14 +50,10 @@ Custom_init(CustomObject *self, PyObject *args, PyObject *kwds) return -1; if (first) { - tmp = self->first; - self->first = Py_NewRef(first); - Py_XDECREF(tmp); + Py_XSETREF(self->first, Py_NewRef(first)); } if (last) { - tmp = self->last; - self->last = Py_NewRef(last); - Py_XDECREF(tmp); + Py_XSETREF(self->last, Py_NewRef(last)); } return 0; } diff --git a/Doc/includes/custom3.c b/Doc/includes/custom3.c index 8d88bc24511829..f6372148a8f31e 100644 --- a/Doc/includes/custom3.c +++ b/Doc/includes/custom3.c @@ -50,14 +50,10 @@ Custom_init(CustomObject *self, PyObject *args, PyObject *kwds) return -1; if (first) { - tmp = self->first; - self->first = Py_NewRef(first); - Py_DECREF(tmp); + Py_SETREF(self->first, Py_NewRef(first)); } if (last) { - tmp = self->last; - self->last = Py_NewRef(last); - Py_DECREF(tmp); + Py_SETREF(self->last, Py_NewRef(last)); } return 0; } @@ -87,9 +83,7 @@ Custom_setfirst(CustomObject *self, PyObject *value, void *closure) "The first attribute value must be a string"); return -1; } - tmp = self->first; - self->first = Py_NewRef(value); - Py_DECREF(tmp); + Py_SETREF(self->first, Py_NewRef(value)); return 0; } @@ -112,9 +106,7 @@ Custom_setlast(CustomObject *self, PyObject *value, void *closure) "The last attribute value must be a string"); return -1; } - tmp = self->last; - self->last = Py_NewRef(value); - Py_DECREF(tmp); + Py_SETREF(self->last, Py_NewRef(value)); return 0; } diff --git a/Doc/includes/custom4.c b/Doc/includes/custom4.c index ad240ae6a8df7b..7b49c12d1fec33 100644 --- a/Doc/includes/custom4.c +++ b/Doc/includes/custom4.c @@ -66,14 +66,10 @@ Custom_init(CustomObject *self, PyObject *args, PyObject *kwds) return -1; if (first) { - tmp = self->first; - self->first = Py_NewRef(first); - Py_DECREF(tmp); + Py_SETREF(self->first, Py_NewRef(first)); } if (last) { - tmp = self->last; - self->last = Py_NewRef(last); - Py_DECREF(tmp); + Py_SETREF(self->last, Py_NewRef(last)); } return 0; } @@ -102,9 +98,7 @@ Custom_setfirst(CustomObject *self, PyObject *value, void *closure) "The first attribute value must be a string"); return -1; } - Py_INCREF(value); - Py_CLEAR(self->first); - self->first = value; + Py_XSETREF(self->first, Py_NewRef(value)); return 0; } @@ -126,9 +120,7 @@ Custom_setlast(CustomObject *self, PyObject *value, void *closure) "The last attribute value must be a string"); return -1; } - Py_INCREF(value); - Py_CLEAR(self->last); - self->last = value; + Py_XSETREF(self->last, Py_NewRef(value)); return 0; } diff --git a/Modules/_abc.c b/Modules/_abc.c index e6e72427a0481d..656afe43a7e68a 100644 --- a/Modules/_abc.c +++ b/Modules/_abc.c @@ -624,8 +624,7 @@ _abc__abc_instancecheck_impl(PyObject *module, PyObject *self, switch (PyObject_IsTrue(result)) { case -1: - Py_DECREF(result); - result = NULL; + Py_CLEAR(result); break; case 0: Py_DECREF(result); diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c index f1fd271f6d213f..5fa583821889f3 100644 --- a/Modules/_collectionsmodule.c +++ b/Modules/_collectionsmodule.c @@ -1256,7 +1256,6 @@ deque_remove(dequeobject *deque, PyObject *value) static int deque_ass_item(dequeobject *deque, Py_ssize_t i, PyObject *v) { - PyObject *old_value; block *b; Py_ssize_t n, len=Py_SIZE(deque), halflen=(len+1)>>1, index=i; @@ -1282,9 +1281,7 @@ deque_ass_item(dequeobject *deque, Py_ssize_t i, PyObject *v) while (--n >= 0) b = b->leftlink; } - old_value = b->data[i]; - b->data[i] = Py_NewRef(v); - Py_DECREF(old_value); + Py_SETREF(b->data[i], Py_NewRef(v)); return 0; } diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c index 20e8516fcde7fb..5dc241e7fbd57e 100644 --- a/Modules/_datetimemodule.c +++ b/Modules/_datetimemodule.c @@ -1328,8 +1328,7 @@ call_tzname(PyObject *tzinfo, PyObject *tzinfoarg) PyErr_Format(PyExc_TypeError, "tzinfo.tzname() must " "return None or a string, not '%s'", Py_TYPE(result)->tp_name); - Py_DECREF(result); - result = NULL; + Py_CLEAR(result); } return result; @@ -1849,8 +1848,7 @@ delta_to_microseconds(PyDateTime_Delta *self) x2 = PyNumber_Multiply(x1, seconds_per_day); /* days in seconds */ if (x2 == NULL) goto Done; - Py_DECREF(x1); - x1 = NULL; + Py_CLEAR(x1); /* x2 has days in seconds */ x1 = PyLong_FromLong(GET_TD_SECONDS(self)); /* seconds */ @@ -1867,8 +1865,7 @@ delta_to_microseconds(PyDateTime_Delta *self) x1 = PyNumber_Multiply(x3, us_per_second); /* us */ if (x1 == NULL) goto Done; - Py_DECREF(x3); - x3 = NULL; + Py_CLEAR(x3); /* x1 has days+seconds in us */ x2 = PyLong_FromLong(GET_TD_MICROSECONDS(self)); @@ -2038,8 +2035,7 @@ multiply_truedivide_timedelta_float(PyDateTime_Delta *delta, PyObject *floatobj, goto error; } temp = PyNumber_Multiply(pyus_in, PyTuple_GET_ITEM(ratio, op)); - Py_DECREF(pyus_in); - pyus_in = NULL; + Py_CLEAR(pyus_in); if (temp == NULL) goto error; pyus_out = divide_nearest(temp, PyTuple_GET_ITEM(ratio, !op)); @@ -6247,9 +6243,7 @@ datetime_astimezone(PyDateTime_DateTime *self, PyObject *args, PyObject *kw) } else { /* Result is already aware - just replace tzinfo. */ - temp = result->tzinfo; - result->tzinfo = Py_NewRef(PyDateTime_TimeZone_UTC); - Py_DECREF(temp); + Py_SETREF(result->tzinfo, Py_NewRef(PyDateTime_TimeZone_UTC)); } /* Attach new tzinfo and let fromutc() do the rest. */ diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c index 7b0977931f2ae6..bd542d18534375 100644 --- a/Modules/_elementtree.c +++ b/Modules/_elementtree.c @@ -345,8 +345,7 @@ get_attrib_from_keywords(PyObject *kwds) } attrib = PyDict_Copy(attrib); if (attrib && PyDict_DelItem(kwds, attrib_str) < 0) { - Py_DECREF(attrib); - attrib = NULL; + Py_CLEAR(attrib); } } else if (!PyErr_Occurred()) { @@ -537,8 +536,7 @@ element_get_text(ElementObject* self) if (!tmp) return NULL; self->text = tmp; - Py_DECREF(res); - res = tmp; + Py_SETREF(res, tmp); } } @@ -559,8 +557,7 @@ element_get_tail(ElementObject* self) if (!tmp) return NULL; self->tail = tmp; - Py_DECREF(res); - res = tmp; + Py_SETREF(res, tmp); } } diff --git a/Modules/_functoolsmodule.c b/Modules/_functoolsmodule.c index 3aef296745ada1..3130306edfe7d7 100644 --- a/Modules/_functoolsmodule.c +++ b/Modules/_functoolsmodule.c @@ -1242,8 +1242,7 @@ lru_cache_clear_list(lru_list_elem *link) { while (link != NULL) { lru_list_elem *next = link->next; - Py_DECREF(link); - link = next; + Py_SETREF(link, next); } } diff --git a/Modules/_io/_iomodule.c b/Modules/_io/_iomodule.c index ccd40ab8bca451..b3b589c2ee3885 100644 --- a/Modules/_io/_iomodule.c +++ b/Modules/_io/_iomodule.c @@ -334,8 +334,7 @@ _io_open_impl(PyObject *module, PyObject *file, const char *mode, goto error; result = raw; - Py_DECREF(path_or_fd); - path_or_fd = NULL; + Py_CLEAR(path_or_fd); modeobj = PyUnicode_FromString(mode); if (modeobj == NULL) diff --git a/Modules/_io/stringio.c b/Modules/_io/stringio.c index 5c3bf353680ea4..ae6c3125a2d9da 100644 --- a/Modules/_io/stringio.c +++ b/Modules/_io/stringio.c @@ -193,8 +193,7 @@ write_str(stringio *self, PyObject *obj) if (self->writenl) { PyObject *translated = PyUnicode_Replace( decoded, &_Py_STR(newline), self->writenl, -1); - Py_DECREF(decoded); - decoded = translated; + Py_SETREF(decoded, translated); } if (decoded == NULL) return -1; diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c index 8b5d00fe4aafd7..ff903e9341de27 100644 --- a/Modules/_io/textio.c +++ b/Modules/_io/textio.c @@ -320,8 +320,7 @@ _PyIncrementalNewlineDecoder_decode(PyObject *myself, out = PyUnicode_DATA(modified); PyUnicode_WRITE(kind, out, 0, '\r'); memcpy(out + kind, PyUnicode_DATA(output), kind * output_len); - Py_DECREF(output); - output = modified; /* output remains ready */ + Py_SETREF(output, modified); /* output remains ready */ self->pendingcr = 0; output_len++; } @@ -336,8 +335,7 @@ _PyIncrementalNewlineDecoder_decode(PyObject *myself, PyObject *modified = PyUnicode_Substring(output, 0, output_len -1); if (modified == NULL) goto error; - Py_DECREF(output); - output = modified; + Py_SETREF(output, modified); self->pendingcr = 1; } } @@ -865,8 +863,7 @@ _textiowrapper_set_decoder(textio *self, PyObject *codec_info, self->decoder, self->readtranslate ? Py_True : Py_False, NULL); if (incrementalDecoder == NULL) return -1; - Py_CLEAR(self->decoder); - self->decoder = incrementalDecoder; + Py_XSETREF(self->decoder, incrementalDecoder); } return 0; diff --git a/Modules/_json.c b/Modules/_json.c index 06f232f7d7de9b..81431aa1041c55 100644 --- a/Modules/_json.c +++ b/Modules/_json.c @@ -709,9 +709,7 @@ _parse_object_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ss if (memokey == NULL) { goto bail; } - Py_INCREF(memokey); - Py_DECREF(key); - key = memokey; + Py_SETREF(key, Py_NewRef(memokey)); idx = next_idx; /* skip whitespace between key and : delimiter, read :, skip whitespace */ diff --git a/Modules/_pickle.c b/Modules/_pickle.c index db62094c5a5af9..bb07e76959ee9c 100644 --- a/Modules/_pickle.c +++ b/Modules/_pickle.c @@ -1829,8 +1829,7 @@ get_deep_attribute(PyObject *obj, PyObject *names, PyObject **pparent) n = PyList_GET_SIZE(names); for (i = 0; i < n; i++) { PyObject *name = PyList_GET_ITEM(names, i); - Py_XDECREF(parent); - parent = obj; + Py_XSETREF(parent, obj); (void)_PyObject_LookupAttr(parent, name, &obj); if (obj == NULL) { Py_DECREF(parent); @@ -3717,9 +3716,7 @@ save_global(PicklerObject *self, PyObject *obj, PyObject *name) else { gen_global: if (parent == module) { - Py_INCREF(lastname); - Py_DECREF(global_name); - global_name = lastname; + Py_SETREF(global_name, Py_NewRef(lastname)); } if (self->proto >= 4) { const char stack_global_op = STACK_GLOBAL; @@ -4347,8 +4344,7 @@ save(PicklerObject *self, PyObject *obj, int pers_save) if (reduce_value != Py_NotImplemented) { goto reduce; } - Py_DECREF(reduce_value); - reduce_value = NULL; + Py_CLEAR(reduce_value); } if (type == &PyType_Type) { diff --git a/Modules/_scproxy.c b/Modules/_scproxy.c index 4c1f1aa300c717..51f7df6ccd8721 100644 --- a/Modules/_scproxy.c +++ b/Modules/_scproxy.c @@ -84,7 +84,7 @@ get_proxy_settings(PyObject* Py_UNUSED(mod), PyObject *Py_UNUSED(ignored)) if (v == NULL) goto error; r = PyDict_SetItemString(result, "exclude_simple", v); - Py_DECREF(v); v = NULL; + Py_CLEAR(v); if (r == -1) goto error; anArray = CFDictionaryGetValue(proxyDict, diff --git a/Modules/_sqlite/cursor.c b/Modules/_sqlite/cursor.c index 7844b6e26cdbda..a4e22bb4a2b58d 100644 --- a/Modules/_sqlite/cursor.c +++ b/Modules/_sqlite/cursor.c @@ -1123,8 +1123,7 @@ pysqlite_cursor_iternext(pysqlite_Cursor *self) PyObject *factory = self->row_factory; PyObject *args[] = { (PyObject *)self, row, }; PyObject *new_row = PyObject_Vectorcall(factory, args, 2, NULL); - Py_DECREF(row); - row = new_row; + Py_SETREF(row, new_row); } return row; } diff --git a/Modules/_struct.c b/Modules/_struct.c index 2f2eb25d984230..98157e18b2b421 100644 --- a/Modules/_struct.c +++ b/Modules/_struct.c @@ -2164,8 +2164,7 @@ cache_struct_converter(PyObject *module, PyObject *fmt, PyStructObject **ptr) _structmodulestate *state = get_struct_state(module); if (fmt == NULL) { - Py_DECREF(*ptr); - *ptr = NULL; + Py_CLEAR(*ptr); return 1; } diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c index 2c21782dde16c8..c559141b7c53c4 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -574,8 +574,7 @@ test_long_and_overflow(PyObject *self, PyObject *Py_UNUSED(ignored)) } temp = PyNumber_Add(num, one); Py_DECREF(one); - Py_DECREF(num); - num = temp; + Py_SETREF(num, temp); if (num == NULL) return NULL; overflow = 0; @@ -618,8 +617,7 @@ test_long_and_overflow(PyObject *self, PyObject *Py_UNUSED(ignored)) } temp = PyNumber_Subtract(num, one); Py_DECREF(one); - Py_DECREF(num); - num = temp; + Py_SETREF(num, temp); if (num == NULL) return NULL; overflow = 0; @@ -738,8 +736,7 @@ test_long_long_and_overflow(PyObject *self, PyObject *Py_UNUSED(ignored)) } temp = PyNumber_Add(num, one); Py_DECREF(one); - Py_DECREF(num); - num = temp; + Py_SETREF(num, temp); if (num == NULL) return NULL; overflow = 0; @@ -782,8 +779,7 @@ test_long_long_and_overflow(PyObject *self, PyObject *Py_UNUSED(ignored)) } temp = PyNumber_Subtract(num, one); Py_DECREF(one); - Py_DECREF(num); - num = temp; + Py_SETREF(num, temp); if (num == NULL) return NULL; overflow = 0; diff --git a/Modules/_testinternalcapi.c b/Modules/_testinternalcapi.c index cec114cb5919da..b14b8ac3c74054 100644 --- a/Modules/_testinternalcapi.c +++ b/Modules/_testinternalcapi.c @@ -523,8 +523,7 @@ set_eval_frame_record(PyObject *self, PyObject *list) PyErr_SetString(PyExc_TypeError, "argument must be a list"); return NULL; } - Py_CLEAR(record_list); - record_list = Py_NewRef(list); + Py_XSETREF(record_list, Py_NewRef(list)); _PyInterpreterState_SetEvalFrameFunc(PyInterpreterState_Get(), record_eval); Py_RETURN_NONE; } diff --git a/Modules/_tkinter.c b/Modules/_tkinter.c index a5d5428eaf5077..e2d0114f9a8822 100644 --- a/Modules/_tkinter.c +++ b/Modules/_tkinter.c @@ -1105,8 +1105,7 @@ fromBignumObj(TkappObject *tkapp, Tcl_Obj *value) PyMem_Free(bytes); if (res != NULL && bigValue.sign == MP_NEG) { PyObject *res2 = PyNumber_Negative(res); - Py_DECREF(res); - res = res2; + Py_SETREF(res, res2); } mp_clear(&bigValue); return res; @@ -2164,8 +2163,7 @@ _tkinter_tkapp_splitlist(TkappObject *self, PyObject *arg) for (i = 0; i < argc; i++) { PyObject *s = unicodeFromTclString(argv[i]); if (!s) { - Py_DECREF(v); - v = NULL; + Py_CLEAR(v); goto finally; } PyTuple_SET_ITEM(v, i, s); diff --git a/Modules/_xxsubinterpretersmodule.c b/Modules/_xxsubinterpretersmodule.c index e65137e58fb5bb..0513f1762ff133 100644 --- a/Modules/_xxsubinterpretersmodule.c +++ b/Modules/_xxsubinterpretersmodule.c @@ -2320,8 +2320,7 @@ channel_list_all(PyObject *self, PyObject *Py_UNUSED(ignored)) PyObject *id = (PyObject *)newchannelid(&ChannelIDtype, *cur, 0, &_globals.channels, 0, 0); if (id == NULL) { - Py_DECREF(ids); - ids = NULL; + Py_CLEAR(ids); break; } PyList_SET_ITEM(ids, (Py_ssize_t)i, id); @@ -2383,8 +2382,7 @@ channel_list_interpreters(PyObject *self, PyObject *args, PyObject *kwds) goto finally; except: - Py_XDECREF(ids); - ids = NULL; + Py_CLEAR(ids); finally: return ids; diff --git a/Modules/_zoneinfo.c b/Modules/_zoneinfo.c index 34f5abd343ece0..f17fb8ef108fff 100644 --- a/Modules/_zoneinfo.c +++ b/Modules/_zoneinfo.c @@ -220,8 +220,7 @@ zoneinfo_new_instance(PyTypeObject *type, PyObject *key) } PyObject *rv = PyObject_CallMethod(file_obj, "close", NULL); - Py_DECREF(file_obj); - file_obj = NULL; + Py_CLEAR(file_obj); if (rv == NULL) { goto error; } @@ -231,8 +230,7 @@ zoneinfo_new_instance(PyTypeObject *type, PyObject *key) goto cleanup; error: - Py_XDECREF(self); - self = NULL; + Py_CLEAR(self); cleanup: if (file_obj != NULL) { PyObject *exc, *val, *tb; @@ -1074,8 +1072,7 @@ load_data(PyZoneInfo_ZoneInfo *self, PyObject *file_obj) // that the dstoff is set correctly in that case. if (PyObject_IsTrue(tti->dstoff)) { _ttinfo *tti_after = &(self->tzrule_after.std); - Py_DECREF(tti_after->dstoff); - tti_after->dstoff = Py_NewRef(tti->dstoff); + Py_SETREF(tti_after->dstoff, Py_NewRef(tti->dstoff)); } } @@ -2607,14 +2604,11 @@ static PyMethodDef module_methods[] = {{NULL, NULL}}; static void module_free(void *m) { - Py_XDECREF(_tzpath_find_tzfile); - _tzpath_find_tzfile = NULL; + Py_CLEAR(_tzpath_find_tzfile); - Py_XDECREF(_common_mod); - _common_mod = NULL; + Py_CLEAR(_common_mod); - Py_XDECREF(io_open); - io_open = NULL; + Py_CLEAR(io_open); xdecref_ttinfo(&NO_TTINFO); diff --git a/Modules/audioop.c b/Modules/audioop.c index 6d484e8bba3ec7..9325f82f9a17e0 100644 --- a/Modules/audioop.c +++ b/Modules/audioop.c @@ -1471,8 +1471,7 @@ audioop_ratecv_impl(PyObject *module, Py_buffer *fragment, int width, len = (Py_ssize_t)(ncp - PyBytes_AsString(str)); rv = PyBytes_FromStringAndSize (PyBytes_AsString(str), len); - Py_DECREF(str); - str = rv; + Py_SETREF(str, rv); if (str == NULL) goto exit; rv = Py_BuildValue("(O(iO))", str, d, samps); diff --git a/Modules/cjkcodecs/multibytecodec.c b/Modules/cjkcodecs/multibytecodec.c index 6d67fce1da0353..1d1bb18cee6c34 100644 --- a/Modules/cjkcodecs/multibytecodec.c +++ b/Modules/cjkcodecs/multibytecodec.c @@ -980,8 +980,7 @@ _multibytecodec_MultibyteIncrementalEncoder_setstate_impl(MultibyteIncrementalEn goto errorexit; } - Py_CLEAR(self->pending); - self->pending = pending; + Py_XSETREF(self->pending, pending); memcpy(self->state.c, statebytes+1+statebytes[0], sizeof(self->state.c)); @@ -1438,8 +1437,7 @@ mbstreamreader_iread(MultibyteStreamReaderObject *self, memcpy(ctrdata + self->pendingsize, PyBytes_AS_STRING(cres), PyBytes_GET_SIZE(cres)); - Py_DECREF(cres); - cres = ctr; + Py_SETREF(cres, ctr); self->pendingsize = 0; } @@ -1465,8 +1463,7 @@ mbstreamreader_iread(MultibyteStreamReaderObject *self, goto errorexit; } - Py_DECREF(cres); - cres = NULL; + Py_CLEAR(cres); if (sizehint < 0 || buf.writer.pos != 0 || rsize == 0) break; diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c index 65d01425052241..4b0a4d88c435c6 100644 --- a/Modules/itertoolsmodule.c +++ b/Modules/itertoolsmodule.c @@ -834,8 +834,7 @@ teedataobject_safe_decref(PyObject *obj) Py_REFCNT(obj) == 1) { PyObject *nextlink = ((teedataobject *)obj)->nextlink; ((teedataobject *)obj)->nextlink = NULL; - Py_DECREF(obj); - obj = nextlink; + Py_SETREF(obj, nextlink); } Py_XDECREF(obj); } diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index 16a2f45c8b84df..2e58f568ed8c88 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -2069,8 +2069,7 @@ factorial_odd_part(unsigned long n) Py_DECREF(partial); if (tmp == NULL) goto error; - Py_DECREF(inner); - inner = tmp; + Py_SETREF(inner, tmp); /* Now inner is the product of all odd integers j in the range (0, n/2**i], giving the inner product in the formula above. */ @@ -2078,8 +2077,7 @@ factorial_odd_part(unsigned long n) tmp = PyNumber_Multiply(outer, inner); if (tmp == NULL) goto error; - Py_DECREF(outer); - outer = tmp; + Py_SETREF(outer, tmp); } Py_DECREF(inner); return outer; @@ -3154,8 +3152,7 @@ math_prod_impl(PyObject *module, PyObject *iterable, PyObject *start) long i_result = PyLong_AsLongAndOverflow(result, &overflow); /* If this already overflowed, don't even enter the loop. */ if (overflow == 0) { - Py_DECREF(result); - result = NULL; + Py_CLEAR(result); } /* Loop over all the items in the iterable until we finish, we overflow * or we found a non integer element */ @@ -3202,8 +3199,7 @@ math_prod_impl(PyObject *module, PyObject *iterable, PyObject *start) */ if (PyFloat_CheckExact(result)) { double f_result = PyFloat_AS_DOUBLE(result); - Py_DECREF(result); - result = NULL; + Py_CLEAR(result); while(result == NULL) { item = PyIter_Next(iter); if (item == NULL) { @@ -3252,8 +3248,7 @@ math_prod_impl(PyObject *module, PyObject *iterable, PyObject *start) if (item == NULL) { /* error, or end-of-sequence */ if (PyErr_Occurred()) { - Py_DECREF(result); - result = NULL; + Py_CLEAR(result); } break; } diff --git a/Modules/nismodule.c b/Modules/nismodule.c index 39b991162b2761..33bb168067b5c7 100644 --- a/Modules/nismodule.c +++ b/Modules/nismodule.c @@ -458,8 +458,7 @@ nis_maps (PyObject *module, PyObject *args, PyObject *kwdict) if (!str || PyList_Append(list, str) < 0) { Py_XDECREF(str); - Py_DECREF(list); - list = NULL; + Py_CLEAR(list); break; } Py_DECREF(str); diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index 5968c1940e76f4..8399aac9a35cdc 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -1190,8 +1190,7 @@ path_converter(PyObject *o, void *p) } /* still owns a reference to the original object */ - Py_DECREF(o); - o = res; + Py_SETREF(o, res); } if (is_unicode) { @@ -4025,14 +4024,12 @@ _listdir_windows_no_opendir(path_t *path, PyObject *list) Py_SETREF(v, PyUnicode_EncodeFSDefault(v)); } if (v == NULL) { - Py_DECREF(list); - list = NULL; + Py_CLEAR(list); break; } if (PyList_Append(list, v) != 0) { Py_DECREF(v); - Py_DECREF(list); - list = NULL; + Py_CLEAR(list); break; } Py_DECREF(v); @@ -13120,15 +13117,13 @@ os_listxattr_impl(PyObject *module, path_t *path, int follow_symlinks) PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start, trace - start); if (!attribute) { - Py_DECREF(result); - result = NULL; + Py_CLEAR(result); goto exit; } error = PyList_Append(result, attribute); Py_DECREF(attribute); if (error) { - Py_DECREF(result); - result = NULL; + Py_CLEAR(result); goto exit; } start = trace + 1; diff --git a/Objects/abstract.c b/Objects/abstract.c index 8aa3fc17c6341b..deb2cc9ea5074c 100644 --- a/Objects/abstract.c +++ b/Objects/abstract.c @@ -806,8 +806,7 @@ PyObject_Format(PyObject *obj, PyObject *format_spec) PyErr_Format(PyExc_TypeError, "__format__ must return a str, not %.200s", Py_TYPE(result)->tp_name); - Py_DECREF(result); - result = NULL; + Py_CLEAR(result); goto done; } @@ -2791,8 +2790,7 @@ PyObject_GetIter(PyObject *o) "iter() returned non-iterator " "of type '%.100s'", Py_TYPE(res)->tp_name); - Py_DECREF(res); - res = NULL; + Py_CLEAR(res); } return res; } @@ -2812,8 +2810,7 @@ PyObject_GetAIter(PyObject *o) { PyErr_Format(PyExc_TypeError, "aiter() returned not an async iterator of type '%.100s'", Py_TYPE(it)->tp_name); - Py_DECREF(it); - it = NULL; + Py_CLEAR(it); } return it; } diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c index 91c89bbd9005a7..a63f396e022f71 100644 --- a/Objects/bytesobject.c +++ b/Objects/bytesobject.c @@ -2109,9 +2109,7 @@ bytes_translate_impl(PyBytesObject *self, PyObject *table, changed = 1; } if (!changed && PyBytes_CheckExact(input_obj)) { - Py_INCREF(input_obj); - Py_DECREF(result); - result = input_obj; + Py_SETREF(result, Py_NewRef(input_obj)); } PyBuffer_Release(&del_table_view); PyBuffer_Release(&table_view); diff --git a/Objects/capsule.c b/Objects/capsule.c index 606e50e6961133..baaddb3f1f0849 100644 --- a/Objects/capsule.c +++ b/Objects/capsule.c @@ -220,8 +220,7 @@ PyCapsule_Import(const char *name, int no_block) } } else { PyObject *object2 = PyObject_GetAttrString(object, trace); - Py_DECREF(object); - object = object2; + Py_SETREF(object, object2); } if (!object) { goto EXIT; diff --git a/Objects/classobject.c b/Objects/classobject.c index eedf8f0e1e1acf..7b7e78387f08ee 100644 --- a/Objects/classobject.c +++ b/Objects/classobject.c @@ -283,8 +283,7 @@ method_repr(PyMethodObject *a) } if (funcname != NULL && !PyUnicode_Check(funcname)) { - Py_DECREF(funcname); - funcname = NULL; + Py_CLEAR(funcname); } /* XXX Shouldn't use repr()/%R here! */ @@ -484,8 +483,7 @@ instancemethod_repr(PyObject *self) return NULL; } if (funcname != NULL && !PyUnicode_Check(funcname)) { - Py_DECREF(funcname); - funcname = NULL; + Py_CLEAR(funcname); } result = PyUnicode_FromFormat("", diff --git a/Objects/descrobject.c b/Objects/descrobject.c index 550bfb36df0f66..86ff801ddc77e3 100644 --- a/Objects/descrobject.c +++ b/Objects/descrobject.c @@ -906,8 +906,7 @@ descr_new(PyTypeObject *descrtype, PyTypeObject *type, const char *name) descr->d_type = (PyTypeObject*)Py_XNewRef(type); descr->d_name = PyUnicode_InternFromString(name); if (descr->d_name == NULL) { - Py_DECREF(descr); - descr = NULL; + Py_CLEAR(descr); } else { descr->d_qualname = NULL; diff --git a/Objects/fileobject.c b/Objects/fileobject.c index ab67cd23cef3b3..56219a8ad6fa0d 100644 --- a/Objects/fileobject.c +++ b/Objects/fileobject.c @@ -67,8 +67,7 @@ PyFile_GetLine(PyObject *f, int n) } if (result != NULL && !PyBytes_Check(result) && !PyUnicode_Check(result)) { - Py_DECREF(result); - result = NULL; + Py_CLEAR(result); PyErr_SetString(PyExc_TypeError, "object.readline() returned non-string"); } @@ -77,8 +76,7 @@ PyFile_GetLine(PyObject *f, int n) const char *s = PyBytes_AS_STRING(result); Py_ssize_t len = PyBytes_GET_SIZE(result); if (len == 0) { - Py_DECREF(result); - result = NULL; + Py_CLEAR(result); PyErr_SetString(PyExc_EOFError, "EOF when reading a line"); } @@ -88,24 +86,21 @@ PyFile_GetLine(PyObject *f, int n) else { PyObject *v; v = PyBytes_FromStringAndSize(s, len-1); - Py_DECREF(result); - result = v; + Py_SETREF(result, v); } } } if (n < 0 && result != NULL && PyUnicode_Check(result)) { Py_ssize_t len = PyUnicode_GET_LENGTH(result); if (len == 0) { - Py_DECREF(result); - result = NULL; + Py_CLEAR(result); PyErr_SetString(PyExc_EOFError, "EOF when reading a line"); } else if (PyUnicode_READ_CHAR(result, len-1) == '\n') { PyObject *v; v = PyUnicode_Substring(result, 0, len-1); - Py_DECREF(result); - result = v; + Py_SETREF(result, v); } } return result; diff --git a/Objects/floatobject.c b/Objects/floatobject.c index 46016e946ad344..912b742f797d24 100644 --- a/Objects/floatobject.c +++ b/Objects/floatobject.c @@ -531,20 +531,17 @@ float_richcompare(PyObject *v, PyObject *w, int op) temp = _PyLong_Lshift(ww, 1); if (temp == NULL) goto Error; - Py_DECREF(ww); - ww = temp; + Py_SETREF(ww, temp); temp = _PyLong_Lshift(vv, 1); if (temp == NULL) goto Error; - Py_DECREF(vv); - vv = temp; + Py_SETREF(vv, temp); temp = PyNumber_Or(vv, _PyLong_GetOne()); if (temp == NULL) goto Error; - Py_DECREF(vv); - vv = temp; + Py_SETREF(vv, temp); } r = PyObject_RichCompareBool(vv, ww, op); diff --git a/Objects/genobject.c b/Objects/genobject.c index 6661e3fb9461a4..c006f1af2177f9 100644 --- a/Objects/genobject.c +++ b/Objects/genobject.c @@ -491,8 +491,7 @@ _gen_throw(PyGenObject *gen, int close_on_genexit, } else { /* Normalize to raise , */ - Py_XDECREF(val); - val = typ; + Py_XSETREF(val, typ); typ = Py_NewRef(PyExceptionInstance_Class(typ)); if (tb == NULL) diff --git a/Objects/longobject.c b/Objects/longobject.c index bb9e40f862aa11..f4bd981e4b9870 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -2598,8 +2598,7 @@ long_from_non_binary_base(const char *start, const char *end, Py_ssize_t digits, memcpy(tmp->ob_digit, z->ob_digit, sizeof(digit) * size_z); - Py_DECREF(z); - z = tmp; + Py_SETREF(z, tmp); z->ob_digit[size_z] = (digit)c; ++size_z; } @@ -4140,8 +4139,7 @@ l_divmod(PyLongObject *v, PyLongObject *w, (Py_SIZE(mod) > 0 && Py_SIZE(w) < 0)) { PyLongObject *temp; temp = (PyLongObject *) long_add(mod, w); - Py_DECREF(mod); - mod = temp; + Py_SETREF(mod, temp); if (mod == NULL) { Py_DECREF(div); return -1; @@ -4152,8 +4150,7 @@ l_divmod(PyLongObject *v, PyLongObject *w, Py_DECREF(div); return -1; } - Py_DECREF(div); - div = temp; + Py_SETREF(div, temp); } if (pdiv != NULL) *pdiv = div; @@ -4189,8 +4186,7 @@ l_mod(PyLongObject *v, PyLongObject *w, PyLongObject **pmod) (Py_SIZE(mod) > 0 && Py_SIZE(w) < 0)) { PyLongObject *temp; temp = (PyLongObject *) long_add(mod, w); - Py_DECREF(mod); - mod = temp; + Py_SETREF(mod, temp); if (mod == NULL) return -1; } @@ -4430,8 +4426,7 @@ long_true_divide(PyObject *v, PyObject *w) else { PyLongObject *div, *rem; div = x_divrem(x, b, &rem); - Py_DECREF(x); - x = div; + Py_SETREF(x, div); if (x == NULL) goto error; if (Py_SIZE(rem)) @@ -4561,8 +4556,7 @@ long_invmod(PyLongObject *a, PyLongObject *n) if (l_divmod(a, n, &q, &r) == -1) { goto Error; } - Py_DECREF(a); - a = n; + Py_SETREF(a, n); n = r; t = (PyLongObject *)long_mul(q, c); Py_DECREF(q); @@ -4574,8 +4568,7 @@ long_invmod(PyLongObject *a, PyLongObject *n) if (s == NULL) { goto Error; } - Py_DECREF(b); - b = c; + Py_SETREF(b, c); c = s; } /* references now owned: a, b, c, n */ @@ -4670,8 +4663,7 @@ long_pow(PyObject *v, PyObject *w, PyObject *x) temp = (PyLongObject *)_PyLong_Copy(c); if (temp == NULL) goto Error; - Py_DECREF(c); - c = temp; + Py_SETREF(c, temp); temp = NULL; _PyLong_Negate(&c); if (c == NULL) @@ -4691,8 +4683,7 @@ long_pow(PyObject *v, PyObject *w, PyObject *x) temp = (PyLongObject *)_PyLong_Copy(b); if (temp == NULL) goto Error; - Py_DECREF(b); - b = temp; + Py_SETREF(b, temp); temp = NULL; _PyLong_Negate(&b); if (b == NULL) @@ -4701,8 +4692,7 @@ long_pow(PyObject *v, PyObject *w, PyObject *x) temp = long_invmod(a, c); if (temp == NULL) goto Error; - Py_DECREF(a); - a = temp; + Py_SETREF(a, temp); temp = NULL; } @@ -4718,8 +4708,7 @@ long_pow(PyObject *v, PyObject *w, PyObject *x) if (Py_SIZE(a) < 0 || Py_SIZE(a) > Py_SIZE(c)) { if (l_mod(a, c, &temp) < 0) goto Error; - Py_DECREF(a); - a = temp; + Py_SETREF(a, temp); temp = NULL; } } @@ -4786,9 +4775,7 @@ long_pow(PyObject *v, PyObject *w, PyObject *x) * because we're primarily trying to cut overhead for small powers. */ assert(bi); /* else there is no significant bit */ - Py_INCREF(a); - Py_DECREF(z); - z = a; + Py_SETREF(z, Py_NewRef(a)); for (bit = 2; ; bit <<= 1) { if (bit > bi) { /* found the first bit */ assert((bi & bit) == 0); @@ -4874,8 +4861,7 @@ long_pow(PyObject *v, PyObject *w, PyObject *x) temp = (PyLongObject *)long_sub(z, c); if (temp == NULL) goto Error; - Py_DECREF(z); - z = temp; + Py_SETREF(z, temp); temp = NULL; } goto Done; @@ -5444,8 +5430,7 @@ _PyLong_GCD(PyObject *aarg, PyObject *barg) /* no progress; do a Euclidean step */ if (l_mod(a, b, &r) < 0) goto error; - Py_DECREF(a); - a = b; + Py_SETREF(a, b); b = r; alloc_a = alloc_b; alloc_b = Py_SIZE(b); @@ -5766,8 +5751,7 @@ _PyLong_DivmodNear(PyObject *a, PyObject *b) goto error; if (quo_is_neg) { temp = long_neg((PyLongObject*)twice_rem); - Py_DECREF(twice_rem); - twice_rem = temp; + Py_SETREF(twice_rem, temp); if (twice_rem == NULL) goto error; } @@ -5781,8 +5765,7 @@ _PyLong_DivmodNear(PyObject *a, PyObject *b) temp = long_sub(quo, (PyLongObject *)one); else temp = long_add(quo, (PyLongObject *)one); - Py_DECREF(quo); - quo = (PyLongObject *)temp; + Py_SETREF(quo, (PyLongObject *)temp); if (quo == NULL) goto error; /* and remainder */ @@ -5790,8 +5773,7 @@ _PyLong_DivmodNear(PyObject *a, PyObject *b) temp = long_add(rem, (PyLongObject *)b); else temp = long_sub(rem, (PyLongObject *)b); - Py_DECREF(rem); - rem = (PyLongObject *)temp; + Py_SETREF(rem, (PyLongObject *)temp); if (rem == NULL) goto error; } @@ -5857,8 +5839,7 @@ int___round___impl(PyObject *self, PyObject *o_ndigits) /* result = self - divmod_near(self, 10 ** -ndigits)[1] */ temp = long_neg((PyLongObject*)ndigits); - Py_DECREF(ndigits); - ndigits = temp; + Py_SETREF(ndigits, temp); if (ndigits == NULL) return NULL; @@ -5870,21 +5851,18 @@ int___round___impl(PyObject *self, PyObject *o_ndigits) temp = long_pow(result, ndigits, Py_None); Py_DECREF(ndigits); - Py_DECREF(result); - result = temp; + Py_SETREF(result, temp); if (result == NULL) return NULL; temp = _PyLong_DivmodNear(self, result); - Py_DECREF(result); - result = temp; + Py_SETREF(result, temp); if (result == NULL) return NULL; temp = long_sub((PyLongObject *)self, (PyLongObject *)PyTuple_GET_ITEM(result, 1)); - Py_DECREF(result); - result = temp; + Py_SETREF(result, temp); return result; } @@ -5949,8 +5927,7 @@ int_bit_length_impl(PyObject *self) Py_DECREF(x); if (y == NULL) goto error; - Py_DECREF(result); - result = y; + Py_SETREF(result, y); x = (PyLongObject *)PyLong_FromLong((long)msd_bits); if (x == NULL) @@ -5959,8 +5936,7 @@ int_bit_length_impl(PyObject *self) Py_DECREF(x); if (y == NULL) goto error; - Py_DECREF(result); - result = y; + Py_SETREF(result, y); return (PyObject *)result; @@ -6026,8 +6002,7 @@ int_bit_count_impl(PyObject *self) if (y == NULL) { goto error; } - Py_DECREF(result); - result = y; + Py_SETREF(result, y); } return result; diff --git a/Objects/setobject.c b/Objects/setobject.c index e0646768384a88..ae9e9b99446116 100644 --- a/Objects/setobject.c +++ b/Objects/setobject.c @@ -1270,8 +1270,7 @@ set_intersection_multi(PySetObject *so, PyObject *args) Py_DECREF(result); return NULL; } - Py_DECREF(result); - result = newresult; + Py_SETREF(result, newresult); } return result; } diff --git a/Objects/sliceobject.c b/Objects/sliceobject.c index 1da1df3090721f..5694bd9c661fa5 100644 --- a/Objects/sliceobject.c +++ b/Objects/sliceobject.c @@ -448,8 +448,7 @@ _PySlice_GetLongIndices(PySliceObject *self, PyObject *length, if (_PyLong_Sign(start) < 0) { /* start += length */ PyObject *tmp = PyNumber_Add(start, length); - Py_DECREF(start); - start = tmp; + Py_SETREF(start, tmp); if (start == NULL) goto error; @@ -457,9 +456,7 @@ _PySlice_GetLongIndices(PySliceObject *self, PyObject *length, if (cmp_result < 0) goto error; if (cmp_result) { - Py_INCREF(lower); - Py_DECREF(start); - start = lower; + Py_SETREF(start, Py_NewRef(lower)); } } else { @@ -467,9 +464,7 @@ _PySlice_GetLongIndices(PySliceObject *self, PyObject *length, if (cmp_result < 0) goto error; if (cmp_result) { - Py_INCREF(upper); - Py_DECREF(start); - start = upper; + Py_SETREF(start, Py_NewRef(upper)); } } } @@ -486,8 +481,7 @@ _PySlice_GetLongIndices(PySliceObject *self, PyObject *length, if (_PyLong_Sign(stop) < 0) { /* stop += length */ PyObject *tmp = PyNumber_Add(stop, length); - Py_DECREF(stop); - stop = tmp; + Py_SETREF(stop, tmp); if (stop == NULL) goto error; @@ -495,9 +489,7 @@ _PySlice_GetLongIndices(PySliceObject *self, PyObject *length, if (cmp_result < 0) goto error; if (cmp_result) { - Py_INCREF(lower); - Py_DECREF(stop); - stop = lower; + Py_SETREF(stop, Py_NewRef(lower)); } } else { @@ -505,9 +497,7 @@ _PySlice_GetLongIndices(PySliceObject *self, PyObject *length, if (cmp_result < 0) goto error; if (cmp_result) { - Py_INCREF(upper); - Py_DECREF(stop); - stop = upper; + Py_SETREF(stop, Py_NewRef(upper)); } } } diff --git a/Objects/stringlib/unicode_format.h b/Objects/stringlib/unicode_format.h index e970588a7e34bc..ccd7c77c0a03fd 100644 --- a/Objects/stringlib/unicode_format.h +++ b/Objects/stringlib/unicode_format.h @@ -473,8 +473,7 @@ get_field_object(SubString *input, PyObject *args, PyObject *kwargs, goto error; /* assign to obj */ - Py_DECREF(obj); - obj = tmp; + Py_SETREF(obj, tmp); } /* end of iterator, this is the non-error case */ if (ok == 1) @@ -825,8 +824,7 @@ output_markup(SubString *field_name, SubString *format_spec, goto done; /* do the assignment, transferring ownership: fieldobj = tmp */ - Py_DECREF(fieldobj); - fieldobj = tmp; + Py_SETREF(fieldobj, tmp); tmp = NULL; } diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 1e58d32f496205..313ca2a2d8b920 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -1212,8 +1212,7 @@ type_repr(PyTypeObject *type) if (mod == NULL) PyErr_Clear(); else if (!PyUnicode_Check(mod)) { - Py_DECREF(mod); - mod = NULL; + Py_CLEAR(mod); } name = type_qualname(type, NULL); if (name == NULL) { @@ -1287,8 +1286,7 @@ type_call(PyTypeObject *type, PyObject *args, PyObject *kwds) int res = type->tp_init(obj, args, kwds); if (res < 0) { assert(_PyErr_Occurred(tstate)); - Py_DECREF(obj); - obj = NULL; + Py_CLEAR(obj); } else { assert(!_PyErr_Occurred(tstate)); @@ -5006,8 +5004,7 @@ object_repr(PyObject *self) if (mod == NULL) PyErr_Clear(); else if (!PyUnicode_Check(mod)) { - Py_DECREF(mod); - mod = NULL; + Py_CLEAR(mod); } name = type_qualname(type, NULL); if (name == NULL) { @@ -5967,8 +5964,7 @@ object___dir___impl(PyObject *self) else { /* Copy __dict__ to avoid mutating it. */ PyObject *temp = PyDict_Copy(dict); - Py_DECREF(dict); - dict = temp; + Py_SETREF(dict, temp); } if (dict == NULL) @@ -8107,8 +8103,7 @@ slot_tp_hash(PyObject *self) func = lookup_maybe_method(self, &_Py_ID(__hash__), &unbound); if (func == Py_None) { - Py_DECREF(func); - func = NULL; + Py_CLEAR(func); } if (func == NULL) { @@ -9376,8 +9371,7 @@ super_getattro(PyObject *self, PyObject *name) (See SF ID #743627) */ (su->obj == (PyObject *)starttype) ? NULL : su->obj, (PyObject *)starttype); - Py_DECREF(res); - res = res2; + Py_SETREF(res, res2); } Py_DECREF(mro); diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index b1acfc71379cd5..55f029dd504ca0 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -13572,8 +13572,7 @@ _PyUnicode_FormatLong(PyObject *val, int alt, int prec, int type) for (i = 0; i < numdigits; i++) *b1++ = *buf++; *b1 = '\0'; - Py_DECREF(result); - result = r1; + Py_SETREF(result, r1); buf = PyBytes_AS_STRING(result); len = numnondigits + prec; } @@ -13590,8 +13589,7 @@ _PyUnicode_FormatLong(PyObject *val, int alt, int prec, int type) || buf != PyUnicode_DATA(result)) { PyObject *unicode; unicode = _PyUnicode_FromASCII(buf, len); - Py_DECREF(result); - result = unicode; + Py_SETREF(result, unicode); } else if (len != PyUnicode_GET_LENGTH(result)) { if (PyUnicode_Resize(&result, len) < 0) diff --git a/Objects/weakrefobject.c b/Objects/weakrefobject.c index ff284c882b0f1c..bd7720e2753307 100644 --- a/Objects/weakrefobject.c +++ b/Objects/weakrefobject.c @@ -824,8 +824,7 @@ PyWeakref_NewRef(PyObject *ob, PyObject *callback) during GC. Return that one instead of this one to avoid violating the invariants of the list of weakrefs for ob. */ - Py_DECREF(result); - result = (PyWeakReference*)Py_NewRef(ref); + Py_SETREF(result, (PyWeakReference*)Py_NewRef(ref)); } } else { @@ -888,8 +887,7 @@ PyWeakref_NewProxy(PyObject *ob, PyObject *callback) during GC. Return that one instead of this one to avoid violating the invariants of the list of weakrefs for ob. */ - Py_DECREF(result); - result = (PyWeakReference*)Py_NewRef(proxy); + Py_SETREF(result, (PyWeakReference*)Py_NewRef(proxy)); goto skip_insert; } prev = ref; diff --git a/Python/_warnings.c b/Python/_warnings.c index d703e1e6d843a5..046c37eb49bac0 100644 --- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -801,8 +801,7 @@ next_external_frame(PyFrameObject *frame) { do { PyFrameObject *back = PyFrame_GetBack(frame); - Py_DECREF(frame); - frame = back; + Py_SETREF(frame, back); } while (frame != NULL && is_internal_frame(frame)); return frame; @@ -828,8 +827,7 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno, if (stack_level <= 0 || is_internal_frame(f)) { while (--stack_level > 0 && f != NULL) { PyFrameObject *back = PyFrame_GetBack(f); - Py_DECREF(f); - f = back; + Py_SETREF(f, back); } } else { diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index 119e21ae0da290..abf6803a7a2a36 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -168,8 +168,7 @@ builtin___build_class__(PyObject *self, PyObject *const *args, Py_ssize_t nargs, goto error; } if (winner != meta) { - Py_DECREF(meta); - meta = Py_NewRef(winner); + Py_SETREF(meta, Py_NewRef(winner)); } } /* else: meta is not a class, so we cannot do the metaclass @@ -219,8 +218,7 @@ builtin___build_class__(PyObject *self, PyObject *const *args, Py_ssize_t nargs, "__class__ set to %.200R defining %.200R as %.200R"; PyErr_Format(PyExc_TypeError, msg, cell_cls, name, cls); } - Py_DECREF(cls); - cls = NULL; + Py_CLEAR(cls); goto error; } } @@ -2484,8 +2482,7 @@ builtin_sum_impl(PyObject *module, PyObject *iterable, PyObject *start) long i_result = PyLong_AsLongAndOverflow(result, &overflow); /* If this already overflowed, don't even enter the loop. */ if (overflow == 0) { - Py_DECREF(result); - result = NULL; + Py_CLEAR(result); } while(result == NULL) { item = PyIter_Next(iter); @@ -2535,8 +2532,7 @@ builtin_sum_impl(PyObject *module, PyObject *iterable, PyObject *start) if (PyFloat_CheckExact(result)) { double f_result = PyFloat_AS_DOUBLE(result); - Py_DECREF(result); - result = NULL; + Py_CLEAR(result); while(result == NULL) { item = PyIter_Next(iter); if (item == NULL) { @@ -2583,8 +2579,7 @@ builtin_sum_impl(PyObject *module, PyObject *iterable, PyObject *start) if (item == NULL) { /* error, or end-of-sequence */ if (PyErr_Occurred()) { - Py_DECREF(result); - result = NULL; + Py_CLEAR(result); } break; } diff --git a/Python/compile.c b/Python/compile.c index 9226bc233ead74..366321143a54aa 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -8280,9 +8280,7 @@ merge_const_one(PyObject *const_cache, PyObject **obj) t = PyTuple_GET_ITEM(t, 1); } - Py_INCREF(t); - Py_DECREF(*obj); - *obj = t; + Py_SETREF(*obj, Py_NewRef(t)); return 1; } diff --git a/Python/errors.c b/Python/errors.c index d74ac347484fed..3bce8c197d9130 100644 --- a/Python/errors.c +++ b/Python/errors.c @@ -37,8 +37,7 @@ _PyErr_Restore(PyThreadState *tstate, PyObject *type, PyObject *value, if (traceback != NULL && !PyTraceBack_Check(traceback)) { /* XXX Should never happen -- fatal error instead? */ /* Well, it could be None. */ - Py_DECREF(traceback); - traceback = NULL; + Py_CLEAR(traceback); } /* Save these in locals to safeguard against recursive @@ -353,16 +352,13 @@ _PyErr_NormalizeException(PyThreadState *tstate, PyObject **exc, if (fixed_value == NULL) { goto error; } - Py_DECREF(value); - value = fixed_value; + Py_SETREF(value, fixed_value); } /* If the class of the instance doesn't exactly match the class of the type, believe the instance. */ else if (inclass != type) { - Py_INCREF(inclass); - Py_DECREF(type); - type = inclass; + Py_SETREF(type, Py_NewRef(inclass)); } } *exc = type; diff --git a/Python/hamt.c b/Python/hamt.c index c4e47eb9e5703a..8cb94641bef251 100644 --- a/Python/hamt.c +++ b/Python/hamt.c @@ -1354,8 +1354,7 @@ hamt_node_collision_assoc(PyHamtNode_Collision *self, } /* Replace the old value with the new value for the our key. */ - Py_DECREF(new_node->c_array[val_idx]); - new_node->c_array[val_idx] = Py_NewRef(val); + Py_SETREF(new_node->c_array[val_idx], Py_NewRef(val)); return (PyHamtNode *)new_node; diff --git a/Python/marshal.c b/Python/marshal.c index 86cf57daa77025..904dd694fafc98 100644 --- a/Python/marshal.c +++ b/Python/marshal.c @@ -1217,8 +1217,7 @@ r_object(RFILE *p) if (!PyErr_Occurred()) PyErr_SetString(PyExc_TypeError, "NULL object in marshal data for tuple"); - Py_DECREF(v); - v = NULL; + Py_CLEAR(v); break; } PyTuple_SET_ITEM(v, i, v2); @@ -1244,8 +1243,7 @@ r_object(RFILE *p) if (!PyErr_Occurred()) PyErr_SetString(PyExc_TypeError, "NULL object in marshal data for list"); - Py_DECREF(v); - v = NULL; + Py_CLEAR(v); break; } PyList_SET_ITEM(v, i, v2); @@ -1277,8 +1275,7 @@ r_object(RFILE *p) Py_DECREF(val); } if (PyErr_Occurred()) { - Py_DECREF(v); - v = NULL; + Py_CLEAR(v); } retval = v; break; @@ -1322,8 +1319,7 @@ r_object(RFILE *p) if (!PyErr_Occurred()) PyErr_SetString(PyExc_TypeError, "NULL object in marshal data for set"); - Py_DECREF(v); - v = NULL; + Py_CLEAR(v); break; } if (PySet_Add(v, v2) == -1) { diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 70872222eb6458..35292b6478a833 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -718,8 +718,7 @@ _Py_HandleSystemExit(int *exitcode_p) /* The error code should be in the `code' attribute. */ PyObject *code = PyObject_GetAttr(value, &_Py_ID(code)); if (code) { - Py_DECREF(value); - value = code; + Py_SETREF(value, code); if (value == Py_None) goto done; } diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 6f0a126a62277b..88f806e616f27e 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -198,8 +198,7 @@ sys_audit_tstate(PyThreadState *ts, const char *event, eventArgs = _Py_VaBuildValue_SizeT(argFormat, vargs); if (eventArgs && !PyTuple_Check(eventArgs)) { PyObject *argTuple = PyTuple_Pack(1, eventArgs); - Py_DECREF(eventArgs); - eventArgs = argTuple; + Py_SETREF(eventArgs, argTuple); } } else { diff --git a/Python/traceback.c b/Python/traceback.c index 356e64364832aa..da26c9b260a3bd 100644 --- a/Python/traceback.c +++ b/Python/traceback.c @@ -136,9 +136,7 @@ tb_next_set(PyTracebackObject *self, PyObject *new_next, void *Py_UNUSED(_)) cursor = cursor->tb_next; } - PyObject *old_next = (PyObject*)self->tb_next; - self->tb_next = (PyTracebackObject *)Py_XNewRef(new_next); - Py_XDECREF(old_next); + Py_XSETREF(self->tb_next, (PyTracebackObject *)Py_XNewRef(new_next)); return 0; } @@ -533,8 +531,7 @@ display_source_line_with_margin(PyObject *f, PyObject *filename, int lineno, int PyObject *truncated; truncated = PyUnicode_Substring(lineobj, i, PyUnicode_GET_LENGTH(lineobj)); if (truncated) { - Py_DECREF(lineobj); - lineobj = truncated; + Py_SETREF(lineobj, truncated); } else { PyErr_Clear(); }