bpo-39573: Use Py_SET_SIZE() function (GH-18402)

Replace direct acccess to PyVarObject.ob_size with usage of
the Py_SET_SIZE() function.
diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c
index a019b49..b2808c0 100644
--- a/Objects/bytearrayobject.c
+++ b/Objects/bytearrayobject.c
@@ -148,7 +148,7 @@
             memcpy(new->ob_bytes, bytes, size);
         new->ob_bytes[size] = '\0';  /* Trailing null byte */
     }
-    Py_SIZE(new) = size;
+    Py_SET_SIZE(new, size);
     new->ob_alloc = alloc;
     new->ob_start = new->ob_bytes;
     new->ob_exports = 0;
@@ -206,7 +206,7 @@
         }
         else {
             /* Minor downsize; quick exit */
-            Py_SIZE(self) = size;
+            Py_SET_SIZE(self, size);
             PyByteArray_AS_STRING(self)[size] = '\0'; /* Trailing null */
             return 0;
         }
@@ -246,7 +246,7 @@
     }
 
     obj->ob_bytes = obj->ob_start = sval;
-    Py_SIZE(self) = size;
+    Py_SET_SIZE(self, size);
     obj->ob_alloc = alloc;
     obj->ob_bytes[size] = '\0'; /* Trailing null byte */
 
@@ -498,7 +498,7 @@
             }
             /* memmove() removed bytes, the bytearray object cannot be
                restored in its previous state. */
-            Py_SIZE(self) += growth;
+            Py_SET_SIZE(self, Py_SIZE(self) + growth);
             res = -1;
         }
         buf = PyByteArray_AS_STRING(self);
@@ -886,7 +886,7 @@
 
         /* Append the byte */
         if (Py_SIZE(self) + 1 < self->ob_alloc) {
-            Py_SIZE(self)++;
+            Py_SET_SIZE(self, Py_SIZE(self) + 1);
             PyByteArray_AS_STRING(self)[Py_SIZE(self)] = '\0';
         }
         else if (PyByteArray_Resize((PyObject *)self, Py_SIZE(self)+1) < 0)
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
index 4edd93d..e139bed 100644
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -2963,7 +2963,7 @@
     }
     _Py_NewReference(*pv);
     sv = (PyBytesObject *) *pv;
-    Py_SIZE(sv) = newsize;
+    Py_SET_SIZE(sv, newsize);
     sv->ob_sval[newsize] = '\0';
     sv->ob_shash = -1;          /* invalidate cached hash value */
     return 0;
diff --git a/Objects/listobject.c b/Objects/listobject.c
index d83e3cf..a406e70 100644
--- a/Objects/listobject.c
+++ b/Objects/listobject.c
@@ -45,7 +45,7 @@
     */
     if (allocated >= newsize && newsize >= (allocated >> 1)) {
         assert(self->ob_item != NULL || newsize == 0);
-        Py_SIZE(self) = newsize;
+        Py_SET_SIZE(self, newsize);
         return 0;
     }
 
@@ -73,7 +73,7 @@
         return -1;
     }
     self->ob_item = items;
-    Py_SIZE(self) = newsize;
+    Py_SET_SIZE(self, newsize);
     self->allocated = new_allocated;
     return 0;
 }
@@ -156,7 +156,7 @@
             return PyErr_NoMemory();
         }
     }
-    Py_SIZE(op) = size;
+    Py_SET_SIZE(op, size);
     op->allocated = size;
     _PyObject_GC_TRACK(op);
     return (PyObject *) op;
@@ -457,7 +457,7 @@
         Py_INCREF(v);
         dest[i] = v;
     }
-    Py_SIZE(np) = len;
+    Py_SET_SIZE(np, len);
     return (PyObject *)np;
 }
 
@@ -518,7 +518,7 @@
         Py_INCREF(v);
         dest[i] = v;
     }
-    Py_SIZE(np) = size;
+    Py_SET_SIZE(np, size);
     return (PyObject *)np;
 #undef b
 }
@@ -561,7 +561,7 @@
             }
         }
     }
-    Py_SIZE(np) = size;
+    Py_SET_SIZE(np, size);
     return (PyObject *) np;
 }
 
@@ -574,7 +574,7 @@
         /* Because XDECREF can recursively invoke operations on
            this list, we make it empty first. */
         i = Py_SIZE(a);
-        Py_SIZE(a) = 0;
+        Py_SET_SIZE(a, 0);
         a->ob_item = NULL;
         a->allocated = 0;
         while (--i >= 0) {
@@ -913,7 +913,7 @@
         if (list_resize(self, mn) < 0)
             goto error;
         /* Make the list sane again. */
-        Py_SIZE(self) = m;
+        Py_SET_SIZE(self, m);
     }
 
     /* Run iterator to exhaustion. */
@@ -931,7 +931,7 @@
         if (Py_SIZE(self) < self->allocated) {
             /* steals ref */
             PyList_SET_ITEM(self, Py_SIZE(self), item);
-            ++Py_SIZE(self);
+            Py_SET_SIZE(self, Py_SIZE(self) + 1);
         }
         else {
             int status = app1(self, item);
@@ -2204,7 +2204,7 @@
     saved_ob_size = Py_SIZE(self);
     saved_ob_item = self->ob_item;
     saved_allocated = self->allocated;
-    Py_SIZE(self) = 0;
+    Py_SET_SIZE(self, 0);
     self->ob_item = NULL;
     self->allocated = -1; /* any operation will reset it to >= 0 */
 
@@ -2421,7 +2421,7 @@
 keyfunc_fail:
     final_ob_item = self->ob_item;
     i = Py_SIZE(self);
-    Py_SIZE(self) = saved_ob_size;
+    Py_SET_SIZE(self, saved_ob_size);
     self->ob_item = saved_ob_item;
     self->allocated = saved_allocated;
     if (final_ob_item != NULL) {
@@ -2811,7 +2811,7 @@
                 Py_INCREF(it);
                 dest[i] = it;
             }
-            Py_SIZE(result) = slicelength;
+            Py_SET_SIZE(result, slicelength);
             return result;
         }
     }
@@ -2904,7 +2904,7 @@
                      sizeof(PyObject *));
             }
 
-            Py_SIZE(self) -= slicelength;
+            Py_SET_SIZE(self, Py_SIZE(self) - slicelength);
             res = list_resize(self, Py_SIZE(self));
 
             for (i = 0; i < slicelength; i++) {
diff --git a/Objects/longobject.c b/Objects/longobject.c
index 67cbc7b..b4d0b05 100644
--- a/Objects/longobject.c
+++ b/Objects/longobject.c
@@ -73,7 +73,7 @@
 
     x = (PyLongObject *)*x_p;
     if (Py_REFCNT(x) == 1) {
-        Py_SIZE(x) = -Py_SIZE(x);
+        Py_SET_SIZE(x, -Py_SIZE(x));
         return;
     }
 
@@ -112,8 +112,9 @@
 
     while (i > 0 && v->ob_digit[i-1] == 0)
         --i;
-    if (i != j)
-        Py_SIZE(v) = (Py_SIZE(v) < 0) ? -(i) : i;
+    if (i != j) {
+        Py_SET_SIZE(v, (Py_SIZE(v) < 0) ? -(i) : i);
+    }
     return v;
 }
 
@@ -281,9 +282,10 @@
     }
     result = _PyLong_New(i);
     if (result != NULL) {
-        Py_SIZE(result) = Py_SIZE(src);
-        while (--i >= 0)
+        Py_SET_SIZE(result, Py_SIZE(src));
+        while (--i >= 0) {
             result->ob_digit[i] = src->ob_digit[i];
+        }
     }
     return (PyObject *)result;
 }
@@ -318,7 +320,7 @@
     if (!(abs_ival >> PyLong_SHIFT)) {
         v = _PyLong_New(1);
         if (v) {
-            Py_SIZE(v) = sign;
+            Py_SET_SIZE(v, sign);
             v->ob_digit[0] = Py_SAFE_DOWNCAST(
                 abs_ival, unsigned long, digit);
         }
@@ -330,7 +332,7 @@
     if (!(abs_ival >> 2*PyLong_SHIFT)) {
         v = _PyLong_New(2);
         if (v) {
-            Py_SIZE(v) = 2*sign;
+            Py_SET_SIZE(v, 2 * sign);
             v->ob_digit[0] = Py_SAFE_DOWNCAST(
                 abs_ival & PyLong_MASK, unsigned long, digit);
             v->ob_digit[1] = Py_SAFE_DOWNCAST(
@@ -349,7 +351,7 @@
     v = _PyLong_New(ndigits);
     if (v != NULL) {
         digit *p = v->ob_digit;
-        Py_SIZE(v) = ndigits*sign;
+        Py_SET_SIZE(v, ndigits * sign);
         t = abs_ival;
         while (t) {
             *p++ = Py_SAFE_DOWNCAST(
@@ -445,8 +447,9 @@
         frac = frac - (double)bits;
         frac = ldexp(frac, PyLong_SHIFT);
     }
-    if (neg)
-        Py_SIZE(v) = -(Py_SIZE(v));
+    if (neg) {
+        Py_SET_SIZE(v, -(Py_SIZE(v)));
+    }
     return (PyObject *)v;
 }
 
@@ -930,7 +933,7 @@
         }
     }
 
-    Py_SIZE(v) = is_signed ? -idigit : idigit;
+    Py_SET_SIZE(v, is_signed ? -idigit : idigit);
     return (PyObject *)long_normalize(v);
 }
 
@@ -1158,7 +1161,7 @@
     v = _PyLong_New(ndigits);
     if (v != NULL) {
         digit *p = v->ob_digit;
-        Py_SIZE(v) = negative ? -ndigits : ndigits;
+        Py_SET_SIZE(v, negative ? -ndigits : ndigits);
         t = abs_ival;
         while (t) {
             *p++ = (digit)(t & PyLong_MASK);
@@ -1201,7 +1204,7 @@
     v = _PyLong_New(ndigits);
     if (v != NULL) {
         digit *p = v->ob_digit;
-        Py_SIZE(v) = negative ? -ndigits : ndigits;
+        Py_SET_SIZE(v, negative ? -ndigits : ndigits);
         t = abs_ival;
         while (t) {
             *p++ = (digit)(t & PyLong_MASK);
@@ -2443,7 +2446,7 @@
         if (z == NULL) {
             return NULL;
         }
-        Py_SIZE(z) = 0;
+        Py_SET_SIZE(z, 0);
 
         /* `convwidth` consecutive input digits are treated as a single
          * digit in base `convmultmax`.
@@ -2493,7 +2496,7 @@
                 assert(c < PyLong_BASE);
                 if (Py_SIZE(z) < size_z) {
                     *pz = (digit)c;
-                    ++Py_SIZE(z);
+                    Py_SET_SIZE(z, Py_SIZE(z) + 1);
                 }
                 else {
                     PyLongObject *tmp;
@@ -2532,7 +2535,7 @@
         goto onError;
     }
     if (sign < 0) {
-        Py_SIZE(z) = -(Py_SIZE(z));
+        Py_SET_SIZE(z, -(Py_SIZE(z)));
     }
     while (*str && Py_ISSPACE(*str)) {
         str++;
@@ -3165,7 +3168,7 @@
     }
     assert(borrow == 0);
     if (sign < 0) {
-        Py_SIZE(z) = -Py_SIZE(z);
+        Py_SET_SIZE(z, -Py_SIZE(z));
     }
     return maybe_small_long(long_normalize(z));
 }
@@ -3189,7 +3192,7 @@
                    That also means z is not an element of
                    small_ints, so negating it in-place is safe. */
                 assert(Py_REFCNT(z) == 1);
-                Py_SIZE(z) = -(Py_SIZE(z));
+                Py_SET_SIZE(z, -(Py_SIZE(z)));
             }
         }
         else
@@ -3222,7 +3225,7 @@
             z = x_add(a, b);
             if (z != NULL) {
                 assert(Py_SIZE(z) == 0 || Py_REFCNT(z) == 1);
-                Py_SIZE(z) = -(Py_SIZE(z));
+                Py_SET_SIZE(z, -(Py_SIZE(z)));
             }
         }
     }
@@ -3615,7 +3618,7 @@
         /* Multiply the next slice of b by a. */
         memcpy(bslice->ob_digit, b->ob_digit + nbdone,
                nbtouse * sizeof(digit));
-        Py_SIZE(bslice) = nbtouse;
+        Py_SET_SIZE(bslice, nbtouse);
         product = k_mul(a, bslice);
         if (product == NULL)
             goto fail;
@@ -4431,7 +4434,7 @@
         return PyLong_FromLong(-MEDIUM_VALUE(v));
     z = (PyLongObject *)_PyLong_Copy(v);
     if (z != NULL)
-        Py_SIZE(z) = -(Py_SIZE(v));
+        Py_SET_SIZE(z, -(Py_SIZE(v)));
     return (PyObject *)z;
 }
 
@@ -4576,7 +4579,7 @@
         return NULL;
     if (Py_SIZE(a) < 0) {
         assert(Py_REFCNT(z) == 1);
-        Py_SIZE(z) = -Py_SIZE(z);
+        Py_SET_SIZE(z, -Py_SIZE(z));
     }
     for (i = 0; i < wordshift; i++)
         z->ob_digit[i] = 0;
@@ -4760,7 +4763,7 @@
 
     /* Complement result if negative. */
     if (negz) {
-        Py_SIZE(z) = -(Py_SIZE(z));
+        Py_SET_SIZE(z, -(Py_SIZE(z)));
         z->ob_digit[size_z] = PyLong_MASK;
         v_complement(z->ob_digit, z->ob_digit, size_z+1);
     }
@@ -4907,8 +4910,9 @@
             T = -A; A = -B; B = T;
             T = -C; C = -D; D = T;
         }
-        if (c != NULL)
-            Py_SIZE(c) = size_a;
+        if (c != NULL) {
+            Py_SET_SIZE(c, size_a);
+        }
         else if (Py_REFCNT(a) == 1) {
             Py_INCREF(a);
             c = a;
@@ -4920,12 +4924,13 @@
                 goto error;
         }
 
-        if (d != NULL)
-            Py_SIZE(d) = size_a;
+        if (d != NULL) {
+            Py_SET_SIZE(d, size_a);
+        }
         else if (Py_REFCNT(b) == 1 && size_a <= alloc_b) {
             Py_INCREF(b);
             d = b;
-            Py_SIZE(d) = size_a;
+            Py_SET_SIZE(d, size_a);
         }
         else {
             alloc_b = size_a;
@@ -5105,9 +5110,10 @@
         return NULL;
     }
     assert(PyLong_Check(newobj));
-    Py_SIZE(newobj) = Py_SIZE(tmp);
-    for (i = 0; i < n; i++)
+    Py_SET_SIZE(newobj, Py_SIZE(tmp));
+    for (i = 0; i < n; i++) {
         newobj->ob_digit[i] = tmp->ob_digit[i];
+    }
     Py_DECREF(tmp);
     return (PyObject *)newobj;
 }
@@ -5744,7 +5750,7 @@
             return -1;
         }
 
-        Py_SIZE(v) = size;
+        Py_SET_SIZE(v, size);
         v->ob_digit[0] = (digit)abs(ival);
 
         tstate->interp->small_ints[i] = v;
diff --git a/Objects/odictobject.c b/Objects/odictobject.c
index 45e089b..f412220 100644
--- a/Objects/odictobject.c
+++ b/Objects/odictobject.c
@@ -1417,8 +1417,9 @@
             }
             count++;
         }
-        if (count < PyList_GET_SIZE(pieces))
-            Py_SIZE(pieces) = count;
+        if (count < PyList_GET_SIZE(pieces)) {
+            Py_SET_SIZE(pieces, count);
+        }
     }
     else {
         PyObject *items = _PyObject_CallMethodIdNoArgs((PyObject *)self,
diff --git a/Objects/stringlib/split.h b/Objects/stringlib/split.h
index 31f77a7..068047f 100644
--- a/Objects/stringlib/split.h
+++ b/Objects/stringlib/split.h
@@ -48,7 +48,7 @@
 
 
 /* Always force the list to the expected size. */
-#define FIX_PREALLOC_SIZE(list) Py_SIZE(list) = count
+#define FIX_PREALLOC_SIZE(list) Py_SET_SIZE(list, count)
 
 Py_LOCAL_INLINE(PyObject *)
 STRINGLIB(split_whitespace)(PyObject* str_obj,
diff --git a/Objects/structseq.c b/Objects/structseq.c
index c86fbe5..1865e24 100644
--- a/Objects/structseq.c
+++ b/Objects/structseq.c
@@ -47,7 +47,7 @@
         return NULL;
     /* Hack the size of the variable object, so invisible fields don't appear
      to Python code. */
-    Py_SIZE(obj) = VISIBLE_SIZE_TP(type);
+    Py_SET_SIZE(obj, VISIBLE_SIZE_TP(type));
     for (i = 0; i < size; i++)
         obj->ob_item[i] = NULL;