Skip to content

Commit c0feb99

Browse files
authored
gh-99300: Use Py_NewRef() in Objects/ directory (#99332)
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and Py_XNewRef() in C files of the Objects/ directory.
1 parent 4ce2a20 commit c0feb99

9 files changed

+77
-156
lines changed

Objects/abstract.c

+7-15
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ PyObject_Type(PyObject *o)
4545
}
4646

4747
v = (PyObject *)Py_TYPE(o);
48-
Py_INCREF(v);
49-
return v;
48+
return Py_NewRef(v);
5049
}
5150

5251
Py_ssize_t
@@ -722,9 +721,7 @@ PyBuffer_FillInfo(Py_buffer *view, PyObject *obj, void *buf, Py_ssize_t len,
722721
return -1;
723722
}
724723

725-
view->obj = obj;
726-
if (obj)
727-
Py_INCREF(obj);
724+
view->obj = Py_XNewRef(obj);
728725
view->buf = buf;
729726
view->len = len;
730727
view->readonly = readonly;
@@ -776,8 +773,7 @@ PyObject_Format(PyObject *obj, PyObject *format_spec)
776773
/* Fast path for common types. */
777774
if (format_spec == NULL || PyUnicode_GET_LENGTH(format_spec) == 0) {
778775
if (PyUnicode_CheckExact(obj)) {
779-
Py_INCREF(obj);
780-
return obj;
776+
return Py_NewRef(obj);
781777
}
782778
if (PyLong_CheckExact(obj)) {
783779
return PyObject_Str(obj);
@@ -1405,8 +1401,7 @@ _PyNumber_Index(PyObject *item)
14051401
}
14061402

14071403
if (PyLong_Check(item)) {
1408-
Py_INCREF(item);
1409-
return item;
1404+
return Py_NewRef(item);
14101405
}
14111406
if (!_PyIndex_Check(item)) {
14121407
PyErr_Format(PyExc_TypeError,
@@ -1520,8 +1515,7 @@ PyNumber_Long(PyObject *o)
15201515
}
15211516

15221517
if (PyLong_CheckExact(o)) {
1523-
Py_INCREF(o);
1524-
return o;
1518+
return Py_NewRef(o);
15251519
}
15261520
m = Py_TYPE(o)->tp_as_number;
15271521
if (m && m->nb_int) { /* This should include subclasses of int */
@@ -2045,8 +2039,7 @@ PySequence_Tuple(PyObject *v)
20452039
a tuple *subclass* instance as-is, hence the restriction
20462040
to exact tuples here. In contrast, lists always make
20472041
a copy, so there's no need for exactness below. */
2048-
Py_INCREF(v);
2049-
return v;
2042+
return Py_NewRef(v);
20502043
}
20512044
if (PyList_CheckExact(v))
20522045
return PyList_AsTuple(v);
@@ -2144,8 +2137,7 @@ PySequence_Fast(PyObject *v, const char *m)
21442137
}
21452138

21462139
if (PyList_CheckExact(v) || PyTuple_CheckExact(v)) {
2147-
Py_INCREF(v);
2148-
return v;
2140+
return Py_NewRef(v);
21492141
}
21502142

21512143
it = PyObject_GetIter(v);

Objects/boolobject.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ PyObject *PyBool_FromLong(long ok)
2323
result = Py_True;
2424
else
2525
result = Py_False;
26-
Py_INCREF(result);
27-
return result;
26+
return Py_NewRef(result);
2827
}
2928

3029
/* We define bool_new to always return either Py_True or Py_False */

Objects/bytesobject.c

+17-34
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ static inline PyObject* bytes_get_empty(void)
5353
// Return a strong reference to the empty bytes string singleton.
5454
static inline PyObject* bytes_new_empty(void)
5555
{
56-
Py_INCREF(EMPTY);
57-
return (PyObject *)EMPTY;
56+
return Py_NewRef(EMPTY);
5857
}
5958

6059

@@ -126,8 +125,7 @@ PyBytes_FromStringAndSize(const char *str, Py_ssize_t size)
126125
}
127126
if (size == 1 && str != NULL) {
128127
op = CHARACTER(*str & 255);
129-
Py_INCREF(op);
130-
return (PyObject *)op;
128+
return Py_NewRef(op);
131129
}
132130
if (size == 0) {
133131
return bytes_new_empty();
@@ -162,8 +160,7 @@ PyBytes_FromString(const char *str)
162160
}
163161
else if (size == 1) {
164162
op = CHARACTER(*str & 255);
165-
Py_INCREF(op);
166-
return (PyObject *)op;
163+
return Py_NewRef(op);
167164
}
168165

169166
/* Inline PyObject_NewVar */
@@ -527,14 +524,12 @@ format_obj(PyObject *v, const char **pbuf, Py_ssize_t *plen)
527524
if (PyBytes_Check(v)) {
528525
*pbuf = PyBytes_AS_STRING(v);
529526
*plen = PyBytes_GET_SIZE(v);
530-
Py_INCREF(v);
531-
return v;
527+
return Py_NewRef(v);
532528
}
533529
if (PyByteArray_Check(v)) {
534530
*pbuf = PyByteArray_AS_STRING(v);
535531
*plen = PyByteArray_GET_SIZE(v);
536-
Py_INCREF(v);
537-
return v;
532+
return Py_NewRef(v);
538533
}
539534
/* does it support __bytes__? */
540535
func = _PyObject_LookupSpecial(v, &_Py_ID(__bytes__));
@@ -1411,13 +1406,11 @@ bytes_concat(PyObject *a, PyObject *b)
14111406

14121407
/* Optimize end cases */
14131408
if (va.len == 0 && PyBytes_CheckExact(b)) {
1414-
result = b;
1415-
Py_INCREF(result);
1409+
result = Py_NewRef(b);
14161410
goto done;
14171411
}
14181412
if (vb.len == 0 && PyBytes_CheckExact(a)) {
1419-
result = a;
1420-
Py_INCREF(result);
1413+
result = Py_NewRef(a);
14211414
goto done;
14221415
}
14231416

@@ -1458,8 +1451,7 @@ bytes_repeat(PyBytesObject *a, Py_ssize_t n)
14581451
}
14591452
size = Py_SIZE(a) * n;
14601453
if (size == Py_SIZE(a) && PyBytes_CheckExact(a)) {
1461-
Py_INCREF(a);
1462-
return (PyObject *)a;
1454+
return Py_NewRef(a);
14631455
}
14641456
nbytes = (size_t)size;
14651457
if (nbytes + PyBytesObject_SIZE <= nbytes) {
@@ -1625,8 +1617,7 @@ bytes_subscript(PyBytesObject* self, PyObject* item)
16251617
else if (start == 0 && step == 1 &&
16261618
slicelength == PyBytes_GET_SIZE(self) &&
16271619
PyBytes_CheckExact(self)) {
1628-
Py_INCREF(self);
1629-
return (PyObject *)self;
1620+
return Py_NewRef(self);
16301621
}
16311622
else if (step == 1) {
16321623
return PyBytes_FromStringAndSize(
@@ -1696,8 +1687,7 @@ bytes___bytes___impl(PyBytesObject *self)
16961687
/*[clinic end generated code: output=63a306a9bc0caac5 input=34ec5ddba98bd6bb]*/
16971688
{
16981689
if (PyBytes_CheckExact(self)) {
1699-
Py_INCREF(self);
1700-
return (PyObject *)self;
1690+
return Py_NewRef(self);
17011691
}
17021692
else {
17031693
return PyBytes_FromStringAndSize(self->ob_sval, Py_SIZE(self));
@@ -1922,8 +1912,7 @@ do_xstrip(PyBytesObject *self, int striptype, PyObject *sepobj)
19221912
PyBuffer_Release(&vsep);
19231913

19241914
if (i == 0 && j == len && PyBytes_CheckExact(self)) {
1925-
Py_INCREF(self);
1926-
return (PyObject*)self;
1915+
return Py_NewRef(self);
19271916
}
19281917
else
19291918
return PyBytes_FromStringAndSize(s+i, j-i);
@@ -1952,8 +1941,7 @@ do_strip(PyBytesObject *self, int striptype)
19521941
}
19531942

19541943
if (i == 0 && j == len && PyBytes_CheckExact(self)) {
1955-
Py_INCREF(self);
1956-
return (PyObject*)self;
1944+
return Py_NewRef(self);
19571945
}
19581946
else
19591947
return PyBytes_FromStringAndSize(s+i, j-i);
@@ -2152,8 +2140,7 @@ bytes_translate_impl(PyBytesObject *self, PyObject *table,
21522140
}
21532141
if (!changed && PyBytes_CheckExact(input_obj)) {
21542142
Py_DECREF(result);
2155-
Py_INCREF(input_obj);
2156-
return input_obj;
2143+
return Py_NewRef(input_obj);
21572144
}
21582145
/* Fix the size of the resulting byte string */
21592146
if (inlen > 0)
@@ -2245,8 +2232,7 @@ bytes_removeprefix_impl(PyBytesObject *self, Py_buffer *prefix)
22452232
}
22462233

22472234
if (PyBytes_CheckExact(self)) {
2248-
Py_INCREF(self);
2249-
return (PyObject *)self;
2235+
return Py_NewRef(self);
22502236
}
22512237

22522238
return PyBytes_FromStringAndSize(self_start, self_len);
@@ -2284,8 +2270,7 @@ bytes_removesuffix_impl(PyBytesObject *self, Py_buffer *suffix)
22842270
}
22852271

22862272
if (PyBytes_CheckExact(self)) {
2287-
Py_INCREF(self);
2288-
return (PyObject *)self;
2273+
return Py_NewRef(self);
22892274
}
22902275

22912276
return PyBytes_FromStringAndSize(self_start, self_len);
@@ -2844,8 +2829,7 @@ PyBytes_FromObject(PyObject *x)
28442829
}
28452830

28462831
if (PyBytes_CheckExact(x)) {
2847-
Py_INCREF(x);
2848-
return x;
2832+
return Py_NewRef(x);
28492833
}
28502834

28512835
/* Use the modern buffer interface */
@@ -3269,8 +3253,7 @@ bytes_iter(PyObject *seq)
32693253
if (it == NULL)
32703254
return NULL;
32713255
it->it_index = 0;
3272-
Py_INCREF(seq);
3273-
it->it_seq = (PyBytesObject *)seq;
3256+
it->it_seq = (PyBytesObject *)Py_NewRef(seq);
32743257
_PyObject_GC_TRACK(it);
32753258
return (PyObject *)it;
32763259
}

Objects/call.c

+3-6
Original file line numberDiff line numberDiff line change
@@ -1000,8 +1000,7 @@ _PyStack_UnpackDict(PyThreadState *tstate,
10001000

10011001
/* Copy positional arguments */
10021002
for (Py_ssize_t i = 0; i < nargs; i++) {
1003-
Py_INCREF(args[i]);
1004-
stack[i] = args[i];
1003+
stack[i] = Py_NewRef(args[i]);
10051004
}
10061005

10071006
PyObject **kwstack = stack + nargs;
@@ -1013,10 +1012,8 @@ _PyStack_UnpackDict(PyThreadState *tstate,
10131012
unsigned long keys_are_strings = Py_TPFLAGS_UNICODE_SUBCLASS;
10141013
while (PyDict_Next(kwargs, &pos, &key, &value)) {
10151014
keys_are_strings &= Py_TYPE(key)->tp_flags;
1016-
Py_INCREF(key);
1017-
Py_INCREF(value);
1018-
PyTuple_SET_ITEM(kwnames, i, key);
1019-
kwstack[i] = value;
1015+
PyTuple_SET_ITEM(kwnames, i, Py_NewRef(key));
1016+
kwstack[i] = Py_NewRef(value);
10201017
i++;
10211018
}
10221019

Objects/cellobject.c

+4-8
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ PyCell_New(PyObject *obj)
1111
op = (PyCellObject *)PyObject_GC_New(PyCellObject, &PyCell_Type);
1212
if (op == NULL)
1313
return NULL;
14-
op->ob_ref = obj;
15-
Py_XINCREF(obj);
14+
op->ob_ref = Py_XNewRef(obj);
1615

1716
_PyObject_GC_TRACK(op);
1817
return (PyObject *)op;
@@ -68,8 +67,7 @@ PyCell_Set(PyObject *op, PyObject *value)
6867
return -1;
6968
}
7069
PyObject *old_value = PyCell_GET(op);
71-
Py_XINCREF(value);
72-
PyCell_SET(op, value);
70+
PyCell_SET(op, Py_XNewRef(value));
7371
Py_XDECREF(old_value);
7472
return 0;
7573
}
@@ -135,15 +133,13 @@ cell_get_contents(PyCellObject *op, void *closure)
135133
PyErr_SetString(PyExc_ValueError, "Cell is empty");
136134
return NULL;
137135
}
138-
Py_INCREF(op->ob_ref);
139-
return op->ob_ref;
136+
return Py_NewRef(op->ob_ref);
140137
}
141138

142139
static int
143140
cell_set_contents(PyCellObject *op, PyObject *obj, void *Py_UNUSED(ignored))
144141
{
145-
Py_XINCREF(obj);
146-
Py_XSETREF(op->ob_ref, obj);
142+
Py_XSETREF(op->ob_ref, Py_XNewRef(obj));
147143
return 0;
148144
}
149145

Objects/classobject.c

+8-16
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,8 @@ PyMethod_New(PyObject *func, PyObject *self)
113113
return NULL;
114114
}
115115
im->im_weakreflist = NULL;
116-
Py_INCREF(func);
117-
im->im_func = func;
118-
Py_INCREF(self);
119-
im->im_self = self;
116+
im->im_func = Py_NewRef(func);
117+
im->im_self = Py_NewRef(self);
120118
im->vectorcall = method_vectorcall;
121119
_PyObject_GC_TRACK(im);
122120
return (PyObject *)im;
@@ -195,8 +193,7 @@ method_getattro(PyObject *obj, PyObject *name)
195193
if (f != NULL)
196194
return f(descr, obj, (PyObject *)Py_TYPE(obj));
197195
else {
198-
Py_INCREF(descr);
199-
return descr;
196+
return Py_NewRef(descr);
200197
}
201198
}
202199

@@ -267,8 +264,7 @@ method_richcompare(PyObject *self, PyObject *other, int op)
267264
res = eq ? Py_True : Py_False;
268265
else
269266
res = eq ? Py_False : Py_True;
270-
Py_INCREF(res);
271-
return res;
267+
return Py_NewRef(res);
272268
}
273269

274270
static PyObject *
@@ -359,8 +355,7 @@ PyInstanceMethod_New(PyObject *func) {
359355
method = PyObject_GC_New(PyInstanceMethodObject,
360356
&PyInstanceMethod_Type);
361357
if (method == NULL) return NULL;
362-
Py_INCREF(func);
363-
method->func = func;
358+
method->func = Py_NewRef(func);
364359
_PyObject_GC_TRACK(method);
365360
return (PyObject *)method;
366361
}
@@ -412,8 +407,7 @@ instancemethod_getattro(PyObject *self, PyObject *name)
412407
if (f != NULL)
413408
return f(descr, self, (PyObject *)Py_TYPE(self));
414409
else {
415-
Py_INCREF(descr);
416-
return descr;
410+
return Py_NewRef(descr);
417411
}
418412
}
419413

@@ -443,8 +437,7 @@ static PyObject *
443437
instancemethod_descr_get(PyObject *descr, PyObject *obj, PyObject *type) {
444438
PyObject *func = PyInstanceMethod_GET_FUNCTION(descr);
445439
if (obj == NULL) {
446-
Py_INCREF(func);
447-
return func;
440+
return Py_NewRef(func);
448441
}
449442
else
450443
return PyMethod_New(func, obj);
@@ -472,8 +465,7 @@ instancemethod_richcompare(PyObject *self, PyObject *other, int op)
472465
res = eq ? Py_True : Py_False;
473466
else
474467
res = eq ? Py_False : Py_True;
475-
Py_INCREF(res);
476-
return res;
468+
return Py_NewRef(res);
477469
}
478470

479471
static PyObject *

0 commit comments

Comments
 (0)