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

Replace direct acccess to PyVarObject.ob_size with usage of
the Py_SET_SIZE() function.
This commit is contained in:
Victor Stinner 2020-02-07 23:18:08 +01:00 committed by GitHub
parent de6f38db48
commit 60ac6ed557
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 95 additions and 86 deletions

View File

@ -1007,7 +1007,7 @@ _asyncio_Future_remove_done_callback(FutureObj *self, PyObject *fn)
} }
if (j < len) { if (j < len) {
Py_SIZE(newlist) = j; Py_SET_SIZE(newlist, j);
} }
j = PyList_GET_SIZE(newlist); j = PyList_GET_SIZE(newlist);
len = PyList_GET_SIZE(self->fut_callbacks); len = PyList_GET_SIZE(self->fut_callbacks);

View File

@ -172,7 +172,7 @@ deque_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
MARK_END(b->rightlink); MARK_END(b->rightlink);
assert(BLOCKLEN >= 2); assert(BLOCKLEN >= 2);
Py_SIZE(deque) = 0; Py_SET_SIZE(deque, 0);
deque->leftblock = b; deque->leftblock = b;
deque->rightblock = b; deque->rightblock = b;
deque->leftindex = CENTER + 1; deque->leftindex = CENTER + 1;
@ -196,7 +196,7 @@ deque_pop(dequeobject *deque, PyObject *unused)
} }
item = deque->rightblock->data[deque->rightindex]; item = deque->rightblock->data[deque->rightindex];
deque->rightindex--; deque->rightindex--;
Py_SIZE(deque)--; Py_SET_SIZE(deque, Py_SIZE(deque) - 1);
deque->state++; deque->state++;
if (deque->rightindex < 0) { if (deque->rightindex < 0) {
@ -234,7 +234,7 @@ deque_popleft(dequeobject *deque, PyObject *unused)
assert(deque->leftblock != NULL); assert(deque->leftblock != NULL);
item = deque->leftblock->data[deque->leftindex]; item = deque->leftblock->data[deque->leftindex];
deque->leftindex++; deque->leftindex++;
Py_SIZE(deque)--; Py_SET_SIZE(deque, Py_SIZE(deque) - 1);
deque->state++; deque->state++;
if (deque->leftindex == BLOCKLEN) { if (deque->leftindex == BLOCKLEN) {
@ -287,7 +287,7 @@ deque_append_internal(dequeobject *deque, PyObject *item, Py_ssize_t maxlen)
MARK_END(b->rightlink); MARK_END(b->rightlink);
deque->rightindex = -1; deque->rightindex = -1;
} }
Py_SIZE(deque)++; Py_SET_SIZE(deque, Py_SIZE(deque) + 1);
deque->rightindex++; deque->rightindex++;
deque->rightblock->data[deque->rightindex] = item; deque->rightblock->data[deque->rightindex] = item;
if (NEEDS_TRIM(deque, maxlen)) { if (NEEDS_TRIM(deque, maxlen)) {
@ -324,7 +324,7 @@ deque_appendleft_internal(dequeobject *deque, PyObject *item, Py_ssize_t maxlen)
MARK_END(b->leftlink); MARK_END(b->leftlink);
deque->leftindex = BLOCKLEN; deque->leftindex = BLOCKLEN;
} }
Py_SIZE(deque)++; Py_SET_SIZE(deque, Py_SIZE(deque) + 1);
deque->leftindex--; deque->leftindex--;
deque->leftblock->data[deque->leftindex] = item; deque->leftblock->data[deque->leftindex] = item;
if (NEEDS_TRIM(deque, deque->maxlen)) { if (NEEDS_TRIM(deque, deque->maxlen)) {
@ -597,7 +597,7 @@ deque_clear(dequeobject *deque)
/* Set the deque to be empty using the newly allocated block */ /* Set the deque to be empty using the newly allocated block */
MARK_END(b->leftlink); MARK_END(b->leftlink);
MARK_END(b->rightlink); MARK_END(b->rightlink);
Py_SIZE(deque) = 0; Py_SET_SIZE(deque, 0);
deque->leftblock = b; deque->leftblock = b;
deque->rightblock = b; deque->rightblock = b;
deque->leftindex = CENTER + 1; deque->leftindex = CENTER + 1;
@ -680,7 +680,7 @@ deque_inplace_repeat(dequeobject *deque, Py_ssize_t n)
if (deque->rightindex == BLOCKLEN - 1) { if (deque->rightindex == BLOCKLEN - 1) {
block *b = newblock(); block *b = newblock();
if (b == NULL) { if (b == NULL) {
Py_SIZE(deque) += i; Py_SET_SIZE(deque, Py_SIZE(deque) + i);
return NULL; return NULL;
} }
b->leftlink = deque->rightblock; b->leftlink = deque->rightblock;
@ -700,7 +700,7 @@ deque_inplace_repeat(dequeobject *deque, Py_ssize_t n)
deque->rightblock->data[deque->rightindex] = item; deque->rightblock->data[deque->rightindex] = item;
} }
} }
Py_SIZE(deque) += i; Py_SET_SIZE(deque, Py_SIZE(deque) + i);
Py_INCREF(deque); Py_INCREF(deque);
return (PyObject *)deque; return (PyObject *)deque;
} }

View File

@ -3253,9 +3253,9 @@ dec_as_long(PyObject *dec, PyObject *context, int round)
i--; i--;
} }
Py_SIZE(pylong) = i; Py_SET_SIZE(pylong, i);
if (mpd_isnegative(x) && !mpd_iszero(x)) { if (mpd_isnegative(x) && !mpd_iszero(x)) {
Py_SIZE(pylong) = -i; Py_SET_SIZE(pylong, -i);
} }
mpd_del(x); mpd_del(x);

View File

@ -461,7 +461,7 @@ Pdata_New(void)
if (!(self = PyObject_New(Pdata, &Pdata_Type))) if (!(self = PyObject_New(Pdata, &Pdata_Type)))
return NULL; return NULL;
Py_SIZE(self) = 0; Py_SET_SIZE(self, 0);
self->mark_set = 0; self->mark_set = 0;
self->fence = 0; self->fence = 0;
self->allocated = 8; self->allocated = 8;
@ -488,7 +488,7 @@ Pdata_clear(Pdata *self, Py_ssize_t clearto)
while (--i >= clearto) { while (--i >= clearto) {
Py_CLEAR(self->data[i]); Py_CLEAR(self->data[i]);
} }
Py_SIZE(self) = clearto; Py_SET_SIZE(self, clearto);
return 0; return 0;
} }
@ -539,7 +539,8 @@ Pdata_pop(Pdata *self)
Pdata_stack_underflow(self); Pdata_stack_underflow(self);
return NULL; return NULL;
} }
return self->data[--Py_SIZE(self)]; Py_SET_SIZE(self, Py_SIZE(self) - 1);
return self->data[Py_SIZE(self)];
} }
#define PDATA_POP(D, V) do { (V) = Pdata_pop((D)); } while (0) #define PDATA_POP(D, V) do { (V) = Pdata_pop((D)); } while (0)
@ -549,7 +550,8 @@ Pdata_push(Pdata *self, PyObject *obj)
if (Py_SIZE(self) == self->allocated && Pdata_grow(self) < 0) { if (Py_SIZE(self) == self->allocated && Pdata_grow(self) < 0) {
return -1; return -1;
} }
self->data[Py_SIZE(self)++] = obj; self->data[Py_SIZE(self)] = obj;
Py_SET_SIZE(self, Py_SIZE(self) + 1);
return 0; return 0;
} }
@ -579,7 +581,7 @@ Pdata_poptuple(Pdata *self, Py_ssize_t start)
for (i = start, j = 0; j < len; i++, j++) for (i = start, j = 0; j < len; i++, j++)
PyTuple_SET_ITEM(tuple, j, self->data[i]); PyTuple_SET_ITEM(tuple, j, self->data[i]);
Py_SIZE(self) = start; Py_SET_SIZE(self, start);
return tuple; return tuple;
} }
@ -596,7 +598,7 @@ Pdata_poplist(Pdata *self, Py_ssize_t start)
for (i = start, j = 0; j < len; i++, j++) for (i = start, j = 0; j < len; i++, j++)
PyList_SET_ITEM(list, j, self->data[i]); PyList_SET_ITEM(list, j, self->data[i]);
Py_SIZE(self) = start; Py_SET_SIZE(self, start);
return list; return list;
} }
@ -6134,7 +6136,7 @@ load_pop(UnpicklerObject *self)
else { else {
len--; len--;
Py_DECREF(self->stack->data[len]); Py_DECREF(self->stack->data[len]);
Py_SIZE(self->stack) = len; Py_SET_SIZE(self->stack, len);
} }
return 0; return 0;
} }
@ -6495,13 +6497,13 @@ do_append(UnpicklerObject *self, Py_ssize_t x)
result = _Pickle_FastCall(append_func, value); result = _Pickle_FastCall(append_func, value);
if (result == NULL) { if (result == NULL) {
Pdata_clear(self->stack, i + 1); Pdata_clear(self->stack, i + 1);
Py_SIZE(self->stack) = x; Py_SET_SIZE(self->stack, x);
Py_DECREF(append_func); Py_DECREF(append_func);
return -1; return -1;
} }
Py_DECREF(result); Py_DECREF(result);
} }
Py_SIZE(self->stack) = x; Py_SET_SIZE(self->stack, x);
Py_DECREF(append_func); Py_DECREF(append_func);
} }
} }
@ -6623,12 +6625,12 @@ load_additems(UnpicklerObject *self)
result = _Pickle_FastCall(add_func, item); result = _Pickle_FastCall(add_func, item);
if (result == NULL) { if (result == NULL) {
Pdata_clear(self->stack, i + 1); Pdata_clear(self->stack, i + 1);
Py_SIZE(self->stack) = mark; Py_SET_SIZE(self->stack, mark);
return -1; return -1;
} }
Py_DECREF(result); Py_DECREF(result);
} }
Py_SIZE(self->stack) = mark; Py_SET_SIZE(self->stack, mark);
} }
return 0; return 0;

View File

@ -128,14 +128,14 @@ array_resize(arrayobject *self, Py_ssize_t newsize)
if (self->allocated >= newsize && if (self->allocated >= newsize &&
Py_SIZE(self) < newsize + 16 && Py_SIZE(self) < newsize + 16 &&
self->ob_item != NULL) { self->ob_item != NULL) {
Py_SIZE(self) = newsize; Py_SET_SIZE(self, newsize);
return 0; return 0;
} }
if (newsize == 0) { if (newsize == 0) {
PyMem_FREE(self->ob_item); PyMem_FREE(self->ob_item);
self->ob_item = NULL; self->ob_item = NULL;
Py_SIZE(self) = 0; Py_SET_SIZE(self, 0);
self->allocated = 0; self->allocated = 0;
return 0; return 0;
} }
@ -165,7 +165,7 @@ array_resize(arrayobject *self, Py_ssize_t newsize)
return -1; return -1;
} }
self->ob_item = items; self->ob_item = items;
Py_SIZE(self) = newsize; Py_SET_SIZE(self, newsize);
self->allocated = _new_size; self->allocated = _new_size;
return 0; return 0;
} }
@ -593,7 +593,7 @@ newarrayobject(PyTypeObject *type, Py_ssize_t size, const struct arraydescr *des
op->ob_descr = descr; op->ob_descr = descr;
op->allocated = size; op->allocated = size;
op->weakreflist = NULL; op->weakreflist = NULL;
Py_SIZE(op) = size; Py_SET_SIZE(op, size);
if (size <= 0) { if (size <= 0) {
op->ob_item = NULL; op->ob_item = NULL;
} }
@ -2696,7 +2696,7 @@ array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return NULL; return NULL;
} }
self->ob_item = item; self->ob_item = item;
Py_SIZE(self) = n / sizeof(Py_UNICODE); Py_SET_SIZE(self, n / sizeof(Py_UNICODE));
memcpy(item, ustr, n); memcpy(item, ustr, n);
self->allocated = Py_SIZE(self); self->allocated = Py_SIZE(self);
} }

View File

@ -2294,7 +2294,7 @@ _PyObject_GC_Resize(PyVarObject *op, Py_ssize_t nitems)
if (g == NULL) if (g == NULL)
return (PyVarObject *)PyErr_NoMemory(); return (PyVarObject *)PyErr_NoMemory();
op = (PyVarObject *) FROM_GC(g); op = (PyVarObject *) FROM_GC(g);
Py_SIZE(op) = nitems; Py_SET_SIZE(op, nitems);
return op; return op;
} }

View File

@ -148,7 +148,7 @@ PyByteArray_FromStringAndSize(const char *bytes, Py_ssize_t size)
memcpy(new->ob_bytes, bytes, size); memcpy(new->ob_bytes, bytes, size);
new->ob_bytes[size] = '\0'; /* Trailing null byte */ new->ob_bytes[size] = '\0'; /* Trailing null byte */
} }
Py_SIZE(new) = size; Py_SET_SIZE(new, size);
new->ob_alloc = alloc; new->ob_alloc = alloc;
new->ob_start = new->ob_bytes; new->ob_start = new->ob_bytes;
new->ob_exports = 0; new->ob_exports = 0;
@ -206,7 +206,7 @@ PyByteArray_Resize(PyObject *self, Py_ssize_t requested_size)
} }
else { else {
/* Minor downsize; quick exit */ /* Minor downsize; quick exit */
Py_SIZE(self) = size; Py_SET_SIZE(self, size);
PyByteArray_AS_STRING(self)[size] = '\0'; /* Trailing null */ PyByteArray_AS_STRING(self)[size] = '\0'; /* Trailing null */
return 0; return 0;
} }
@ -246,7 +246,7 @@ PyByteArray_Resize(PyObject *self, Py_ssize_t requested_size)
} }
obj->ob_bytes = obj->ob_start = sval; obj->ob_bytes = obj->ob_start = sval;
Py_SIZE(self) = size; Py_SET_SIZE(self, size);
obj->ob_alloc = alloc; obj->ob_alloc = alloc;
obj->ob_bytes[size] = '\0'; /* Trailing null byte */ obj->ob_bytes[size] = '\0'; /* Trailing null byte */
@ -498,7 +498,7 @@ bytearray_setslice_linear(PyByteArrayObject *self,
} }
/* memmove() removed bytes, the bytearray object cannot be /* memmove() removed bytes, the bytearray object cannot be
restored in its previous state. */ restored in its previous state. */
Py_SIZE(self) += growth; Py_SET_SIZE(self, Py_SIZE(self) + growth);
res = -1; res = -1;
} }
buf = PyByteArray_AS_STRING(self); buf = PyByteArray_AS_STRING(self);
@ -886,7 +886,7 @@ bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds)
/* Append the byte */ /* Append the byte */
if (Py_SIZE(self) + 1 < self->ob_alloc) { 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'; PyByteArray_AS_STRING(self)[Py_SIZE(self)] = '\0';
} }
else if (PyByteArray_Resize((PyObject *)self, Py_SIZE(self)+1) < 0) else if (PyByteArray_Resize((PyObject *)self, Py_SIZE(self)+1) < 0)

View File

@ -2963,7 +2963,7 @@ _PyBytes_Resize(PyObject **pv, Py_ssize_t newsize)
} }
_Py_NewReference(*pv); _Py_NewReference(*pv);
sv = (PyBytesObject *) *pv; sv = (PyBytesObject *) *pv;
Py_SIZE(sv) = newsize; Py_SET_SIZE(sv, newsize);
sv->ob_sval[newsize] = '\0'; sv->ob_sval[newsize] = '\0';
sv->ob_shash = -1; /* invalidate cached hash value */ sv->ob_shash = -1; /* invalidate cached hash value */
return 0; return 0;

View File

@ -45,7 +45,7 @@ list_resize(PyListObject *self, Py_ssize_t newsize)
*/ */
if (allocated >= newsize && newsize >= (allocated >> 1)) { if (allocated >= newsize && newsize >= (allocated >> 1)) {
assert(self->ob_item != NULL || newsize == 0); assert(self->ob_item != NULL || newsize == 0);
Py_SIZE(self) = newsize; Py_SET_SIZE(self, newsize);
return 0; return 0;
} }
@ -73,7 +73,7 @@ list_resize(PyListObject *self, Py_ssize_t newsize)
return -1; return -1;
} }
self->ob_item = items; self->ob_item = items;
Py_SIZE(self) = newsize; Py_SET_SIZE(self, newsize);
self->allocated = new_allocated; self->allocated = new_allocated;
return 0; return 0;
} }
@ -156,7 +156,7 @@ PyList_New(Py_ssize_t size)
return PyErr_NoMemory(); return PyErr_NoMemory();
} }
} }
Py_SIZE(op) = size; Py_SET_SIZE(op, size);
op->allocated = size; op->allocated = size;
_PyObject_GC_TRACK(op); _PyObject_GC_TRACK(op);
return (PyObject *) op; return (PyObject *) op;
@ -457,7 +457,7 @@ list_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
Py_INCREF(v); Py_INCREF(v);
dest[i] = v; dest[i] = v;
} }
Py_SIZE(np) = len; Py_SET_SIZE(np, len);
return (PyObject *)np; return (PyObject *)np;
} }
@ -518,7 +518,7 @@ list_concat(PyListObject *a, PyObject *bb)
Py_INCREF(v); Py_INCREF(v);
dest[i] = v; dest[i] = v;
} }
Py_SIZE(np) = size; Py_SET_SIZE(np, size);
return (PyObject *)np; return (PyObject *)np;
#undef b #undef b
} }
@ -561,7 +561,7 @@ list_repeat(PyListObject *a, Py_ssize_t n)
} }
} }
} }
Py_SIZE(np) = size; Py_SET_SIZE(np, size);
return (PyObject *) np; return (PyObject *) np;
} }
@ -574,7 +574,7 @@ _list_clear(PyListObject *a)
/* Because XDECREF can recursively invoke operations on /* Because XDECREF can recursively invoke operations on
this list, we make it empty first. */ this list, we make it empty first. */
i = Py_SIZE(a); i = Py_SIZE(a);
Py_SIZE(a) = 0; Py_SET_SIZE(a, 0);
a->ob_item = NULL; a->ob_item = NULL;
a->allocated = 0; a->allocated = 0;
while (--i >= 0) { while (--i >= 0) {
@ -913,7 +913,7 @@ list_extend(PyListObject *self, PyObject *iterable)
if (list_resize(self, mn) < 0) if (list_resize(self, mn) < 0)
goto error; goto error;
/* Make the list sane again. */ /* Make the list sane again. */
Py_SIZE(self) = m; Py_SET_SIZE(self, m);
} }
/* Run iterator to exhaustion. */ /* Run iterator to exhaustion. */
@ -931,7 +931,7 @@ list_extend(PyListObject *self, PyObject *iterable)
if (Py_SIZE(self) < self->allocated) { if (Py_SIZE(self) < self->allocated) {
/* steals ref */ /* steals ref */
PyList_SET_ITEM(self, Py_SIZE(self), item); PyList_SET_ITEM(self, Py_SIZE(self), item);
++Py_SIZE(self); Py_SET_SIZE(self, Py_SIZE(self) + 1);
} }
else { else {
int status = app1(self, item); int status = app1(self, item);
@ -2204,7 +2204,7 @@ list_sort_impl(PyListObject *self, PyObject *keyfunc, int reverse)
saved_ob_size = Py_SIZE(self); saved_ob_size = Py_SIZE(self);
saved_ob_item = self->ob_item; saved_ob_item = self->ob_item;
saved_allocated = self->allocated; saved_allocated = self->allocated;
Py_SIZE(self) = 0; Py_SET_SIZE(self, 0);
self->ob_item = NULL; self->ob_item = NULL;
self->allocated = -1; /* any operation will reset it to >= 0 */ self->allocated = -1; /* any operation will reset it to >= 0 */
@ -2421,7 +2421,7 @@ fail:
keyfunc_fail: keyfunc_fail:
final_ob_item = self->ob_item; final_ob_item = self->ob_item;
i = Py_SIZE(self); i = Py_SIZE(self);
Py_SIZE(self) = saved_ob_size; Py_SET_SIZE(self, saved_ob_size);
self->ob_item = saved_ob_item; self->ob_item = saved_ob_item;
self->allocated = saved_allocated; self->allocated = saved_allocated;
if (final_ob_item != NULL) { if (final_ob_item != NULL) {
@ -2811,7 +2811,7 @@ list_subscript(PyListObject* self, PyObject* item)
Py_INCREF(it); Py_INCREF(it);
dest[i] = it; dest[i] = it;
} }
Py_SIZE(result) = slicelength; Py_SET_SIZE(result, slicelength);
return result; return result;
} }
} }
@ -2904,7 +2904,7 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
sizeof(PyObject *)); sizeof(PyObject *));
} }
Py_SIZE(self) -= slicelength; Py_SET_SIZE(self, Py_SIZE(self) - slicelength);
res = list_resize(self, Py_SIZE(self)); res = list_resize(self, Py_SIZE(self));
for (i = 0; i < slicelength; i++) { for (i = 0; i < slicelength; i++) {

View File

@ -73,7 +73,7 @@ _PyLong_Negate(PyLongObject **x_p)
x = (PyLongObject *)*x_p; x = (PyLongObject *)*x_p;
if (Py_REFCNT(x) == 1) { if (Py_REFCNT(x) == 1) {
Py_SIZE(x) = -Py_SIZE(x); Py_SET_SIZE(x, -Py_SIZE(x));
return; return;
} }
@ -112,8 +112,9 @@ long_normalize(PyLongObject *v)
while (i > 0 && v->ob_digit[i-1] == 0) while (i > 0 && v->ob_digit[i-1] == 0)
--i; --i;
if (i != j) if (i != j) {
Py_SIZE(v) = (Py_SIZE(v) < 0) ? -(i) : i; Py_SET_SIZE(v, (Py_SIZE(v) < 0) ? -(i) : i);
}
return v; return v;
} }
@ -281,10 +282,11 @@ _PyLong_Copy(PyLongObject *src)
} }
result = _PyLong_New(i); result = _PyLong_New(i);
if (result != NULL) { if (result != NULL) {
Py_SIZE(result) = Py_SIZE(src); Py_SET_SIZE(result, Py_SIZE(src));
while (--i >= 0) while (--i >= 0) {
result->ob_digit[i] = src->ob_digit[i]; result->ob_digit[i] = src->ob_digit[i];
} }
}
return (PyObject *)result; return (PyObject *)result;
} }
@ -318,7 +320,7 @@ PyLong_FromLong(long ival)
if (!(abs_ival >> PyLong_SHIFT)) { if (!(abs_ival >> PyLong_SHIFT)) {
v = _PyLong_New(1); v = _PyLong_New(1);
if (v) { if (v) {
Py_SIZE(v) = sign; Py_SET_SIZE(v, sign);
v->ob_digit[0] = Py_SAFE_DOWNCAST( v->ob_digit[0] = Py_SAFE_DOWNCAST(
abs_ival, unsigned long, digit); abs_ival, unsigned long, digit);
} }
@ -330,7 +332,7 @@ PyLong_FromLong(long ival)
if (!(abs_ival >> 2*PyLong_SHIFT)) { if (!(abs_ival >> 2*PyLong_SHIFT)) {
v = _PyLong_New(2); v = _PyLong_New(2);
if (v) { if (v) {
Py_SIZE(v) = 2*sign; Py_SET_SIZE(v, 2 * sign);
v->ob_digit[0] = Py_SAFE_DOWNCAST( v->ob_digit[0] = Py_SAFE_DOWNCAST(
abs_ival & PyLong_MASK, unsigned long, digit); abs_ival & PyLong_MASK, unsigned long, digit);
v->ob_digit[1] = Py_SAFE_DOWNCAST( v->ob_digit[1] = Py_SAFE_DOWNCAST(
@ -349,7 +351,7 @@ PyLong_FromLong(long ival)
v = _PyLong_New(ndigits); v = _PyLong_New(ndigits);
if (v != NULL) { if (v != NULL) {
digit *p = v->ob_digit; digit *p = v->ob_digit;
Py_SIZE(v) = ndigits*sign; Py_SET_SIZE(v, ndigits * sign);
t = abs_ival; t = abs_ival;
while (t) { while (t) {
*p++ = Py_SAFE_DOWNCAST( *p++ = Py_SAFE_DOWNCAST(
@ -445,8 +447,9 @@ PyLong_FromDouble(double dval)
frac = frac - (double)bits; frac = frac - (double)bits;
frac = ldexp(frac, PyLong_SHIFT); frac = ldexp(frac, PyLong_SHIFT);
} }
if (neg) if (neg) {
Py_SIZE(v) = -(Py_SIZE(v)); Py_SET_SIZE(v, -(Py_SIZE(v)));
}
return (PyObject *)v; return (PyObject *)v;
} }
@ -930,7 +933,7 @@ _PyLong_FromByteArray(const unsigned char* bytes, size_t n,
} }
} }
Py_SIZE(v) = is_signed ? -idigit : idigit; Py_SET_SIZE(v, is_signed ? -idigit : idigit);
return (PyObject *)long_normalize(v); return (PyObject *)long_normalize(v);
} }
@ -1158,7 +1161,7 @@ PyLong_FromLongLong(long long ival)
v = _PyLong_New(ndigits); v = _PyLong_New(ndigits);
if (v != NULL) { if (v != NULL) {
digit *p = v->ob_digit; digit *p = v->ob_digit;
Py_SIZE(v) = negative ? -ndigits : ndigits; Py_SET_SIZE(v, negative ? -ndigits : ndigits);
t = abs_ival; t = abs_ival;
while (t) { while (t) {
*p++ = (digit)(t & PyLong_MASK); *p++ = (digit)(t & PyLong_MASK);
@ -1201,7 +1204,7 @@ PyLong_FromSsize_t(Py_ssize_t ival)
v = _PyLong_New(ndigits); v = _PyLong_New(ndigits);
if (v != NULL) { if (v != NULL) {
digit *p = v->ob_digit; digit *p = v->ob_digit;
Py_SIZE(v) = negative ? -ndigits : ndigits; Py_SET_SIZE(v, negative ? -ndigits : ndigits);
t = abs_ival; t = abs_ival;
while (t) { while (t) {
*p++ = (digit)(t & PyLong_MASK); *p++ = (digit)(t & PyLong_MASK);
@ -2443,7 +2446,7 @@ digit beyond the first.
if (z == NULL) { if (z == NULL) {
return NULL; return NULL;
} }
Py_SIZE(z) = 0; Py_SET_SIZE(z, 0);
/* `convwidth` consecutive input digits are treated as a single /* `convwidth` consecutive input digits are treated as a single
* digit in base `convmultmax`. * digit in base `convmultmax`.
@ -2493,7 +2496,7 @@ digit beyond the first.
assert(c < PyLong_BASE); assert(c < PyLong_BASE);
if (Py_SIZE(z) < size_z) { if (Py_SIZE(z) < size_z) {
*pz = (digit)c; *pz = (digit)c;
++Py_SIZE(z); Py_SET_SIZE(z, Py_SIZE(z) + 1);
} }
else { else {
PyLongObject *tmp; PyLongObject *tmp;
@ -2532,7 +2535,7 @@ digit beyond the first.
goto onError; goto onError;
} }
if (sign < 0) { if (sign < 0) {
Py_SIZE(z) = -(Py_SIZE(z)); Py_SET_SIZE(z, -(Py_SIZE(z)));
} }
while (*str && Py_ISSPACE(*str)) { while (*str && Py_ISSPACE(*str)) {
str++; str++;
@ -3165,7 +3168,7 @@ x_sub(PyLongObject *a, PyLongObject *b)
} }
assert(borrow == 0); assert(borrow == 0);
if (sign < 0) { if (sign < 0) {
Py_SIZE(z) = -Py_SIZE(z); Py_SET_SIZE(z, -Py_SIZE(z));
} }
return maybe_small_long(long_normalize(z)); return maybe_small_long(long_normalize(z));
} }
@ -3189,7 +3192,7 @@ long_add(PyLongObject *a, PyLongObject *b)
That also means z is not an element of That also means z is not an element of
small_ints, so negating it in-place is safe. */ small_ints, so negating it in-place is safe. */
assert(Py_REFCNT(z) == 1); assert(Py_REFCNT(z) == 1);
Py_SIZE(z) = -(Py_SIZE(z)); Py_SET_SIZE(z, -(Py_SIZE(z)));
} }
} }
else else
@ -3222,7 +3225,7 @@ long_sub(PyLongObject *a, PyLongObject *b)
z = x_add(a, b); z = x_add(a, b);
if (z != NULL) { if (z != NULL) {
assert(Py_SIZE(z) == 0 || Py_REFCNT(z) == 1); 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 @@ k_lopsided_mul(PyLongObject *a, PyLongObject *b)
/* Multiply the next slice of b by a. */ /* Multiply the next slice of b by a. */
memcpy(bslice->ob_digit, b->ob_digit + nbdone, memcpy(bslice->ob_digit, b->ob_digit + nbdone,
nbtouse * sizeof(digit)); nbtouse * sizeof(digit));
Py_SIZE(bslice) = nbtouse; Py_SET_SIZE(bslice, nbtouse);
product = k_mul(a, bslice); product = k_mul(a, bslice);
if (product == NULL) if (product == NULL)
goto fail; goto fail;
@ -4431,7 +4434,7 @@ long_neg(PyLongObject *v)
return PyLong_FromLong(-MEDIUM_VALUE(v)); return PyLong_FromLong(-MEDIUM_VALUE(v));
z = (PyLongObject *)_PyLong_Copy(v); z = (PyLongObject *)_PyLong_Copy(v);
if (z != NULL) if (z != NULL)
Py_SIZE(z) = -(Py_SIZE(v)); Py_SET_SIZE(z, -(Py_SIZE(v)));
return (PyObject *)z; return (PyObject *)z;
} }
@ -4576,7 +4579,7 @@ long_lshift1(PyLongObject *a, Py_ssize_t wordshift, digit remshift)
return NULL; return NULL;
if (Py_SIZE(a) < 0) { if (Py_SIZE(a) < 0) {
assert(Py_REFCNT(z) == 1); assert(Py_REFCNT(z) == 1);
Py_SIZE(z) = -Py_SIZE(z); Py_SET_SIZE(z, -Py_SIZE(z));
} }
for (i = 0; i < wordshift; i++) for (i = 0; i < wordshift; i++)
z->ob_digit[i] = 0; z->ob_digit[i] = 0;
@ -4760,7 +4763,7 @@ long_bitwise(PyLongObject *a,
/* Complement result if negative. */ /* Complement result if negative. */
if (negz) { if (negz) {
Py_SIZE(z) = -(Py_SIZE(z)); Py_SET_SIZE(z, -(Py_SIZE(z)));
z->ob_digit[size_z] = PyLong_MASK; z->ob_digit[size_z] = PyLong_MASK;
v_complement(z->ob_digit, z->ob_digit, size_z+1); v_complement(z->ob_digit, z->ob_digit, size_z+1);
} }
@ -4907,8 +4910,9 @@ _PyLong_GCD(PyObject *aarg, PyObject *barg)
T = -A; A = -B; B = T; T = -A; A = -B; B = T;
T = -C; C = -D; D = T; T = -C; C = -D; D = T;
} }
if (c != NULL) if (c != NULL) {
Py_SIZE(c) = size_a; Py_SET_SIZE(c, size_a);
}
else if (Py_REFCNT(a) == 1) { else if (Py_REFCNT(a) == 1) {
Py_INCREF(a); Py_INCREF(a);
c = a; c = a;
@ -4920,12 +4924,13 @@ _PyLong_GCD(PyObject *aarg, PyObject *barg)
goto error; goto error;
} }
if (d != NULL) if (d != NULL) {
Py_SIZE(d) = size_a; Py_SET_SIZE(d, size_a);
}
else if (Py_REFCNT(b) == 1 && size_a <= alloc_b) { else if (Py_REFCNT(b) == 1 && size_a <= alloc_b) {
Py_INCREF(b); Py_INCREF(b);
d = b; d = b;
Py_SIZE(d) = size_a; Py_SET_SIZE(d, size_a);
} }
else { else {
alloc_b = size_a; alloc_b = size_a;
@ -5105,9 +5110,10 @@ long_subtype_new(PyTypeObject *type, PyObject *x, PyObject *obase)
return NULL; return NULL;
} }
assert(PyLong_Check(newobj)); assert(PyLong_Check(newobj));
Py_SIZE(newobj) = Py_SIZE(tmp); Py_SET_SIZE(newobj, Py_SIZE(tmp));
for (i = 0; i < n; i++) for (i = 0; i < n; i++) {
newobj->ob_digit[i] = tmp->ob_digit[i]; newobj->ob_digit[i] = tmp->ob_digit[i];
}
Py_DECREF(tmp); Py_DECREF(tmp);
return (PyObject *)newobj; return (PyObject *)newobj;
} }
@ -5744,7 +5750,7 @@ _PyLong_Init(PyThreadState *tstate)
return -1; return -1;
} }
Py_SIZE(v) = size; Py_SET_SIZE(v, size);
v->ob_digit[0] = (digit)abs(ival); v->ob_digit[0] = (digit)abs(ival);
tstate->interp->small_ints[i] = v; tstate->interp->small_ints[i] = v;

View File

@ -1417,8 +1417,9 @@ odict_repr(PyODictObject *self)
} }
count++; count++;
} }
if (count < PyList_GET_SIZE(pieces)) if (count < PyList_GET_SIZE(pieces)) {
Py_SIZE(pieces) = count; Py_SET_SIZE(pieces, count);
}
} }
else { else {
PyObject *items = _PyObject_CallMethodIdNoArgs((PyObject *)self, PyObject *items = _PyObject_CallMethodIdNoArgs((PyObject *)self,

View File

@ -48,7 +48,7 @@
/* Always force the list to the expected size. */ /* 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 *) Py_LOCAL_INLINE(PyObject *)
STRINGLIB(split_whitespace)(PyObject* str_obj, STRINGLIB(split_whitespace)(PyObject* str_obj,

View File

@ -47,7 +47,7 @@ PyStructSequence_New(PyTypeObject *type)
return NULL; return NULL;
/* Hack the size of the variable object, so invisible fields don't appear /* Hack the size of the variable object, so invisible fields don't appear
to Python code. */ to Python code. */
Py_SIZE(obj) = VISIBLE_SIZE_TP(type); Py_SET_SIZE(obj, VISIBLE_SIZE_TP(type));
for (i = 0; i < size; i++) for (i = 0; i < size; i++)
obj->ob_item[i] = NULL; obj->ob_item[i] = NULL;

View File

@ -4436,7 +4436,7 @@ unpack_iterable(PyThreadState *tstate, PyObject *v,
*--sp = PyList_GET_ITEM(l, ll - j); *--sp = PyList_GET_ITEM(l, ll - j);
} }
/* Resize the list. */ /* Resize the list. */
Py_SIZE(l) = ll - argcntafter; Py_SET_SIZE(l, ll - argcntafter);
Py_DECREF(it); Py_DECREF(it);
return 1; return 1;

View File

@ -551,7 +551,7 @@ hamt_node_bitmap_new(Py_ssize_t size)
return NULL; return NULL;
} }
Py_SIZE(node) = size; Py_SET_SIZE(node, size);
for (i = 0; i < size; i++) { for (i = 0; i < size; i++) {
node->b_array[i] = NULL; node->b_array[i] = NULL;
@ -1288,7 +1288,7 @@ hamt_node_collision_new(int32_t hash, Py_ssize_t size)
node->c_array[i] = NULL; node->c_array[i] = NULL;
} }
Py_SIZE(node) = size; Py_SET_SIZE(node, size);
node->c_hash = hash; node->c_hash = hash;
_PyObject_GC_TRACK(node); _PyObject_GC_TRACK(node);

View File

@ -813,7 +813,7 @@ r_PyLong(RFILE *p)
if (ob == NULL) if (ob == NULL)
return NULL; return NULL;
Py_SIZE(ob) = n > 0 ? size : -size; Py_SET_SIZE(ob, n > 0 ? size : -size);
for (i = 0; i < size-1; i++) { for (i = 0; i < size-1; i++) {
d = 0; d = 0;