Skip to content

Commit 8211cf5

Browse files
authored
gh-99300: Replace Py_INCREF() with Py_NewRef() (#99530)
Replace Py_INCREF() and Py_XINCREF() using a cast with Py_NewRef() and Py_XNewRef().
1 parent 19c1462 commit 8211cf5

18 files changed

+40
-74
lines changed

Objects/dictobject.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1883,13 +1883,11 @@ _PyDict_SetItem_KnownHash(PyObject *op, PyObject *key, PyObject *value,
18831883
assert(hash != -1);
18841884
mp = (PyDictObject *)op;
18851885

1886-
Py_INCREF(key);
1887-
Py_INCREF(value);
18881886
if (mp->ma_keys == Py_EMPTY_KEYS) {
1889-
return insert_to_emptydict(mp, key, hash, value);
1887+
return insert_to_emptydict(mp, Py_NewRef(key), hash, Py_NewRef(value));
18901888
}
18911889
/* insertdict() handles any resizing that might be necessary */
1892-
return insertdict(mp, key, hash, value);
1890+
return insertdict(mp, Py_NewRef(key), hash, Py_NewRef(value));
18931891
}
18941892

18951893
static void
@@ -2197,9 +2195,8 @@ _PyDict_Pop_KnownHash(PyObject *dict, PyObject *key, Py_hash_t hash, PyObject *d
21972195
return NULL;
21982196
}
21992197
assert(old_value != NULL);
2200-
Py_INCREF(old_value);
22012198
uint64_t new_version = _PyDict_NotifyEvent(PyDict_EVENT_DELETED, mp, key, NULL);
2202-
delitem_common(mp, hash, ix, old_value, new_version);
2199+
delitem_common(mp, hash, ix, Py_NewRef(old_value), new_version);
22032200

22042201
ASSERT_CONSISTENT(mp);
22052202
return old_value;

Objects/funcobject.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,8 @@ PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname
4747

4848
PyCodeObject *code_obj = (PyCodeObject *)Py_NewRef(code);
4949

50-
PyObject *name = code_obj->co_name;
51-
assert(name != NULL);
52-
Py_INCREF(name);
50+
assert(code_obj->co_name != NULL);
51+
PyObject *name = Py_NewRef(code_obj->co_name);
5352

5453
if (!qualname) {
5554
qualname = code_obj->co_qualname;
@@ -525,10 +524,7 @@ func_get_annotations(PyFunctionObject *op, void *Py_UNUSED(ignored))
525524
return NULL;
526525
}
527526
PyObject *d = func_get_annotation_dict(op);
528-
if (d) {
529-
Py_INCREF(d);
530-
}
531-
return d;
527+
return Py_XNewRef(d);
532528
}
533529

534530
static int

Objects/genobject.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,7 @@ _PyGen_yf(PyGenObject *gen)
341341
/* Not in a yield from */
342342
return NULL;
343343
}
344-
yf = _PyFrame_StackPeek(frame);
345-
Py_INCREF(yf);
344+
yf = Py_NewRef(_PyFrame_StackPeek(frame));
346345
}
347346

348347
return yf;
@@ -494,8 +493,7 @@ _gen_throw(PyGenObject *gen, int close_on_genexit,
494493
/* Normalize to raise <class>, <instance> */
495494
Py_XDECREF(val);
496495
val = typ;
497-
typ = PyExceptionInstance_Class(typ);
498-
Py_INCREF(typ);
496+
typ = Py_NewRef(PyExceptionInstance_Class(typ));
499497

500498
if (tb == NULL)
501499
/* Returns NULL if there's no traceback */

Objects/namespaceobject.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,8 @@ namespace_repr(PyObject *ns)
8585
if (pairs == NULL)
8686
goto error;
8787

88-
d = ((_PyNamespaceObject *)ns)->ns_dict;
89-
assert(d != NULL);
90-
Py_INCREF(d);
88+
assert(((_PyNamespaceObject *)ns)->ns_dict != NULL);
89+
d = Py_NewRef(((_PyNamespaceObject *)ns)->ns_dict);
9190

9291
keys = PyDict_Keys(d);
9392
if (keys == NULL)

Objects/odictobject.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,8 +1114,7 @@ OrderedDict_popitem_impl(PyODictObject *self, int last)
11141114
}
11151115

11161116
node = last ? _odict_LAST(self) : _odict_FIRST(self);
1117-
key = _odictnode_KEY(node);
1118-
Py_INCREF(key);
1117+
key = Py_NewRef(_odictnode_KEY(node));
11191118
value = _odict_popkey_hash((PyObject *)self, key, NULL, _odictnode_HASH(node));
11201119
if (value == NULL)
11211120
return NULL;

Objects/rangeobject.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -936,10 +936,8 @@ longrangeiter_reduce(longrangeiterobject *r, PyObject *Py_UNUSED(ignored))
936936
Py_DECREF(product);
937937
if (stop == NULL)
938938
return NULL;
939-
Py_INCREF(r->start);
940-
Py_INCREF(r->step);
941939
range = (PyObject*)make_range_object(&PyRange_Type,
942-
r->start, stop, r->step);
940+
Py_NewRef(r->start), stop, Py_NewRef(r->step));
943941
if (range == NULL) {
944942
Py_DECREF(r->start);
945943
Py_DECREF(stop);

Objects/typeobject.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5437,8 +5437,7 @@ object_getstate_default(PyObject *obj, int required)
54375437
for (i = 0; i < slotnames_size; i++) {
54385438
PyObject *name, *value;
54395439

5440-
name = PyList_GET_ITEM(slotnames, i);
5441-
Py_INCREF(name);
5440+
name = Py_NewRef(PyList_GET_ITEM(slotnames, i));
54425441
if (_PyObject_LookupAttr(obj, name, &value) < 0) {
54435442
Py_DECREF(name);
54445443
goto error;
@@ -5570,10 +5569,8 @@ _PyObject_GetNewArguments(PyObject *obj, PyObject **args, PyObject **kwargs)
55705569
Py_DECREF(newargs);
55715570
return -1;
55725571
}
5573-
*args = PyTuple_GET_ITEM(newargs, 0);
5574-
Py_INCREF(*args);
5575-
*kwargs = PyTuple_GET_ITEM(newargs, 1);
5576-
Py_INCREF(*kwargs);
5572+
*args = Py_NewRef(PyTuple_GET_ITEM(newargs, 0));
5573+
*kwargs = Py_NewRef(PyTuple_GET_ITEM(newargs, 1));
55775574
Py_DECREF(newargs);
55785575

55795576
/* XXX We should perhaps allow None to be passed here. */
@@ -9601,8 +9598,7 @@ super_init_impl(PyObject *self, PyTypeObject *type, PyObject *obj) {
96019598
return -1;
96029599
Py_INCREF(obj);
96039600
}
9604-
Py_INCREF(type);
9605-
Py_XSETREF(su->type, type);
9601+
Py_XSETREF(su->type, Py_NewRef(type));
96069602
Py_XSETREF(su->obj, obj);
96079603
Py_XSETREF(su->obj_type, obj_type);
96089604
return 0;

Objects/unionobject.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,7 @@ union_getitem(PyObject *self, PyObject *item)
295295
res = make_union(newargs);
296296
}
297297
else {
298-
res = PyTuple_GET_ITEM(newargs, 0);
299-
Py_INCREF(res);
298+
res = Py_NewRef(PyTuple_GET_ITEM(newargs, 0));
300299
for (Py_ssize_t iarg = 1; iarg < nargs; iarg++) {
301300
PyObject *arg = PyTuple_GET_ITEM(newargs, iarg);
302301
Py_SETREF(res, PyNumber_Or(res, arg));

Python/bltinmodule.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2402,8 +2402,7 @@ builtin_vars(PyObject *self, PyObject *args)
24022402
if (!PyArg_UnpackTuple(args, "vars", 0, 1, &v))
24032403
return NULL;
24042404
if (v == NULL) {
2405-
d = PyEval_GetLocals();
2406-
Py_XINCREF(d);
2405+
d = Py_XNewRef(PyEval_GetLocals());
24072406
}
24082407
else {
24092408
if (_PyObject_LookupAttr(v, &_Py_ID(__dict__), &d) == 0) {

Python/codecs.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -428,8 +428,7 @@ _PyCodec_EncodeInternal(PyObject *object,
428428
"encoder must return a tuple (object, integer)");
429429
goto onError;
430430
}
431-
v = PyTuple_GET_ITEM(result,0);
432-
Py_INCREF(v);
431+
v = Py_NewRef(PyTuple_GET_ITEM(result,0));
433432
/* We don't check or use the second (integer) entry. */
434433

435434
Py_DECREF(args);
@@ -473,8 +472,7 @@ _PyCodec_DecodeInternal(PyObject *object,
473472
"decoder must return a tuple (object,integer)");
474473
goto onError;
475474
}
476-
v = PyTuple_GET_ITEM(result,0);
477-
Py_INCREF(v);
475+
v = Py_NewRef(PyTuple_GET_ITEM(result,0));
478476
/* We don't check or use the second (integer) entry. */
479477

480478
Py_DECREF(args);
@@ -569,8 +567,7 @@ PyObject *codec_getitem_checked(const char *encoding,
569567
if (codec == NULL)
570568
return NULL;
571569

572-
v = PyTuple_GET_ITEM(codec, index);
573-
Py_INCREF(v);
570+
v = Py_NewRef(PyTuple_GET_ITEM(codec, index));
574571
Py_DECREF(codec);
575572
return v;
576573
}

0 commit comments

Comments
 (0)