Skip to content

Commit 60ac6ed

Browse files
authored
bpo-39573: Use Py_SET_SIZE() function (GH-18402)
Replace direct acccess to PyVarObject.ob_size with usage of the Py_SET_SIZE() function.
1 parent de6f38d commit 60ac6ed

16 files changed

+95
-86
lines changed

Modules/_asynciomodule.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1007,7 +1007,7 @@ _asyncio_Future_remove_done_callback(FutureObj *self, PyObject *fn)
10071007
}
10081008

10091009
if (j < len) {
1010-
Py_SIZE(newlist) = j;
1010+
Py_SET_SIZE(newlist, j);
10111011
}
10121012
j = PyList_GET_SIZE(newlist);
10131013
len = PyList_GET_SIZE(self->fut_callbacks);

Modules/_collectionsmodule.c

+8-8
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ deque_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
172172
MARK_END(b->rightlink);
173173

174174
assert(BLOCKLEN >= 2);
175-
Py_SIZE(deque) = 0;
175+
Py_SET_SIZE(deque, 0);
176176
deque->leftblock = b;
177177
deque->rightblock = b;
178178
deque->leftindex = CENTER + 1;
@@ -196,7 +196,7 @@ deque_pop(dequeobject *deque, PyObject *unused)
196196
}
197197
item = deque->rightblock->data[deque->rightindex];
198198
deque->rightindex--;
199-
Py_SIZE(deque)--;
199+
Py_SET_SIZE(deque, Py_SIZE(deque) - 1);
200200
deque->state++;
201201

202202
if (deque->rightindex < 0) {
@@ -234,7 +234,7 @@ deque_popleft(dequeobject *deque, PyObject *unused)
234234
assert(deque->leftblock != NULL);
235235
item = deque->leftblock->data[deque->leftindex];
236236
deque->leftindex++;
237-
Py_SIZE(deque)--;
237+
Py_SET_SIZE(deque, Py_SIZE(deque) - 1);
238238
deque->state++;
239239

240240
if (deque->leftindex == BLOCKLEN) {
@@ -287,7 +287,7 @@ deque_append_internal(dequeobject *deque, PyObject *item, Py_ssize_t maxlen)
287287
MARK_END(b->rightlink);
288288
deque->rightindex = -1;
289289
}
290-
Py_SIZE(deque)++;
290+
Py_SET_SIZE(deque, Py_SIZE(deque) + 1);
291291
deque->rightindex++;
292292
deque->rightblock->data[deque->rightindex] = item;
293293
if (NEEDS_TRIM(deque, maxlen)) {
@@ -324,7 +324,7 @@ deque_appendleft_internal(dequeobject *deque, PyObject *item, Py_ssize_t maxlen)
324324
MARK_END(b->leftlink);
325325
deque->leftindex = BLOCKLEN;
326326
}
327-
Py_SIZE(deque)++;
327+
Py_SET_SIZE(deque, Py_SIZE(deque) + 1);
328328
deque->leftindex--;
329329
deque->leftblock->data[deque->leftindex] = item;
330330
if (NEEDS_TRIM(deque, deque->maxlen)) {
@@ -597,7 +597,7 @@ deque_clear(dequeobject *deque)
597597
/* Set the deque to be empty using the newly allocated block */
598598
MARK_END(b->leftlink);
599599
MARK_END(b->rightlink);
600-
Py_SIZE(deque) = 0;
600+
Py_SET_SIZE(deque, 0);
601601
deque->leftblock = b;
602602
deque->rightblock = b;
603603
deque->leftindex = CENTER + 1;
@@ -680,7 +680,7 @@ deque_inplace_repeat(dequeobject *deque, Py_ssize_t n)
680680
if (deque->rightindex == BLOCKLEN - 1) {
681681
block *b = newblock();
682682
if (b == NULL) {
683-
Py_SIZE(deque) += i;
683+
Py_SET_SIZE(deque, Py_SIZE(deque) + i);
684684
return NULL;
685685
}
686686
b->leftlink = deque->rightblock;
@@ -700,7 +700,7 @@ deque_inplace_repeat(dequeobject *deque, Py_ssize_t n)
700700
deque->rightblock->data[deque->rightindex] = item;
701701
}
702702
}
703-
Py_SIZE(deque) += i;
703+
Py_SET_SIZE(deque, Py_SIZE(deque) + i);
704704
Py_INCREF(deque);
705705
return (PyObject *)deque;
706706
}

Modules/_decimal/_decimal.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -3253,9 +3253,9 @@ dec_as_long(PyObject *dec, PyObject *context, int round)
32533253
i--;
32543254
}
32553255

3256-
Py_SIZE(pylong) = i;
3256+
Py_SET_SIZE(pylong, i);
32573257
if (mpd_isnegative(x) && !mpd_iszero(x)) {
3258-
Py_SIZE(pylong) = -i;
3258+
Py_SET_SIZE(pylong, -i);
32593259
}
32603260

32613261
mpd_del(x);

Modules/_pickle.c

+13-11
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ Pdata_New(void)
461461

462462
if (!(self = PyObject_New(Pdata, &Pdata_Type)))
463463
return NULL;
464-
Py_SIZE(self) = 0;
464+
Py_SET_SIZE(self, 0);
465465
self->mark_set = 0;
466466
self->fence = 0;
467467
self->allocated = 8;
@@ -488,7 +488,7 @@ Pdata_clear(Pdata *self, Py_ssize_t clearto)
488488
while (--i >= clearto) {
489489
Py_CLEAR(self->data[i]);
490490
}
491-
Py_SIZE(self) = clearto;
491+
Py_SET_SIZE(self, clearto);
492492
return 0;
493493
}
494494

@@ -539,7 +539,8 @@ Pdata_pop(Pdata *self)
539539
Pdata_stack_underflow(self);
540540
return NULL;
541541
}
542-
return self->data[--Py_SIZE(self)];
542+
Py_SET_SIZE(self, Py_SIZE(self) - 1);
543+
return self->data[Py_SIZE(self)];
543544
}
544545
#define PDATA_POP(D, V) do { (V) = Pdata_pop((D)); } while (0)
545546

@@ -549,7 +550,8 @@ Pdata_push(Pdata *self, PyObject *obj)
549550
if (Py_SIZE(self) == self->allocated && Pdata_grow(self) < 0) {
550551
return -1;
551552
}
552-
self->data[Py_SIZE(self)++] = obj;
553+
self->data[Py_SIZE(self)] = obj;
554+
Py_SET_SIZE(self, Py_SIZE(self) + 1);
553555
return 0;
554556
}
555557

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

582-
Py_SIZE(self) = start;
584+
Py_SET_SIZE(self, start);
583585
return tuple;
584586
}
585587

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

599-
Py_SIZE(self) = start;
601+
Py_SET_SIZE(self, start);
600602
return list;
601603
}
602604

@@ -6134,7 +6136,7 @@ load_pop(UnpicklerObject *self)
61346136
else {
61356137
len--;
61366138
Py_DECREF(self->stack->data[len]);
6137-
Py_SIZE(self->stack) = len;
6139+
Py_SET_SIZE(self->stack, len);
61386140
}
61396141
return 0;
61406142
}
@@ -6495,13 +6497,13 @@ do_append(UnpicklerObject *self, Py_ssize_t x)
64956497
result = _Pickle_FastCall(append_func, value);
64966498
if (result == NULL) {
64976499
Pdata_clear(self->stack, i + 1);
6498-
Py_SIZE(self->stack) = x;
6500+
Py_SET_SIZE(self->stack, x);
64996501
Py_DECREF(append_func);
65006502
return -1;
65016503
}
65026504
Py_DECREF(result);
65036505
}
6504-
Py_SIZE(self->stack) = x;
6506+
Py_SET_SIZE(self->stack, x);
65056507
Py_DECREF(append_func);
65066508
}
65076509
}
@@ -6623,12 +6625,12 @@ load_additems(UnpicklerObject *self)
66236625
result = _Pickle_FastCall(add_func, item);
66246626
if (result == NULL) {
66256627
Pdata_clear(self->stack, i + 1);
6626-
Py_SIZE(self->stack) = mark;
6628+
Py_SET_SIZE(self->stack, mark);
66276629
return -1;
66286630
}
66296631
Py_DECREF(result);
66306632
}
6631-
Py_SIZE(self->stack) = mark;
6633+
Py_SET_SIZE(self->stack, mark);
66326634
}
66336635

66346636
return 0;

Modules/arraymodule.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,14 @@ array_resize(arrayobject *self, Py_ssize_t newsize)
128128
if (self->allocated >= newsize &&
129129
Py_SIZE(self) < newsize + 16 &&
130130
self->ob_item != NULL) {
131-
Py_SIZE(self) = newsize;
131+
Py_SET_SIZE(self, newsize);
132132
return 0;
133133
}
134134

135135
if (newsize == 0) {
136136
PyMem_FREE(self->ob_item);
137137
self->ob_item = NULL;
138-
Py_SIZE(self) = 0;
138+
Py_SET_SIZE(self, 0);
139139
self->allocated = 0;
140140
return 0;
141141
}
@@ -165,7 +165,7 @@ array_resize(arrayobject *self, Py_ssize_t newsize)
165165
return -1;
166166
}
167167
self->ob_item = items;
168-
Py_SIZE(self) = newsize;
168+
Py_SET_SIZE(self, newsize);
169169
self->allocated = _new_size;
170170
return 0;
171171
}
@@ -593,7 +593,7 @@ newarrayobject(PyTypeObject *type, Py_ssize_t size, const struct arraydescr *des
593593
op->ob_descr = descr;
594594
op->allocated = size;
595595
op->weakreflist = NULL;
596-
Py_SIZE(op) = size;
596+
Py_SET_SIZE(op, size);
597597
if (size <= 0) {
598598
op->ob_item = NULL;
599599
}
@@ -2696,7 +2696,7 @@ array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
26962696
return NULL;
26972697
}
26982698
self->ob_item = item;
2699-
Py_SIZE(self) = n / sizeof(Py_UNICODE);
2699+
Py_SET_SIZE(self, n / sizeof(Py_UNICODE));
27002700
memcpy(item, ustr, n);
27012701
self->allocated = Py_SIZE(self);
27022702
}

Modules/gcmodule.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2294,7 +2294,7 @@ _PyObject_GC_Resize(PyVarObject *op, Py_ssize_t nitems)
22942294
if (g == NULL)
22952295
return (PyVarObject *)PyErr_NoMemory();
22962296
op = (PyVarObject *) FROM_GC(g);
2297-
Py_SIZE(op) = nitems;
2297+
Py_SET_SIZE(op, nitems);
22982298
return op;
22992299
}
23002300

Objects/bytearrayobject.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ PyByteArray_FromStringAndSize(const char *bytes, Py_ssize_t size)
148148
memcpy(new->ob_bytes, bytes, size);
149149
new->ob_bytes[size] = '\0'; /* Trailing null byte */
150150
}
151-
Py_SIZE(new) = size;
151+
Py_SET_SIZE(new, size);
152152
new->ob_alloc = alloc;
153153
new->ob_start = new->ob_bytes;
154154
new->ob_exports = 0;
@@ -206,7 +206,7 @@ PyByteArray_Resize(PyObject *self, Py_ssize_t requested_size)
206206
}
207207
else {
208208
/* Minor downsize; quick exit */
209-
Py_SIZE(self) = size;
209+
Py_SET_SIZE(self, size);
210210
PyByteArray_AS_STRING(self)[size] = '\0'; /* Trailing null */
211211
return 0;
212212
}
@@ -246,7 +246,7 @@ PyByteArray_Resize(PyObject *self, Py_ssize_t requested_size)
246246
}
247247

248248
obj->ob_bytes = obj->ob_start = sval;
249-
Py_SIZE(self) = size;
249+
Py_SET_SIZE(self, size);
250250
obj->ob_alloc = alloc;
251251
obj->ob_bytes[size] = '\0'; /* Trailing null byte */
252252

@@ -498,7 +498,7 @@ bytearray_setslice_linear(PyByteArrayObject *self,
498498
}
499499
/* memmove() removed bytes, the bytearray object cannot be
500500
restored in its previous state. */
501-
Py_SIZE(self) += growth;
501+
Py_SET_SIZE(self, Py_SIZE(self) + growth);
502502
res = -1;
503503
}
504504
buf = PyByteArray_AS_STRING(self);
@@ -886,7 +886,7 @@ bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds)
886886

887887
/* Append the byte */
888888
if (Py_SIZE(self) + 1 < self->ob_alloc) {
889-
Py_SIZE(self)++;
889+
Py_SET_SIZE(self, Py_SIZE(self) + 1);
890890
PyByteArray_AS_STRING(self)[Py_SIZE(self)] = '\0';
891891
}
892892
else if (PyByteArray_Resize((PyObject *)self, Py_SIZE(self)+1) < 0)

Objects/bytesobject.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2963,7 +2963,7 @@ _PyBytes_Resize(PyObject **pv, Py_ssize_t newsize)
29632963
}
29642964
_Py_NewReference(*pv);
29652965
sv = (PyBytesObject *) *pv;
2966-
Py_SIZE(sv) = newsize;
2966+
Py_SET_SIZE(sv, newsize);
29672967
sv->ob_sval[newsize] = '\0';
29682968
sv->ob_shash = -1; /* invalidate cached hash value */
29692969
return 0;

0 commit comments

Comments
 (0)