From: Serhiy Storchaka Date: Sun, 10 Apr 2016 15:12:01 +0000 (+0300) Subject: Issue #26200: Added Py_SETREF and replaced Py_XSETREF with Py_SETREF X-Git-Tag: v3.6.0a1~240 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f01e408c1688a207eba18444da8c151c872fba59;p=python Issue #26200: Added Py_SETREF and replaced Py_XSETREF with Py_SETREF in places where Py_DECREF was used. --- f01e408c1688a207eba18444da8c151c872fba59 diff --cc Modules/_ctypes/_ctypes.c index 2bf088a130,7e20301ec1..060dca577c --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@@ -5149,8 -5150,9 +5149,8 @@@ comerror_init(PyObject *self, PyObject if (PyObject_SetAttrString(self, "details", details) < 0) return -1; - bself = (PyBaseExceptionObject *)self; Py_INCREF(args); - Py_XSETREF(((PyBaseExceptionObject *)self)->args, args); - Py_SETREF(bself->args, args); ++ Py_SETREF(((PyBaseExceptionObject *)self)->args, args); return 0; } diff --cc Modules/_datetimemodule.c index e5191a0f16,0e50023eea..261c4bc6e7 --- a/Modules/_datetimemodule.c +++ b/Modules/_datetimemodule.c @@@ -1057,8 -1057,10 +1057,8 @@@ format_utcoffset(char *buf, size_t bufl } /* Offset is normalized, so it is negative if days < 0 */ if (GET_TD_DAYS(offset) < 0) { - PyObject *temp = offset; sign = '-'; - Py_XSETREF(offset, delta_negative((PyDateTime_Delta *)offset)); - offset = delta_negative((PyDateTime_Delta *)offset); - Py_DECREF(temp); ++ Py_SETREF(offset, delta_negative((PyDateTime_Delta *)offset)); if (offset == NULL) return -1; } @@@ -3045,8 -3047,10 +3045,8 @@@ tzinfo_fromutc(PyDateTime_TZInfo *self if (dst == Py_None) goto Inconsistent; if (delta_bool((PyDateTime_Delta *)dst) != 0) { - Py_XSETREF(result, add_datetime_timedelta((PyDateTime_DateTime *)result, - PyObject *temp = result; - result = add_datetime_timedelta((PyDateTime_DateTime *)result, - (PyDateTime_Delta *)dst, 1); - Py_DECREF(temp); ++ Py_SETREF(result, add_datetime_timedelta((PyDateTime_DateTime *)result, + (PyDateTime_Delta *)dst, 1)); if (result == NULL) goto Fail; } @@@ -4445,7 -4469,9 +4445,7 @@@ datetime_subtract(PyObject *left, PyObj return NULL; if (offdiff != NULL) { - Py_XSETREF(result, delta_subtract(result, offdiff)); - PyObject *temp = result; - result = delta_subtract(result, offdiff); - Py_DECREF(temp); ++ Py_SETREF(result, delta_subtract(result, offdiff)); Py_DECREF(offdiff); } } diff --cc Modules/_elementtree.c index 5c49c151c1,0f1d6a1faa..63639c72e7 --- a/Modules/_elementtree.c +++ b/Modules/_elementtree.c @@@ -1907,90 -1879,92 +1907,90 @@@ element_ass_subscr(PyObject* self_, PyO } static PyObject* -element_getattro(ElementObject* self, PyObject* nameobj) +element_tag_getter(ElementObject *self, void *closure) { - PyObject* res; - char *name = ""; + PyObject *res = self->tag; + Py_INCREF(res); + return res; +} - if (PyUnicode_Check(nameobj)) - name = _PyUnicode_AsString(nameobj); +static PyObject* +element_text_getter(ElementObject *self, void *closure) +{ + PyObject *res = element_get_text(self); + Py_XINCREF(res); + return res; +} - if (name == NULL) - return NULL; +static PyObject* +element_tail_getter(ElementObject *self, void *closure) +{ + PyObject *res = element_get_tail(self); + Py_XINCREF(res); + return res; +} - /* handle common attributes first */ - if (strcmp(name, "tag") == 0) { - res = self->tag; - Py_INCREF(res); - return res; - } else if (strcmp(name, "text") == 0) { - res = element_get_text(self); - Py_XINCREF(res); - return res; +static PyObject* +element_attrib_getter(ElementObject *self, void *closure) +{ + PyObject *res; + if (!self->extra) { + if (create_extra(self, NULL) < 0) + return NULL; } + res = element_get_attrib(self); + Py_XINCREF(res); + return res; +} - /* methods */ - res = PyObject_GenericGetAttr((PyObject*) self, nameobj); - if (res) - return res; - - /* less common attributes */ - if (strcmp(name, "tail") == 0) { - PyErr_Clear(); - res = element_get_tail(self); - } else if (strcmp(name, "attrib") == 0) { - PyErr_Clear(); - if (!self->extra) { - if (create_extra(self, NULL) < 0) - return NULL; - } - res = element_get_attrib(self); +/* macro for setter validation */ +#define _VALIDATE_ATTR_VALUE(V) \ + if ((V) == NULL) { \ + PyErr_SetString( \ + PyExc_AttributeError, \ + "can't delete element attribute"); \ + return -1; \ } - if (!res) - return NULL; - - Py_INCREF(res); - return res; +static int +element_tag_setter(ElementObject *self, PyObject *value, void *closure) +{ + _VALIDATE_ATTR_VALUE(value); + Py_INCREF(value); - Py_XSETREF(self->tag, value); ++ Py_SETREF(self->tag, value); + return 0; } static int -element_setattro(ElementObject* self, PyObject* nameobj, PyObject* value) +element_text_setter(ElementObject *self, PyObject *value, void *closure) { - char *name = ""; + _VALIDATE_ATTR_VALUE(value); + Py_INCREF(value); + Py_DECREF(JOIN_OBJ(self->text)); + self->text = value; + return 0; +} - if (value == NULL) { - PyErr_SetString(PyExc_AttributeError, - "can't delete attribute"); - return -1; - } - if (PyUnicode_Check(nameobj)) - name = _PyUnicode_AsString(nameobj); - if (name == NULL) - return -1; +static int +element_tail_setter(ElementObject *self, PyObject *value, void *closure) +{ + _VALIDATE_ATTR_VALUE(value); + Py_INCREF(value); + Py_DECREF(JOIN_OBJ(self->tail)); + self->tail = value; + return 0; +} - if (strcmp(name, "tag") == 0) { - Py_INCREF(value); - Py_SETREF(self->tag, value); - } else if (strcmp(name, "text") == 0) { - Py_DECREF(JOIN_OBJ(self->text)); - self->text = value; - Py_INCREF(self->text); - } else if (strcmp(name, "tail") == 0) { - Py_DECREF(JOIN_OBJ(self->tail)); - self->tail = value; - Py_INCREF(self->tail); - } else if (strcmp(name, "attrib") == 0) { - if (!self->extra) { - if (create_extra(self, NULL) < 0) - return -1; - } - Py_INCREF(value); - Py_SETREF(self->extra->attrib, value); - } else { - PyErr_SetString(PyExc_AttributeError, - "Can't set arbitrary attributes on Element"); - return -1; +static int +element_attrib_setter(ElementObject *self, PyObject *value, void *closure) +{ + _VALIDATE_ATTR_VALUE(value); + if (!self->extra) { + if (create_extra(self, NULL) < 0) + return -1; } - + Py_INCREF(value); - Py_XSETREF(self->extra->attrib, value); ++ Py_SETREF(self->extra->attrib, value); return 0; } diff --cc Modules/_io/bytesio.c index 84d2c5d813,49174b449e..ca3a639cd0 --- a/Modules/_io/bytesio.c +++ b/Modules/_io/bytesio.c @@@ -96,7 -96,9 +96,7 @@@ unshare_buffer(bytesio *self, size_t si return -1; memcpy(PyBytes_AS_STRING(new_buf), PyBytes_AS_STRING(self->buf), self->string_size); - Py_XSETREF(self->buf, new_buf); - old_buf = self->buf; - self->buf = new_buf; - Py_DECREF(old_buf); ++ Py_SETREF(self->buf, new_buf); return 0; } diff --cc Modules/itertoolsmodule.c index 532ce012b0,781fbcdf1a..1d550fadad --- a/Modules/itertoolsmodule.c +++ b/Modules/itertoolsmodule.c @@@ -3456,8 -3449,11 +3456,8 @@@ accumulate_next(accumulateobject *lz if (newtotal == NULL) return NULL; - oldtotal = lz->total; - lz->total = newtotal; - Py_DECREF(oldtotal); - Py_INCREF(newtotal); - Py_XSETREF(lz->total, newtotal); ++ Py_SETREF(lz->total, newtotal); return newtotal; } diff --cc Objects/listobject.c index 7c02be7276,d688179d6b..6e2d026e95 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@@ -735,7 -739,9 +735,7 @@@ list_ass_item(PyListObject *a, Py_ssize if (v == NULL) return list_ass_slice(a, i, i+1, v); Py_INCREF(v); - Py_XSETREF(a->ob_item[i], v); - old_value = a->ob_item[i]; - a->ob_item[i] = v; - Py_DECREF(old_value); ++ Py_SETREF(a->ob_item[i], v); return 0; }