Skip to content

Commit 584e55b

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

8 files changed

+120
-244
lines changed

Diff for: Objects/enumobject.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -389,8 +389,7 @@ reversed_new_impl(PyTypeObject *type, PyObject *seq)
389389
return NULL;
390390

391391
ro->index = n-1;
392-
Py_INCREF(seq);
393-
ro->seq = seq;
392+
ro->seq = Py_NewRef(seq);
394393
return (PyObject *)ro;
395394
}
396395

Diff for: Objects/exceptions.c

+37-83
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ BaseException_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
5353
self->suppress_context = 0;
5454

5555
if (args) {
56-
self->args = args;
57-
Py_INCREF(args);
56+
self->args = Py_NewRef(args);
5857
return (PyObject *)self;
5958
}
6059

@@ -73,9 +72,7 @@ BaseException_init(PyBaseExceptionObject *self, PyObject *args, PyObject *kwds)
7372
if (!_PyArg_NoKeywords(Py_TYPE(self)->tp_name, kwds))
7473
return -1;
7574

76-
Py_INCREF(args);
77-
Py_XSETREF(self->args, args);
78-
75+
Py_XSETREF(self->args, Py_NewRef(args));
7976
return 0;
8077
}
8178

@@ -185,8 +182,7 @@ BaseException_with_traceback(PyObject *self, PyObject *tb) {
185182
if (PyException_SetTraceback(self, tb))
186183
return NULL;
187184

188-
Py_INCREF(self);
189-
return self;
185+
return Py_NewRef(self);
190186
}
191187

192188
PyDoc_STRVAR(with_traceback_doc,
@@ -258,8 +254,7 @@ BaseException_get_args(PyBaseExceptionObject *self, void *Py_UNUSED(ignored))
258254
if (self->args == NULL) {
259255
Py_RETURN_NONE;
260256
}
261-
Py_INCREF(self->args);
262-
return self->args;
257+
return Py_NewRef(self->args);
263258
}
264259

265260
static int
@@ -283,8 +278,7 @@ BaseException_get_tb(PyBaseExceptionObject *self, void *Py_UNUSED(ignored))
283278
if (self->traceback == NULL) {
284279
Py_RETURN_NONE;
285280
}
286-
Py_INCREF(self->traceback);
287-
return self->traceback;
281+
return Py_NewRef(self->traceback);
288282
}
289283

290284
static int
@@ -300,8 +294,7 @@ BaseException_set_tb(PyBaseExceptionObject *self, PyObject *tb, void *Py_UNUSED(
300294
return -1;
301295
}
302296

303-
Py_INCREF(tb);
304-
Py_XSETREF(self->traceback, tb);
297+
Py_XSETREF(self->traceback, Py_NewRef(tb));
305298
return 0;
306299
}
307300

@@ -380,8 +373,7 @@ PyObject *
380373
PyException_GetTraceback(PyObject *self)
381374
{
382375
PyBaseExceptionObject *base_self = _PyBaseExceptionObject_cast(self);
383-
Py_XINCREF(base_self->traceback);
384-
return base_self->traceback;
376+
return Py_XNewRef(base_self->traceback);
385377
}
386378

387379

@@ -395,8 +387,7 @@ PyObject *
395387
PyException_GetCause(PyObject *self)
396388
{
397389
PyObject *cause = _PyBaseExceptionObject_cast(self)->cause;
398-
Py_XINCREF(cause);
399-
return cause;
390+
return Py_XNewRef(cause);
400391
}
401392

402393
/* Steals a reference to cause */
@@ -412,8 +403,7 @@ PyObject *
412403
PyException_GetContext(PyObject *self)
413404
{
414405
PyObject *context = _PyBaseExceptionObject_cast(self)->context;
415-
Py_XINCREF(context);
416-
return context;
406+
return Py_XNewRef(context);
417407
}
418408

419409
/* Steals a reference to context */
@@ -579,8 +569,7 @@ StopIteration_init(PyStopIterationObject *self, PyObject *args, PyObject *kwds)
579569
value = PyTuple_GET_ITEM(args, 0);
580570
else
581571
value = Py_None;
582-
Py_INCREF(value);
583-
self->value = value;
572+
self->value = Py_NewRef(value);
584573
return 0;
585574
}
586575

@@ -633,12 +622,10 @@ SystemExit_init(PySystemExitObject *self, PyObject *args, PyObject *kwds)
633622
if (size == 0)
634623
return 0;
635624
if (size == 1) {
636-
Py_INCREF(PyTuple_GET_ITEM(args, 0));
637-
Py_XSETREF(self->code, PyTuple_GET_ITEM(args, 0));
625+
Py_XSETREF(self->code, Py_NewRef(PyTuple_GET_ITEM(args, 0)));
638626
}
639627
else { /* size > 1 */
640-
Py_INCREF(args);
641-
Py_XSETREF(self->code, args);
628+
Py_XSETREF(self->code, Py_NewRef(args));
642629
}
643630
return 0;
644631
}
@@ -1493,18 +1480,12 @@ ImportError_init(PyImportErrorObject *self, PyObject *args, PyObject *kwds)
14931480
}
14941481
Py_DECREF(empty_tuple);
14951482

1496-
Py_XINCREF(name);
1497-
Py_XSETREF(self->name, name);
1498-
1499-
Py_XINCREF(path);
1500-
Py_XSETREF(self->path, path);
1501-
1502-
Py_XINCREF(name_from);
1503-
Py_XSETREF(self->name_from, name_from);
1483+
Py_XSETREF(self->name, Py_XNewRef(name));
1484+
Py_XSETREF(self->path, Py_XNewRef(path));
1485+
Py_XSETREF(self->name_from, Py_XNewRef(name_from));
15041486

15051487
if (PyTuple_GET_SIZE(args) == 1) {
1506-
msg = PyTuple_GET_ITEM(args, 0);
1507-
Py_INCREF(msg);
1488+
msg = Py_NewRef(PyTuple_GET_ITEM(args, 0));
15081489
}
15091490
Py_XSETREF(self->msg, msg);
15101491

@@ -1543,8 +1524,7 @@ static PyObject *
15431524
ImportError_str(PyImportErrorObject *self)
15441525
{
15451526
if (self->msg && PyUnicode_CheckExact(self->msg)) {
1546-
Py_INCREF(self->msg);
1547-
return self->msg;
1527+
return Py_NewRef(self->msg);
15481528
}
15491529
else {
15501530
return BaseException_str((PyBaseExceptionObject *)self);
@@ -1574,8 +1554,7 @@ ImportError_getstate(PyImportErrorObject *self)
15741554
return dict;
15751555
}
15761556
else if (dict) {
1577-
Py_INCREF(dict);
1578-
return dict;
1557+
return Py_NewRef(dict);
15791558
}
15801559
else {
15811560
Py_RETURN_NONE;
@@ -1702,8 +1681,7 @@ oserror_parse_args(PyObject **p_args,
17021681
PyTuple_SET_ITEM(newargs, 0, *myerrno);
17031682
for (i = 1; i < nargs; i++) {
17041683
PyObject *val = PyTuple_GET_ITEM(args, i);
1705-
Py_INCREF(val);
1706-
PyTuple_SET_ITEM(newargs, i, val);
1684+
PyTuple_SET_ITEM(newargs, i, Py_NewRef(val));
17071685
}
17081686
Py_DECREF(args);
17091687
args = *p_args = newargs;
@@ -1738,12 +1716,10 @@ oserror_init(PyOSErrorObject *self, PyObject **p_args,
17381716
return -1;
17391717
}
17401718
else {
1741-
Py_INCREF(filename);
1742-
self->filename = filename;
1719+
self->filename = Py_NewRef(filename);
17431720

17441721
if (filename2 && filename2 != Py_None) {
1745-
Py_INCREF(filename2);
1746-
self->filename2 = filename2;
1722+
self->filename2 = Py_NewRef(filename2);
17471723
}
17481724

17491725
if (nargs >= 2 && nargs <= 5) {
@@ -1758,15 +1734,10 @@ oserror_init(PyOSErrorObject *self, PyObject **p_args,
17581734
}
17591735
}
17601736
}
1761-
Py_XINCREF(myerrno);
1762-
self->myerrno = myerrno;
1763-
1764-
Py_XINCREF(strerror);
1765-
self->strerror = strerror;
1766-
1737+
self->myerrno = Py_XNewRef(myerrno);
1738+
self->strerror = Py_XNewRef(strerror);
17671739
#ifdef MS_WINDOWS
1768-
Py_XINCREF(winerror);
1769-
self->winerror = winerror;
1740+
self->winerror = Py_XNewRef(winerror);
17701741
#endif
17711742

17721743
/* Steals the reference to args */
@@ -1992,7 +1963,7 @@ static PyObject *
19921963
OSError_reduce(PyOSErrorObject *self, PyObject *Py_UNUSED(ignored))
19931964
{
19941965
PyObject *args = self->args;
1995-
PyObject *res = NULL, *tmp;
1966+
PyObject *res = NULL;
19961967

19971968
/* self->args is only the first two real arguments if there was a
19981969
* file name given to OSError. */
@@ -2002,29 +1973,20 @@ OSError_reduce(PyOSErrorObject *self, PyObject *Py_UNUSED(ignored))
20021973
if (!args)
20031974
return NULL;
20041975

2005-
tmp = PyTuple_GET_ITEM(self->args, 0);
2006-
Py_INCREF(tmp);
2007-
PyTuple_SET_ITEM(args, 0, tmp);
2008-
2009-
tmp = PyTuple_GET_ITEM(self->args, 1);
2010-
Py_INCREF(tmp);
2011-
PyTuple_SET_ITEM(args, 1, tmp);
2012-
2013-
Py_INCREF(self->filename);
2014-
PyTuple_SET_ITEM(args, 2, self->filename);
1976+
PyTuple_SET_ITEM(args, 0, Py_NewRef(PyTuple_GET_ITEM(self->args, 0)));
1977+
PyTuple_SET_ITEM(args, 1, Py_NewRef(PyTuple_GET_ITEM(self->args, 1)));
1978+
PyTuple_SET_ITEM(args, 2, Py_NewRef(self->filename));
20151979

20161980
if (self->filename2) {
20171981
/*
20181982
* This tuple is essentially used as OSError(*args).
20191983
* So, to recreate filename2, we need to pass in
20201984
* winerror as well.
20211985
*/
2022-
Py_INCREF(Py_None);
2023-
PyTuple_SET_ITEM(args, 3, Py_None);
1986+
PyTuple_SET_ITEM(args, 3, Py_NewRef(Py_None));
20241987

20251988
/* filename2 */
2026-
Py_INCREF(self->filename2);
2027-
PyTuple_SET_ITEM(args, 4, self->filename2);
1989+
PyTuple_SET_ITEM(args, 4, Py_NewRef(self->filename2));
20281990
}
20291991
} else
20301992
Py_INCREF(args);
@@ -2185,8 +2147,7 @@ NameError_init(PyNameErrorObject *self, PyObject *args, PyObject *kwds)
21852147
}
21862148
Py_DECREF(empty_tuple);
21872149

2188-
Py_XINCREF(name);
2189-
Py_XSETREF(self->name, name);
2150+
Py_XSETREF(self->name, Py_XNewRef(name));
21902151

21912152
return 0;
21922153
}
@@ -2260,11 +2221,8 @@ AttributeError_init(PyAttributeErrorObject *self, PyObject *args, PyObject *kwds
22602221
}
22612222
Py_DECREF(empty_tuple);
22622223

2263-
Py_XINCREF(name);
2264-
Py_XSETREF(self->name, name);
2265-
2266-
Py_XINCREF(obj);
2267-
Py_XSETREF(self->obj, obj);
2224+
Py_XSETREF(self->name, Py_XNewRef(name));
2225+
Py_XSETREF(self->obj, Py_XNewRef(obj));
22682226

22692227
return 0;
22702228
}
@@ -2322,8 +2280,7 @@ SyntaxError_init(PySyntaxErrorObject *self, PyObject *args, PyObject *kwds)
23222280
return -1;
23232281

23242282
if (lenargs >= 1) {
2325-
Py_INCREF(PyTuple_GET_ITEM(args, 0));
2326-
Py_XSETREF(self->msg, PyTuple_GET_ITEM(args, 0));
2283+
Py_XSETREF(self->msg, Py_NewRef(PyTuple_GET_ITEM(args, 0)));
23272284
}
23282285
if (lenargs == 2) {
23292286
info = PyTuple_GET_ITEM(args, 1);
@@ -2419,8 +2376,7 @@ my_basename(PyObject *name)
24192376
return PyUnicode_Substring(name, offset, size);
24202377
}
24212378
else {
2422-
Py_INCREF(name);
2423-
return name;
2379+
return Py_NewRef(name);
24242380
}
24252381
}
24262382

@@ -2572,8 +2528,7 @@ get_string(PyObject *attr, const char *name)
25722528
PyErr_Format(PyExc_TypeError, "%.200s attribute must be bytes", name);
25732529
return NULL;
25742530
}
2575-
Py_INCREF(attr);
2576-
return attr;
2531+
return Py_NewRef(attr);
25772532
}
25782533

25792534
static PyObject *
@@ -2589,8 +2544,7 @@ get_unicode(PyObject *attr, const char *name)
25892544
"%.200s attribute must be unicode", name);
25902545
return NULL;
25912546
}
2592-
Py_INCREF(attr);
2593-
return attr;
2547+
return Py_NewRef(attr);
25942548
}
25952549

25962550
static int

Diff for: Objects/floatobject.c

+8-9
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,7 @@ convert_to_double(PyObject **v, double *dbl)
371371
}
372372
}
373373
else {
374-
Py_INCREF(Py_NotImplemented);
375-
*v = Py_NotImplemented;
374+
*v = Py_NewRef(Py_NotImplemented);
376375
return -1;
377376
}
378377
return 0;
@@ -904,8 +903,7 @@ float_is_integer_impl(PyObject *self)
904903
PyExc_ValueError);
905904
return NULL;
906905
}
907-
Py_INCREF(o);
908-
return o;
906+
return Py_NewRef(o);
909907
}
910908

911909
/*[clinic input]
@@ -1124,11 +1122,12 @@ float___round___impl(PyObject *self, PyObject *o_ndigits)
11241122
static PyObject *
11251123
float_float(PyObject *v)
11261124
{
1127-
if (PyFloat_CheckExact(v))
1128-
Py_INCREF(v);
1129-
else
1130-
v = PyFloat_FromDouble(((PyFloatObject *)v)->ob_fval);
1131-
return v;
1125+
if (PyFloat_CheckExact(v)) {
1126+
return Py_NewRef(v);
1127+
}
1128+
else {
1129+
return PyFloat_FromDouble(((PyFloatObject *)v)->ob_fval);
1130+
}
11321131
}
11331132

11341133
/*[clinic input]

0 commit comments

Comments
 (0)