Skip to content

Commit

Permalink
bpo-39573: Use Py_SET_SIZE() function (pythonGH-18402)
Browse files Browse the repository at this point in the history
Replace direct acccess to PyVarObject.ob_size with usage of
the Py_SET_SIZE() function.
  • Loading branch information
vstinner authored Feb 7, 2020
1 parent de6f38d commit 60ac6ed
Show file tree
Hide file tree
Showing 16 changed files with 95 additions and 86 deletions.
2 changes: 1 addition & 1 deletion Modules/_asynciomodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1007,7 +1007,7 @@ _asyncio_Future_remove_done_callback(FutureObj *self, PyObject *fn)
}

if (j < len) {
Py_SIZE(newlist) = j;
Py_SET_SIZE(newlist, j);
}
j = PyList_GET_SIZE(newlist);
len = PyList_GET_SIZE(self->fut_callbacks);
Expand Down
16 changes: 8 additions & 8 deletions Modules/_collectionsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ deque_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
MARK_END(b->rightlink);

assert(BLOCKLEN >= 2);
Py_SIZE(deque) = 0;
Py_SET_SIZE(deque, 0);
deque->leftblock = b;
deque->rightblock = b;
deque->leftindex = CENTER + 1;
Expand All @@ -196,7 +196,7 @@ deque_pop(dequeobject *deque, PyObject *unused)
}
item = deque->rightblock->data[deque->rightindex];
deque->rightindex--;
Py_SIZE(deque)--;
Py_SET_SIZE(deque, Py_SIZE(deque) - 1);
deque->state++;

if (deque->rightindex < 0) {
Expand Down Expand Up @@ -234,7 +234,7 @@ deque_popleft(dequeobject *deque, PyObject *unused)
assert(deque->leftblock != NULL);
item = deque->leftblock->data[deque->leftindex];
deque->leftindex++;
Py_SIZE(deque)--;
Py_SET_SIZE(deque, Py_SIZE(deque) - 1);
deque->state++;

if (deque->leftindex == BLOCKLEN) {
Expand Down Expand Up @@ -287,7 +287,7 @@ deque_append_internal(dequeobject *deque, PyObject *item, Py_ssize_t maxlen)
MARK_END(b->rightlink);
deque->rightindex = -1;
}
Py_SIZE(deque)++;
Py_SET_SIZE(deque, Py_SIZE(deque) + 1);
deque->rightindex++;
deque->rightblock->data[deque->rightindex] = item;
if (NEEDS_TRIM(deque, maxlen)) {
Expand Down Expand Up @@ -324,7 +324,7 @@ deque_appendleft_internal(dequeobject *deque, PyObject *item, Py_ssize_t maxlen)
MARK_END(b->leftlink);
deque->leftindex = BLOCKLEN;
}
Py_SIZE(deque)++;
Py_SET_SIZE(deque, Py_SIZE(deque) + 1);
deque->leftindex--;
deque->leftblock->data[deque->leftindex] = item;
if (NEEDS_TRIM(deque, deque->maxlen)) {
Expand Down Expand Up @@ -597,7 +597,7 @@ deque_clear(dequeobject *deque)
/* Set the deque to be empty using the newly allocated block */
MARK_END(b->leftlink);
MARK_END(b->rightlink);
Py_SIZE(deque) = 0;
Py_SET_SIZE(deque, 0);
deque->leftblock = b;
deque->rightblock = b;
deque->leftindex = CENTER + 1;
Expand Down Expand Up @@ -680,7 +680,7 @@ deque_inplace_repeat(dequeobject *deque, Py_ssize_t n)
if (deque->rightindex == BLOCKLEN - 1) {
block *b = newblock();
if (b == NULL) {
Py_SIZE(deque) += i;
Py_SET_SIZE(deque, Py_SIZE(deque) + i);
return NULL;
}
b->leftlink = deque->rightblock;
Expand All @@ -700,7 +700,7 @@ deque_inplace_repeat(dequeobject *deque, Py_ssize_t n)
deque->rightblock->data[deque->rightindex] = item;
}
}
Py_SIZE(deque) += i;
Py_SET_SIZE(deque, Py_SIZE(deque) + i);
Py_INCREF(deque);
return (PyObject *)deque;
}
Expand Down
4 changes: 2 additions & 2 deletions Modules/_decimal/_decimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -3253,9 +3253,9 @@ dec_as_long(PyObject *dec, PyObject *context, int round)
i--;
}

Py_SIZE(pylong) = i;
Py_SET_SIZE(pylong, i);
if (mpd_isnegative(x) && !mpd_iszero(x)) {
Py_SIZE(pylong) = -i;
Py_SET_SIZE(pylong, -i);
}

mpd_del(x);
Expand Down
24 changes: 13 additions & 11 deletions Modules/_pickle.c
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ Pdata_New(void)

if (!(self = PyObject_New(Pdata, &Pdata_Type)))
return NULL;
Py_SIZE(self) = 0;
Py_SET_SIZE(self, 0);
self->mark_set = 0;
self->fence = 0;
self->allocated = 8;
Expand All @@ -488,7 +488,7 @@ Pdata_clear(Pdata *self, Py_ssize_t clearto)
while (--i >= clearto) {
Py_CLEAR(self->data[i]);
}
Py_SIZE(self) = clearto;
Py_SET_SIZE(self, clearto);
return 0;
}

Expand Down Expand Up @@ -539,7 +539,8 @@ Pdata_pop(Pdata *self)
Pdata_stack_underflow(self);
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)

Expand All @@ -549,7 +550,8 @@ Pdata_push(Pdata *self, PyObject *obj)
if (Py_SIZE(self) == self->allocated && Pdata_grow(self) < 0) {
return -1;
}
self->data[Py_SIZE(self)++] = obj;
self->data[Py_SIZE(self)] = obj;
Py_SET_SIZE(self, Py_SIZE(self) + 1);
return 0;
}

Expand Down Expand Up @@ -579,7 +581,7 @@ Pdata_poptuple(Pdata *self, Py_ssize_t start)
for (i = start, j = 0; j < len; i++, j++)
PyTuple_SET_ITEM(tuple, j, self->data[i]);

Py_SIZE(self) = start;
Py_SET_SIZE(self, start);
return tuple;
}

Expand All @@ -596,7 +598,7 @@ Pdata_poplist(Pdata *self, Py_ssize_t start)
for (i = start, j = 0; j < len; i++, j++)
PyList_SET_ITEM(list, j, self->data[i]);

Py_SIZE(self) = start;
Py_SET_SIZE(self, start);
return list;
}

Expand Down Expand Up @@ -6134,7 +6136,7 @@ load_pop(UnpicklerObject *self)
else {
len--;
Py_DECREF(self->stack->data[len]);
Py_SIZE(self->stack) = len;
Py_SET_SIZE(self->stack, len);
}
return 0;
}
Expand Down Expand Up @@ -6495,13 +6497,13 @@ do_append(UnpicklerObject *self, Py_ssize_t x)
result = _Pickle_FastCall(append_func, value);
if (result == NULL) {
Pdata_clear(self->stack, i + 1);
Py_SIZE(self->stack) = x;
Py_SET_SIZE(self->stack, x);
Py_DECREF(append_func);
return -1;
}
Py_DECREF(result);
}
Py_SIZE(self->stack) = x;
Py_SET_SIZE(self->stack, x);
Py_DECREF(append_func);
}
}
Expand Down Expand Up @@ -6623,12 +6625,12 @@ load_additems(UnpicklerObject *self)
result = _Pickle_FastCall(add_func, item);
if (result == NULL) {
Pdata_clear(self->stack, i + 1);
Py_SIZE(self->stack) = mark;
Py_SET_SIZE(self->stack, mark);
return -1;
}
Py_DECREF(result);
}
Py_SIZE(self->stack) = mark;
Py_SET_SIZE(self->stack, mark);
}

return 0;
Expand Down
10 changes: 5 additions & 5 deletions Modules/arraymodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,14 @@ array_resize(arrayobject *self, Py_ssize_t newsize)
if (self->allocated >= newsize &&
Py_SIZE(self) < newsize + 16 &&
self->ob_item != NULL) {
Py_SIZE(self) = newsize;
Py_SET_SIZE(self, newsize);
return 0;
}

if (newsize == 0) {
PyMem_FREE(self->ob_item);
self->ob_item = NULL;
Py_SIZE(self) = 0;
Py_SET_SIZE(self, 0);
self->allocated = 0;
return 0;
}
Expand Down Expand Up @@ -165,7 +165,7 @@ array_resize(arrayobject *self, Py_ssize_t newsize)
return -1;
}
self->ob_item = items;
Py_SIZE(self) = newsize;
Py_SET_SIZE(self, newsize);
self->allocated = _new_size;
return 0;
}
Expand Down Expand Up @@ -593,7 +593,7 @@ newarrayobject(PyTypeObject *type, Py_ssize_t size, const struct arraydescr *des
op->ob_descr = descr;
op->allocated = size;
op->weakreflist = NULL;
Py_SIZE(op) = size;
Py_SET_SIZE(op, size);
if (size <= 0) {
op->ob_item = NULL;
}
Expand Down Expand Up @@ -2696,7 +2696,7 @@ array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return NULL;
}
self->ob_item = item;
Py_SIZE(self) = n / sizeof(Py_UNICODE);
Py_SET_SIZE(self, n / sizeof(Py_UNICODE));
memcpy(item, ustr, n);
self->allocated = Py_SIZE(self);
}
Expand Down
2 changes: 1 addition & 1 deletion Modules/gcmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2294,7 +2294,7 @@ _PyObject_GC_Resize(PyVarObject *op, Py_ssize_t nitems)
if (g == NULL)
return (PyVarObject *)PyErr_NoMemory();
op = (PyVarObject *) FROM_GC(g);
Py_SIZE(op) = nitems;
Py_SET_SIZE(op, nitems);
return op;
}

Expand Down
10 changes: 5 additions & 5 deletions Objects/bytearrayobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ PyByteArray_FromStringAndSize(const char *bytes, Py_ssize_t size)
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;
Expand Down Expand Up @@ -206,7 +206,7 @@ PyByteArray_Resize(PyObject *self, Py_ssize_t requested_size)
}
else {
/* Minor downsize; quick exit */
Py_SIZE(self) = size;
Py_SET_SIZE(self, size);
PyByteArray_AS_STRING(self)[size] = '\0'; /* Trailing null */
return 0;
}
Expand Down Expand Up @@ -246,7 +246,7 @@ PyByteArray_Resize(PyObject *self, Py_ssize_t requested_size)
}

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 */

Expand Down Expand Up @@ -498,7 +498,7 @@ bytearray_setslice_linear(PyByteArrayObject *self,
}
/* 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);
Expand Down Expand Up @@ -886,7 +886,7 @@ bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds)

/* 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)
Expand Down
2 changes: 1 addition & 1 deletion Objects/bytesobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2963,7 +2963,7 @@ _PyBytes_Resize(PyObject **pv, Py_ssize_t newsize)
}
_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;
Expand Down
Loading

0 comments on commit 60ac6ed

Please sign in to comment.