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

+3-6
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

+3-7
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

+2-4
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

+2-3
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

+1-2
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

+1-3
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

+4-8
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

+1-2
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

+1-2
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

+3-6
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
}

Python/compile.c

+2-4
Original file line numberDiff line numberDiff line change
@@ -1459,8 +1459,7 @@ merge_consts_recursive(PyObject *const_cache, PyObject *o)
14591459
}
14601460
PyObject *u;
14611461
if (PyTuple_CheckExact(k)) {
1462-
u = PyTuple_GET_ITEM(k, 1);
1463-
Py_INCREF(u);
1462+
u = Py_NewRef(PyTuple_GET_ITEM(k, 1));
14641463
Py_DECREF(k);
14651464
}
14661465
else {
@@ -2732,8 +2731,7 @@ compiler_class(struct compiler *c, stmt_ty s)
27322731
{
27332732
location loc = LOCATION(firstlineno, firstlineno, 0, 0);
27342733
/* use the class name for name mangling */
2735-
Py_INCREF(s->v.ClassDef.name);
2736-
Py_XSETREF(c->u->u_private, s->v.ClassDef.name);
2734+
Py_XSETREF(c->u->u_private, Py_NewRef(s->v.ClassDef.name));
27372735
/* load (global) __name__ ... */
27382736
if (!compiler_nameop(c, loc, &_Py_ID(__name__), Load)) {
27392737
compiler_exit_scope(c);

Python/errors.c

+8-15
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,7 @@ _PyErr_SetObject(PyThreadState *tstate, PyObject *exception, PyObject *value)
178178
}
179179
if (value != NULL && PyExceptionInstance_Check(value))
180180
tb = PyException_GetTraceback(value);
181-
Py_XINCREF(exception);
182-
_PyErr_Restore(tstate, exception, value, tb);
181+
_PyErr_Restore(tstate, Py_XNewRef(exception), value, tb);
183182
}
184183

185184
void
@@ -489,13 +488,9 @@ _PyErr_GetExcInfo(PyThreadState *tstate,
489488
{
490489
_PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
491490

492-
*p_type = get_exc_type(exc_info->exc_value);
493-
*p_value = exc_info->exc_value;
494-
*p_traceback = get_exc_traceback(exc_info->exc_value);
495-
496-
Py_XINCREF(*p_type);
497-
Py_XINCREF(*p_value);
498-
Py_XINCREF(*p_traceback);
491+
*p_type = Py_XNewRef(get_exc_type(exc_info->exc_value));
492+
*p_value = Py_XNewRef(exc_info->exc_value);
493+
*p_traceback = Py_XNewRef(get_exc_traceback(exc_info->exc_value));
499494
}
500495

501496
PyObject*
@@ -674,9 +669,9 @@ _PyErr_FormatVFromCause(PyThreadState *tstate, PyObject *exception,
674669

675670
_PyErr_Fetch(tstate, &exc, &val2, &tb);
676671
_PyErr_NormalizeException(tstate, &exc, &val2, &tb);
677-
Py_INCREF(val);
678-
PyException_SetCause(val2, val);
679-
PyException_SetContext(val2, val);
672+
PyException_SetCause(val2, Py_NewRef(val));
673+
PyException_SetContext(val2, Py_NewRef(val));
674+
Py_DECREF(val);
680675
_PyErr_Restore(tstate, exc, val2, tb);
681676

682677
return NULL;
@@ -1165,9 +1160,7 @@ PyErr_NewException(const char *name, PyObject *base, PyObject *dict)
11651160
goto failure;
11661161
}
11671162
if (PyTuple_Check(base)) {
1168-
bases = base;
1169-
/* INCREF as we create a new ref in the else branch */
1170-
Py_INCREF(bases);
1163+
bases = Py_NewRef(base);
11711164
} else {
11721165
bases = PyTuple_Pack(1, base);
11731166
if (bases == NULL)

Python/hamt.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -838,8 +838,7 @@ hamt_node_bitmap_assoc(PyHamtNode_Bitmap *self,
838838

839839
if (self->b_array[j] == NULL) {
840840
new_node->a_array[i] =
841-
(PyHamtNode *)self->b_array[j + 1];
842-
Py_INCREF(new_node->a_array[i]);
841+
(PyHamtNode *)Py_NewRef(self->b_array[j + 1]);
843842
}
844843
else {
845844
int32_t rehash = hamt_hash(self->b_array[j]);

Python/import.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -625,8 +625,7 @@ import_add_module(PyThreadState *tstate, PyObject *name)
625625

626626
PyObject *m;
627627
if (PyDict_CheckExact(modules)) {
628-
m = PyDict_GetItemWithError(modules, name);
629-
Py_XINCREF(m);
628+
m = Py_XNewRef(PyDict_GetItemWithError(modules, name));
630629
}
631630
else {
632631
m = PyObject_GetItem(modules, name);

Python/marshal.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -326,8 +326,8 @@ w_ref(PyObject *v, char *flag, WFILE *p)
326326
goto err;
327327
}
328328
w = (int)s;
329-
Py_INCREF(v);
330-
if (_Py_hashtable_set(p->hashtable, v, (void *)(uintptr_t)w) < 0) {
329+
if (_Py_hashtable_set(p->hashtable, Py_NewRef(v),
330+
(void *)(uintptr_t)w) < 0) {
331331
Py_DECREF(v);
332332
goto err;
333333
}

Python/pythonrun.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -515,8 +515,7 @@ parse_syntax_error(PyObject *err, PyObject **message, PyObject **filename,
515515
if (v == Py_None) {
516516
Py_DECREF(v);
517517
_Py_DECLARE_STR(anon_string, "<string>");
518-
*filename = &_Py_STR(anon_string);
519-
Py_INCREF(*filename);
518+
*filename = Py_NewRef(&_Py_STR(anon_string));
520519
}
521520
else {
522521
*filename = v;

Python/structmember.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ PyMember_GetOne(const char *obj_addr, PyMemberDef *l)
7474
PyErr_Format(PyExc_AttributeError,
7575
"'%.200s' object has no attribute '%s'",
7676
tp->tp_name, l->name);
77-
}
77+
}
7878
Py_XINCREF(v);
7979
break;
8080
case T_LONGLONG:

Python/symtable.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -373,17 +373,17 @@ PySymtable_Lookup(struct symtable *st, void *key)
373373
if (k == NULL)
374374
return NULL;
375375
v = PyDict_GetItemWithError(st->st_blocks, k);
376+
Py_DECREF(k);
377+
376378
if (v) {
377379
assert(PySTEntry_Check(v));
378-
Py_INCREF(v);
379380
}
380381
else if (!PyErr_Occurred()) {
381382
PyErr_SetString(PyExc_KeyError,
382383
"unknown symbol table entry");
383384
}
384385

385-
Py_DECREF(k);
386-
return (PySTEntryObject *)v;
386+
return (PySTEntryObject *)Py_XNewRef(v);
387387
}
388388

389389
long

0 commit comments

Comments
 (0)