Issue #20440: Massive replacing unsafe attribute setting code with special
macro Py_SETREF.
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
index d8fb96b..51d0871 100644
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -3520,8 +3520,7 @@
/* Multiple references, need to create new object */
PyObject *v;
v = bytes_concat(*pv, w);
- Py_DECREF(*pv);
- *pv = v;
+ Py_SETREF(*pv, v);
}
}
diff --git a/Objects/descrobject.c b/Objects/descrobject.c
index 9ffbca7..da11f6b 100644
--- a/Objects/descrobject.c
+++ b/Objects/descrobject.c
@@ -1509,8 +1509,7 @@
PyObject *get_doc = _PyObject_GetAttrId(get, &PyId___doc__);
if (get_doc) {
if (Py_TYPE(self) == &PyProperty_Type) {
- Py_XDECREF(prop->prop_doc);
- prop->prop_doc = get_doc;
+ Py_SETREF(prop->prop_doc, get_doc);
}
else {
/* If this is a property subclass, put __doc__
diff --git a/Objects/exceptions.c b/Objects/exceptions.c
index f5a1a2b..62ea378 100644
--- a/Objects/exceptions.c
+++ b/Objects/exceptions.c
@@ -236,8 +236,7 @@
}
Py_XINCREF(tb);
- Py_XDECREF(self->traceback);
- self->traceback = tb;
+ Py_SETREF(self->traceback, tb);
return 0;
}
diff --git a/Objects/frameobject.c b/Objects/frameobject.c
index 172f2cb..37e626d 100644
--- a/Objects/frameobject.c
+++ b/Objects/frameobject.c
@@ -857,8 +857,7 @@
}
} else if (values[j] != value) {
Py_XINCREF(value);
- Py_XDECREF(values[j]);
- values[j] = value;
+ Py_SETREF(values[j], value);
}
Py_XDECREF(value);
}
diff --git a/Objects/funcobject.c b/Objects/funcobject.c
index b043934..13daaba 100644
--- a/Objects/funcobject.c
+++ b/Objects/funcobject.c
@@ -127,8 +127,7 @@
PyErr_SetString(PyExc_SystemError, "non-tuple default args");
return -1;
}
- Py_XDECREF(((PyFunctionObject *) op) -> func_defaults);
- ((PyFunctionObject *) op) -> func_defaults = defaults;
+ Py_SETREF(((PyFunctionObject *)op)->func_defaults, defaults);
return 0;
}
@@ -159,8 +158,7 @@
"non-dict keyword only default args");
return -1;
}
- Py_XDECREF(((PyFunctionObject *)op) -> func_kwdefaults);
- ((PyFunctionObject *) op) -> func_kwdefaults = defaults;
+ Py_SETREF(((PyFunctionObject *)op)->func_kwdefaults, defaults);
return 0;
}
@@ -192,8 +190,7 @@
closure->ob_type->tp_name);
return -1;
}
- Py_XDECREF(((PyFunctionObject *) op) -> func_closure);
- ((PyFunctionObject *) op) -> func_closure = closure;
+ Py_SETREF(((PyFunctionObject *)op)->func_closure, closure);
return 0;
}
@@ -224,8 +221,7 @@
"non-dict annotations");
return -1;
}
- Py_XDECREF(((PyFunctionObject *)op) -> func_annotations);
- ((PyFunctionObject *) op) -> func_annotations = annotations;
+ Py_SETREF(((PyFunctionObject *)op)->func_annotations, annotations);
return 0;
}
@@ -531,8 +527,7 @@
if (name != Py_None) {
Py_INCREF(name);
- Py_DECREF(newfunc->func_name);
- newfunc->func_name = name;
+ Py_SETREF(newfunc->func_name, name);
}
if (defaults != Py_None) {
Py_INCREF(defaults);
diff --git a/Objects/moduleobject.c b/Objects/moduleobject.c
index 7b41b0b..24c5f4c 100644
--- a/Objects/moduleobject.c
+++ b/Objects/moduleobject.c
@@ -69,8 +69,7 @@
return -1;
if (PyUnicode_CheckExact(name)) {
Py_INCREF(name);
- Py_XDECREF(mod->md_name);
- mod->md_name = name;
+ Py_SETREF(mod->md_name, name);
}
return 0;
diff --git a/Objects/rangeobject.c b/Objects/rangeobject.c
index da1d703..f858cd2 100644
--- a/Objects/rangeobject.c
+++ b/Objects/rangeobject.c
@@ -1066,8 +1066,7 @@
result = PyNumber_Add(r->start, product);
Py_DECREF(product);
if (result) {
- Py_DECREF(r->index);
- r->index = new_index;
+ Py_SETREF(r->index, new_index);
}
else {
Py_DECREF(new_index);
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index 0b94fc5..5b858bd 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -315,9 +315,8 @@
are borrowed reference */
for (i = 0; i < (1 << MCACHE_SIZE_EXP); i++) {
method_cache[i].value = NULL;
- Py_XDECREF(method_cache[i].name);
- method_cache[i].name = Py_None;
Py_INCREF(Py_None);
+ Py_SETREF(method_cache[i].name, Py_None);
}
/* mark all version tags as invalid */
PyType_Modified(&PyBaseObject_Type);
@@ -462,8 +461,7 @@
et = (PyHeapTypeObject*)type;
Py_INCREF(value);
- Py_DECREF(et->ht_qualname);
- et->ht_qualname = value;
+ Py_SETREF(et->ht_qualname, value);
return 0;
}
@@ -2910,8 +2908,7 @@
else
method_cache_misses++;
#endif
- Py_DECREF(method_cache[h].name);
- method_cache[h].name = name;
+ Py_SETREF(method_cache[h].name, name);
}
return res;
}
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index 0b78301..c5b35e1 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -1665,8 +1665,7 @@
_Py_INCREF_UNICODE_EMPTY();
if (!unicode_empty)
return -1;
- Py_DECREF(*p_unicode);
- *p_unicode = unicode_empty;
+ Py_SETREF(*p_unicode, unicode_empty);
return 0;
}
@@ -1674,8 +1673,7 @@
PyObject *copy = resize_copy(unicode, length);
if (copy == NULL)
return -1;
- Py_DECREF(*p_unicode);
- *p_unicode = copy;
+ Py_SETREF(*p_unicode, copy);
return 0;
}
@@ -13322,8 +13320,7 @@
return -1;
_PyUnicode_FastCopyCharacters(newbuffer, 0,
writer->buffer, 0, writer->pos);
- Py_DECREF(writer->buffer);
- writer->buffer = newbuffer;
+ Py_SETREF(writer->buffer, newbuffer);
}
_PyUnicodeWriter_Update(writer);
return 0;
@@ -15009,8 +15006,7 @@
if (t) {
Py_INCREF(t);
- Py_DECREF(*p);
- *p = t;
+ Py_SETREF(*p, t);
return;
}