Skip to content

Commit 1960eb0

Browse files
authored
gh-99300: Use Py_NewRef() in Objects/ directory (#99351)
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and Py_XNewRef() in C files of the Objects/ directory.
1 parent 584e55b commit 1960eb0

File tree

5 files changed

+100
-188
lines changed

5 files changed

+100
-188
lines changed

Objects/longobject.c

+20-31
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ get_small_int(sdigit ival)
6262
{
6363
assert(IS_SMALL_INT(ival));
6464
PyObject *v = (PyObject *)&_PyLong_SMALL_INTS[_PY_NSMALLNEGINTS + ival];
65-
Py_INCREF(v);
66-
return v;
65+
return Py_NewRef(v);
6766
}
6867

6968
static PyLongObject *
@@ -1785,8 +1784,7 @@ pylong_int_to_decimal_string(PyObject *aa,
17851784
goto success;
17861785
}
17871786
else {
1788-
*p_output = (PyObject *)s;
1789-
Py_INCREF(s);
1787+
*p_output = Py_NewRef(s);
17901788
goto success;
17911789
}
17921790

@@ -2911,8 +2909,7 @@ long_divrem(PyLongObject *a, PyLongObject *b,
29112909
return -1;
29122910
}
29132911
PyObject *zero = _PyLong_GetZero();
2914-
Py_INCREF(zero);
2915-
*pdiv = (PyLongObject*)zero;
2912+
*pdiv = (PyLongObject*)Py_NewRef(zero);
29162913
return 0;
29172914
}
29182915
if (size_b == 1) {
@@ -3747,10 +3744,8 @@ k_mul(PyLongObject *a, PyLongObject *b)
37473744
assert(Py_SIZE(ah) > 0); /* the split isn't degenerate */
37483745

37493746
if (a == b) {
3750-
bh = ah;
3751-
bl = al;
3752-
Py_INCREF(bh);
3753-
Py_INCREF(bl);
3747+
bh = (PyLongObject*)Py_NewRef(ah);
3748+
bl = (PyLongObject*)Py_NewRef(al);
37543749
}
37553750
else if (kmul_split(b, shift, &bh, &bl) < 0) goto fail;
37563751

@@ -3822,8 +3817,7 @@ k_mul(PyLongObject *a, PyLongObject *b)
38223817
ah = al = NULL;
38233818

38243819
if (a == b) {
3825-
t2 = t1;
3826-
Py_INCREF(t2);
3820+
t2 = (PyLongObject*)Py_NewRef(t1);
38273821
}
38283822
else if ((t2 = x_add(bh, bl)) == NULL) {
38293823
Py_DECREF(t1);
@@ -4067,12 +4061,10 @@ pylong_int_divmod(PyLongObject *v, PyLongObject *w,
40674061
return -1;
40684062
}
40694063
if (pdiv != NULL) {
4070-
Py_INCREF(q);
4071-
*pdiv = (PyLongObject *)q;
4064+
*pdiv = (PyLongObject *)Py_NewRef(q);
40724065
}
40734066
if (pmod != NULL) {
4074-
Py_INCREF(r);
4075-
*pmod = (PyLongObject *)r;
4067+
*pmod = (PyLongObject *)Py_NewRef(r);
40764068
}
40774069
Py_DECREF(result);
40784070
return 0;
@@ -4638,11 +4630,10 @@ long_pow(PyObject *v, PyObject *w, PyObject *x)
46384630

46394631
/* a, b, c = v, w, x */
46404632
CHECK_BINOP(v, w);
4641-
a = (PyLongObject*)v; Py_INCREF(a);
4642-
b = (PyLongObject*)w; Py_INCREF(b);
4633+
a = (PyLongObject*)Py_NewRef(v);
4634+
b = (PyLongObject*)Py_NewRef(w);
46434635
if (PyLong_Check(x)) {
4644-
c = (PyLongObject *)x;
4645-
Py_INCREF(x);
4636+
c = (PyLongObject *)Py_NewRef(x);
46464637
}
46474638
else if (x == Py_None)
46484639
c = NULL;
@@ -4824,8 +4815,7 @@ long_pow(PyObject *v, PyObject *w, PyObject *x)
48244815
/* Left-to-right k-ary sliding window exponentiation
48254816
* (Handbook of Applied Cryptography (HAC) Algorithm 14.85)
48264817
*/
4827-
Py_INCREF(a);
4828-
table[0] = a;
4818+
table[0] = (PyLongObject*)Py_NewRef(a);
48294819
num_table_entries = 1;
48304820
MULT(a, a, a2);
48314821
/* table[i] == a**(2*i + 1) % c */
@@ -5362,11 +5352,12 @@ long_or(PyObject *a, PyObject *b)
53625352
static PyObject *
53635353
long_long(PyObject *v)
53645354
{
5365-
if (PyLong_CheckExact(v))
5366-
Py_INCREF(v);
5367-
else
5368-
v = _PyLong_Copy((PyLongObject *)v);
5369-
return v;
5355+
if (PyLong_CheckExact(v)) {
5356+
return Py_NewRef(v);
5357+
}
5358+
else {
5359+
return _PyLong_Copy((PyLongObject *)v);
5360+
}
53705361
}
53715362

53725363
PyObject *
@@ -5473,8 +5464,7 @@ _PyLong_GCD(PyObject *aarg, PyObject *barg)
54735464
Py_SET_SIZE(c, size_a);
54745465
}
54755466
else if (Py_REFCNT(a) == 1) {
5476-
Py_INCREF(a);
5477-
c = a;
5467+
c = (PyLongObject*)Py_NewRef(a);
54785468
}
54795469
else {
54805470
alloc_a = size_a;
@@ -5487,8 +5477,7 @@ _PyLong_GCD(PyObject *aarg, PyObject *barg)
54875477
Py_SET_SIZE(d, size_a);
54885478
}
54895479
else if (Py_REFCNT(b) == 1 && size_a <= alloc_b) {
5490-
Py_INCREF(b);
5491-
d = b;
5480+
d = (PyLongObject*)Py_NewRef(b);
54925481
Py_SET_SIZE(d, size_a);
54935482
}
54945483
else {

Objects/rangeobject.c

+17-34
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,8 @@ range_from_array(PyTypeObject *type, PyObject *const *args, Py_ssize_t num_args)
105105
if (!stop) {
106106
return NULL;
107107
}
108-
start = _PyLong_GetZero();
109-
Py_INCREF(start);
110-
step = _PyLong_GetOne();
111-
Py_INCREF(step);
108+
start = Py_NewRef(_PyLong_GetZero());
109+
step = Py_NewRef(_PyLong_GetOne());
112110
break;
113111
case 0:
114112
PyErr_SetString(PyExc_TypeError,
@@ -216,8 +214,7 @@ compute_range_length(PyObject *start, PyObject *stop, PyObject *step)
216214
if (cmp_result < 0)
217215
return NULL;
218216
result = zero;
219-
Py_INCREF(result);
220-
return result;
217+
return Py_NewRef(result);
221218
}
222219

223220
if ((tmp1 = PyNumber_Subtract(hi, lo)) == NULL)
@@ -297,8 +294,7 @@ compute_range_item(rangeobject *r, PyObject *arg)
297294
return NULL;
298295
}
299296
} else {
300-
i = arg;
301-
Py_INCREF(i);
297+
i = Py_NewRef(arg);
302298
}
303299

304300
/* PyLong equivalent to:
@@ -522,30 +518,24 @@ range_hash(rangeobject *r)
522518
t = PyTuple_New(3);
523519
if (!t)
524520
return -1;
525-
Py_INCREF(r->length);
526-
PyTuple_SET_ITEM(t, 0, r->length);
521+
PyTuple_SET_ITEM(t, 0, Py_NewRef(r->length));
527522
cmp_result = PyObject_Not(r->length);
528523
if (cmp_result == -1)
529524
goto end;
530525
if (cmp_result == 1) {
531-
Py_INCREF(Py_None);
532-
Py_INCREF(Py_None);
533-
PyTuple_SET_ITEM(t, 1, Py_None);
534-
PyTuple_SET_ITEM(t, 2, Py_None);
526+
PyTuple_SET_ITEM(t, 1, Py_NewRef(Py_None));
527+
PyTuple_SET_ITEM(t, 2, Py_NewRef(Py_None));
535528
}
536529
else {
537-
Py_INCREF(r->start);
538-
PyTuple_SET_ITEM(t, 1, r->start);
530+
PyTuple_SET_ITEM(t, 1, Py_NewRef(r->start));
539531
cmp_result = PyObject_RichCompareBool(r->length, _PyLong_GetOne(), Py_EQ);
540532
if (cmp_result == -1)
541533
goto end;
542534
if (cmp_result == 1) {
543-
Py_INCREF(Py_None);
544-
PyTuple_SET_ITEM(t, 2, Py_None);
535+
PyTuple_SET_ITEM(t, 2, Py_NewRef(Py_None));
545536
}
546537
else {
547-
Py_INCREF(r->step);
548-
PyTuple_SET_ITEM(t, 2, r->step);
538+
PyTuple_SET_ITEM(t, 2, Py_NewRef(r->step));
549539
}
550540
}
551541
result = PyObject_Hash(t);
@@ -982,8 +972,7 @@ longrangeiter_setstate(longrangeiterobject *r, PyObject *state)
982972
if (cmp > 0)
983973
state = r->len;
984974
}
985-
Py_INCREF(state);
986-
Py_XSETREF(r->index, state);
975+
Py_XSETREF(r->index, Py_NewRef(state));
987976
Py_RETURN_NONE;
988977
}
989978

@@ -1118,14 +1107,10 @@ range_iter(PyObject *seq)
11181107
if (it == NULL)
11191108
return NULL;
11201109

1121-
it->start = r->start;
1122-
it->step = r->step;
1123-
it->len = r->length;
1124-
it->index = _PyLong_GetZero();
1125-
Py_INCREF(it->start);
1126-
Py_INCREF(it->step);
1127-
Py_INCREF(it->len);
1128-
Py_INCREF(it->index);
1110+
it->start = Py_NewRef(r->start);
1111+
it->step = Py_NewRef(r->step);
1112+
it->len = Py_NewRef(r->length);
1113+
it->index = Py_NewRef(_PyLong_GetZero());
11291114
return (PyObject *)it;
11301115
}
11311116

@@ -1206,8 +1191,7 @@ range_reverse(PyObject *seq, PyObject *Py_UNUSED(ignored))
12061191
it->index = it->start = it->step = NULL;
12071192

12081193
/* start + (len - 1) * step */
1209-
it->len = range->length;
1210-
Py_INCREF(it->len);
1194+
it->len = Py_NewRef(range->length);
12111195

12121196
diff = PyNumber_Subtract(it->len, _PyLong_GetOne());
12131197
if (!diff)
@@ -1228,8 +1212,7 @@ range_reverse(PyObject *seq, PyObject *Py_UNUSED(ignored))
12281212
if (!it->step)
12291213
goto create_failure;
12301214

1231-
it->index = _PyLong_GetZero();
1232-
Py_INCREF(it->index);
1215+
it->index = Py_NewRef(_PyLong_GetZero());
12331216
return (PyObject *)it;
12341217

12351218
create_failure:

Objects/tupleobject.c

+15-30
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ tuple_alloc(Py_ssize_t size)
6161
static inline PyObject *
6262
tuple_get_empty(void)
6363
{
64-
Py_INCREF(&_Py_SINGLETON(tuple_empty));
65-
return (PyObject *)&_Py_SINGLETON(tuple_empty);
64+
return Py_NewRef(&_Py_SINGLETON(tuple_empty));
6665
}
6766

6867
PyObject *
@@ -171,8 +170,7 @@ PyTuple_Pack(Py_ssize_t n, ...)
171170
items = result->ob_item;
172171
for (i = 0; i < n; i++) {
173172
o = va_arg(vargs, PyObject *);
174-
Py_INCREF(o);
175-
items[i] = o;
173+
items[i] = Py_NewRef(o);
176174
}
177175
va_end(vargs);
178176
_PyObject_GC_TRACK(result);
@@ -367,8 +365,7 @@ tupleitem(PyTupleObject *a, Py_ssize_t i)
367365
PyErr_SetString(PyExc_IndexError, "tuple index out of range");
368366
return NULL;
369367
}
370-
Py_INCREF(a->ob_item[i]);
371-
return a->ob_item[i];
368+
return Py_NewRef(a->ob_item[i]);
372369
}
373370

374371
PyObject *
@@ -385,8 +382,7 @@ _PyTuple_FromArray(PyObject *const *src, Py_ssize_t n)
385382
PyObject **dst = tuple->ob_item;
386383
for (Py_ssize_t i = 0; i < n; i++) {
387384
PyObject *item = src[i];
388-
Py_INCREF(item);
389-
dst[i] = item;
385+
dst[i] = Py_NewRef(item);
390386
}
391387
_PyObject_GC_TRACK(tuple);
392388
return (PyObject *)tuple;
@@ -425,8 +421,7 @@ tupleslice(PyTupleObject *a, Py_ssize_t ilow,
425421
if (ihigh < ilow)
426422
ihigh = ilow;
427423
if (ilow == 0 && ihigh == Py_SIZE(a) && PyTuple_CheckExact(a)) {
428-
Py_INCREF(a);
429-
return (PyObject *)a;
424+
return Py_NewRef(a);
430425
}
431426
return _PyTuple_FromArray(a->ob_item + ilow, ihigh - ilow);
432427
}
@@ -449,8 +444,7 @@ tupleconcat(PyTupleObject *a, PyObject *bb)
449444
PyObject **src, **dest;
450445
PyTupleObject *np;
451446
if (Py_SIZE(a) == 0 && PyTuple_CheckExact(bb)) {
452-
Py_INCREF(bb);
453-
return bb;
447+
return Py_NewRef(bb);
454448
}
455449
if (!PyTuple_Check(bb)) {
456450
PyErr_Format(PyExc_TypeError,
@@ -461,8 +455,7 @@ tupleconcat(PyTupleObject *a, PyObject *bb)
461455
PyTupleObject *b = (PyTupleObject *)bb;
462456

463457
if (Py_SIZE(b) == 0 && PyTuple_CheckExact(a)) {
464-
Py_INCREF(a);
465-
return (PyObject *)a;
458+
return Py_NewRef(a);
466459
}
467460
assert((size_t)Py_SIZE(a) + (size_t)Py_SIZE(b) < PY_SSIZE_T_MAX);
468461
size = Py_SIZE(a) + Py_SIZE(b);
@@ -478,15 +471,13 @@ tupleconcat(PyTupleObject *a, PyObject *bb)
478471
dest = np->ob_item;
479472
for (i = 0; i < Py_SIZE(a); i++) {
480473
PyObject *v = src[i];
481-
Py_INCREF(v);
482-
dest[i] = v;
474+
dest[i] = Py_NewRef(v);
483475
}
484476
src = b->ob_item;
485477
dest = np->ob_item + Py_SIZE(a);
486478
for (i = 0; i < Py_SIZE(b); i++) {
487479
PyObject *v = src[i];
488-
Py_INCREF(v);
489-
dest[i] = v;
480+
dest[i] = Py_NewRef(v);
490481
}
491482
_PyObject_GC_TRACK(np);
492483
return (PyObject *)np;
@@ -500,8 +491,7 @@ tuplerepeat(PyTupleObject *a, Py_ssize_t n)
500491
if (PyTuple_CheckExact(a)) {
501492
/* Since tuples are immutable, we can return a shared
502493
copy in this case */
503-
Py_INCREF(a);
504-
return (PyObject *)a;
494+
return Py_NewRef(a);
505495
}
506496
}
507497
if (input_size == 0 || n <= 0) {
@@ -747,8 +737,7 @@ tuple_subtype_new(PyTypeObject *type, PyObject *iterable)
747737
}
748738
for (i = 0; i < n; i++) {
749739
item = PyTuple_GET_ITEM(tmp, i);
750-
Py_INCREF(item);
751-
PyTuple_SET_ITEM(newobj, i, item);
740+
PyTuple_SET_ITEM(newobj, i, Py_NewRef(item));
752741
}
753742
Py_DECREF(tmp);
754743

@@ -799,8 +788,7 @@ tuplesubscript(PyTupleObject* self, PyObject* item)
799788
else if (start == 0 && step == 1 &&
800789
slicelength == PyTuple_GET_SIZE(self) &&
801790
PyTuple_CheckExact(self)) {
802-
Py_INCREF(self);
803-
return (PyObject *)self;
791+
return Py_NewRef(self);
804792
}
805793
else {
806794
PyTupleObject* result = tuple_alloc(slicelength);
@@ -810,8 +798,7 @@ tuplesubscript(PyTupleObject* self, PyObject* item)
810798
dest = result->ob_item;
811799
for (cur = start, i = 0; i < slicelength;
812800
cur += step, i++) {
813-
it = src[cur];
814-
Py_INCREF(it);
801+
it = Py_NewRef(src[cur]);
815802
dest[i] = it;
816803
}
817804

@@ -1044,8 +1031,7 @@ tupleiter_next(tupleiterobject *it)
10441031
if (it->it_index < PyTuple_GET_SIZE(seq)) {
10451032
item = PyTuple_GET_ITEM(seq, it->it_index);
10461033
++it->it_index;
1047-
Py_INCREF(item);
1048-
return item;
1034+
return Py_NewRef(item);
10491035
}
10501036

10511037
it->it_seq = NULL;
@@ -1146,8 +1132,7 @@ tuple_iter(PyObject *seq)
11461132
if (it == NULL)
11471133
return NULL;
11481134
it->it_index = 0;
1149-
Py_INCREF(seq);
1150-
it->it_seq = (PyTupleObject *)seq;
1135+
it->it_seq = (PyTupleObject *)Py_NewRef(seq);
11511136
_PyObject_GC_TRACK(it);
11521137
return (PyObject *)it;
11531138
}

0 commit comments

Comments
 (0)