]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-99300: Replace Py_INCREF() with Py_NewRef() (#99530)
authorVictor Stinner <vstinner@python.org>
Wed, 16 Nov 2022 17:34:24 +0000 (18:34 +0100)
committerGitHub <noreply@github.com>
Wed, 16 Nov 2022 17:34:24 +0000 (18:34 +0100)
Replace Py_INCREF() and Py_XINCREF() using a cast with Py_NewRef()
and Py_XNewRef().

18 files changed:
Objects/dictobject.c
Objects/funcobject.c
Objects/genobject.c
Objects/namespaceobject.c
Objects/odictobject.c
Objects/rangeobject.c
Objects/typeobject.c
Objects/unionobject.c
Python/bltinmodule.c
Python/codecs.c
Python/compile.c
Python/errors.c
Python/hamt.c
Python/import.c
Python/marshal.c
Python/pythonrun.c
Python/structmember.c
Python/symtable.c

index fc487203869c0e8c6c943b388aa83924e1c06abe..51903ab271d8cb8b16e56e557d0ca4f0de3f9c2a 100644 (file)
@@ -1883,13 +1883,11 @@ _PyDict_SetItem_KnownHash(PyObject *op, PyObject *key, PyObject *value,
     assert(hash != -1);
     mp = (PyDictObject *)op;
 
-    Py_INCREF(key);
-    Py_INCREF(value);
     if (mp->ma_keys == Py_EMPTY_KEYS) {
-        return insert_to_emptydict(mp, key, hash, value);
+        return insert_to_emptydict(mp, Py_NewRef(key), hash, Py_NewRef(value));
     }
     /* insertdict() handles any resizing that might be necessary */
-    return insertdict(mp, key, hash, value);
+    return insertdict(mp, Py_NewRef(key), hash, Py_NewRef(value));
 }
 
 static void
@@ -2197,9 +2195,8 @@ _PyDict_Pop_KnownHash(PyObject *dict, PyObject *key, Py_hash_t hash, PyObject *d
         return NULL;
     }
     assert(old_value != NULL);
-    Py_INCREF(old_value);
     uint64_t new_version = _PyDict_NotifyEvent(PyDict_EVENT_DELETED, mp, key, NULL);
-    delitem_common(mp, hash, ix, old_value, new_version);
+    delitem_common(mp, hash, ix, Py_NewRef(old_value), new_version);
 
     ASSERT_CONSISTENT(mp);
     return old_value;
index 80117bfb203749a7259e0ae82009ae22901c54dd..d60e4eb8486e72f56a7adcecb20b6f556159c085 100644 (file)
@@ -47,9 +47,8 @@ PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname
 
     PyCodeObject *code_obj = (PyCodeObject *)Py_NewRef(code);
 
-    PyObject *name = code_obj->co_name;
-    assert(name != NULL);
-    Py_INCREF(name);
+    assert(code_obj->co_name != NULL);
+    PyObject *name = Py_NewRef(code_obj->co_name);
 
     if (!qualname) {
         qualname = code_obj->co_qualname;
@@ -525,10 +524,7 @@ func_get_annotations(PyFunctionObject *op, void *Py_UNUSED(ignored))
             return NULL;
     }
     PyObject *d = func_get_annotation_dict(op);
-    if (d) {
-        Py_INCREF(d);
-    }
-    return d;
+    return Py_XNewRef(d);
 }
 
 static int
index 3e0e0750970a6d252a28d6a8d9e51d4380fee25c..6661e3fb9461a4ea2d95415acc9df4f5da77d2b5 100644 (file)
@@ -341,8 +341,7 @@ _PyGen_yf(PyGenObject *gen)
             /* Not in a yield from */
             return NULL;
         }
-        yf = _PyFrame_StackPeek(frame);
-        Py_INCREF(yf);
+        yf = Py_NewRef(_PyFrame_StackPeek(frame));
     }
 
     return yf;
@@ -494,8 +493,7 @@ throw_here:
             /* Normalize to raise <class>, <instance> */
             Py_XDECREF(val);
             val = typ;
-            typ = PyExceptionInstance_Class(typ);
-            Py_INCREF(typ);
+            typ = Py_NewRef(PyExceptionInstance_Class(typ));
 
             if (tb == NULL)
                 /* Returns NULL if there's no traceback */
index 7875e7cafecb65529597d61b9158d0bf12b9b734..2cc4ddd3c91daabc306b50c29c1cecc9ed956441 100644 (file)
@@ -85,9 +85,8 @@ namespace_repr(PyObject *ns)
     if (pairs == NULL)
         goto error;
 
-    d = ((_PyNamespaceObject *)ns)->ns_dict;
-    assert(d != NULL);
-    Py_INCREF(d);
+    assert(((_PyNamespaceObject *)ns)->ns_dict != NULL);
+    d = Py_NewRef(((_PyNamespaceObject *)ns)->ns_dict);
 
     keys = PyDict_Keys(d);
     if (keys == NULL)
index 33af1d1d52b8e5dc7ff539edf99c54da026f7794..4976b70b5dff5a22ed9d04b772fbc8217db59c0e 100644 (file)
@@ -1114,8 +1114,7 @@ OrderedDict_popitem_impl(PyODictObject *self, int last)
     }
 
     node = last ? _odict_LAST(self) : _odict_FIRST(self);
-    key = _odictnode_KEY(node);
-    Py_INCREF(key);
+    key = Py_NewRef(_odictnode_KEY(node));
     value = _odict_popkey_hash((PyObject *)self, key, NULL, _odictnode_HASH(node));
     if (value == NULL)
         return NULL;
index 8e3083e5b828621522b2255c21c827f9a1b715ce..a889aa04db81f0e692d9de1909697f1a605971ee 100644 (file)
@@ -936,10 +936,8 @@ longrangeiter_reduce(longrangeiterobject *r, PyObject *Py_UNUSED(ignored))
     Py_DECREF(product);
     if (stop ==  NULL)
         return NULL;
-    Py_INCREF(r->start);
-    Py_INCREF(r->step);
     range =  (PyObject*)make_range_object(&PyRange_Type,
-                               r->start, stop, r->step);
+                               Py_NewRef(r->start), stop, Py_NewRef(r->step));
     if (range == NULL) {
         Py_DECREF(r->start);
         Py_DECREF(stop);
index 675d6d874de6bd7479ef2ae2c5f9bf3ebd53c85a..1e58d32f4962057be118e43ff0722bcc9b0447b1 100644 (file)
@@ -5437,8 +5437,7 @@ object_getstate_default(PyObject *obj, int required)
         for (i = 0; i < slotnames_size; i++) {
             PyObject *name, *value;
 
-            name = PyList_GET_ITEM(slotnames, i);
-            Py_INCREF(name);
+            name = Py_NewRef(PyList_GET_ITEM(slotnames, i));
             if (_PyObject_LookupAttr(obj, name, &value) < 0) {
                 Py_DECREF(name);
                 goto error;
@@ -5570,10 +5569,8 @@ _PyObject_GetNewArguments(PyObject *obj, PyObject **args, PyObject **kwargs)
             Py_DECREF(newargs);
             return -1;
         }
-        *args = PyTuple_GET_ITEM(newargs, 0);
-        Py_INCREF(*args);
-        *kwargs = PyTuple_GET_ITEM(newargs, 1);
-        Py_INCREF(*kwargs);
+        *args = Py_NewRef(PyTuple_GET_ITEM(newargs, 0));
+        *kwargs = Py_NewRef(PyTuple_GET_ITEM(newargs, 1));
         Py_DECREF(newargs);
 
         /* XXX We should perhaps allow None to be passed here. */
@@ -9601,8 +9598,7 @@ super_init_impl(PyObject *self, PyTypeObject *type, PyObject *obj) {
             return -1;
         Py_INCREF(obj);
     }
-    Py_INCREF(type);
-    Py_XSETREF(su->type, type);
+    Py_XSETREF(su->type, Py_NewRef(type));
     Py_XSETREF(su->obj, obj);
     Py_XSETREF(su->obj_type, obj_type);
     return 0;
index b2ac3509cee9320d3d81ebd22af0ca7d39e037a4..f273f7d15ef25f68ef8889707165fd7ffaf1006b 100644 (file)
@@ -295,8 +295,7 @@ union_getitem(PyObject *self, PyObject *item)
         res = make_union(newargs);
     }
     else {
-        res = PyTuple_GET_ITEM(newargs, 0);
-        Py_INCREF(res);
+        res = Py_NewRef(PyTuple_GET_ITEM(newargs, 0));
         for (Py_ssize_t iarg = 1; iarg < nargs; iarg++) {
             PyObject *arg = PyTuple_GET_ITEM(newargs, iarg);
             Py_SETREF(res, PyNumber_Or(res, arg));
index b23f52ebc707e238df21a6eee5c61560803d727a..119e21ae0da2900b8d244213d16d9ae17c4dca79 100644 (file)
@@ -2402,8 +2402,7 @@ builtin_vars(PyObject *self, PyObject *args)
     if (!PyArg_UnpackTuple(args, "vars", 0, 1, &v))
         return NULL;
     if (v == NULL) {
-        d = PyEval_GetLocals();
-        Py_XINCREF(d);
+        d = Py_XNewRef(PyEval_GetLocals());
     }
     else {
         if (_PyObject_LookupAttr(v, &_Py_ID(__dict__), &d) == 0) {
index 64addf00d526ed9f6b437979f2d4e2f25062cb5f..b2087b499dfdbaa5ab4d7dc61ce80c6ece4bf054 100644 (file)
@@ -428,8 +428,7 @@ _PyCodec_EncodeInternal(PyObject *object,
                         "encoder must return a tuple (object, integer)");
         goto onError;
     }
-    v = PyTuple_GET_ITEM(result,0);
-    Py_INCREF(v);
+    v = Py_NewRef(PyTuple_GET_ITEM(result,0));
     /* We don't check or use the second (integer) entry. */
 
     Py_DECREF(args);
@@ -473,8 +472,7 @@ _PyCodec_DecodeInternal(PyObject *object,
                         "decoder must return a tuple (object,integer)");
         goto onError;
     }
-    v = PyTuple_GET_ITEM(result,0);
-    Py_INCREF(v);
+    v = Py_NewRef(PyTuple_GET_ITEM(result,0));
     /* We don't check or use the second (integer) entry. */
 
     Py_DECREF(args);
@@ -569,8 +567,7 @@ PyObject *codec_getitem_checked(const char *encoding,
     if (codec == NULL)
         return NULL;
 
-    v = PyTuple_GET_ITEM(codec, index);
-    Py_INCREF(v);
+    v = Py_NewRef(PyTuple_GET_ITEM(codec, index));
     Py_DECREF(codec);
     return v;
 }
index bd41ebc1a9c5e0b7c5a4855c81a8765af9812af6..9226bc233ead747ebcb55fa08acf32cf646186b0 100644 (file)
@@ -1459,8 +1459,7 @@ merge_consts_recursive(PyObject *const_cache, PyObject *o)
             }
             PyObject *u;
             if (PyTuple_CheckExact(k)) {
-                u = PyTuple_GET_ITEM(k, 1);
-                Py_INCREF(u);
+                u = Py_NewRef(PyTuple_GET_ITEM(k, 1));
                 Py_DECREF(k);
             }
             else {
@@ -2732,8 +2731,7 @@ compiler_class(struct compiler *c, stmt_ty s)
     {
         location loc = LOCATION(firstlineno, firstlineno, 0, 0);
         /* use the class name for name mangling */
-        Py_INCREF(s->v.ClassDef.name);
-        Py_XSETREF(c->u->u_private, s->v.ClassDef.name);
+        Py_XSETREF(c->u->u_private, Py_NewRef(s->v.ClassDef.name));
         /* load (global) __name__ ... */
         if (!compiler_nameop(c, loc, &_Py_ID(__name__), Load)) {
             compiler_exit_scope(c);
index 7d2dd9b8c18b80b60b435714cf125a203776310c..d74ac347484fed52e4f430247734871a8297e05b 100644 (file)
@@ -178,8 +178,7 @@ _PyErr_SetObject(PyThreadState *tstate, PyObject *exception, PyObject *value)
     }
     if (value != NULL && PyExceptionInstance_Check(value))
         tb = PyException_GetTraceback(value);
-    Py_XINCREF(exception);
-    _PyErr_Restore(tstate, exception, value, tb);
+    _PyErr_Restore(tstate, Py_XNewRef(exception), value, tb);
 }
 
 void
@@ -489,13 +488,9 @@ _PyErr_GetExcInfo(PyThreadState *tstate,
 {
     _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
 
-    *p_type = get_exc_type(exc_info->exc_value);
-    *p_value = exc_info->exc_value;
-    *p_traceback = get_exc_traceback(exc_info->exc_value);
-
-    Py_XINCREF(*p_type);
-    Py_XINCREF(*p_value);
-    Py_XINCREF(*p_traceback);
+    *p_type = Py_XNewRef(get_exc_type(exc_info->exc_value));
+    *p_value = Py_XNewRef(exc_info->exc_value);
+    *p_traceback = Py_XNewRef(get_exc_traceback(exc_info->exc_value));
 }
 
 PyObject*
@@ -674,9 +669,9 @@ _PyErr_FormatVFromCause(PyThreadState *tstate, PyObject *exception,
 
     _PyErr_Fetch(tstate, &exc, &val2, &tb);
     _PyErr_NormalizeException(tstate, &exc, &val2, &tb);
-    Py_INCREF(val);
-    PyException_SetCause(val2, val);
-    PyException_SetContext(val2, val);
+    PyException_SetCause(val2, Py_NewRef(val));
+    PyException_SetContext(val2, Py_NewRef(val));
+    Py_DECREF(val);
     _PyErr_Restore(tstate, exc, val2, tb);
 
     return NULL;
@@ -1165,9 +1160,7 @@ PyErr_NewException(const char *name, PyObject *base, PyObject *dict)
             goto failure;
     }
     if (PyTuple_Check(base)) {
-        bases = base;
-        /* INCREF as we create a new ref in the else branch */
-        Py_INCREF(bases);
+        bases = Py_NewRef(base);
     } else {
         bases = PyTuple_Pack(1, base);
         if (bases == NULL)
index 4e61a1fc2b28853a132b3f17d8414f1f2560b422..c4e47eb9e5703a754322f3b3b2679ec946af619b 100644 (file)
@@ -838,8 +838,7 @@ hamt_node_bitmap_assoc(PyHamtNode_Bitmap *self,
 
                     if (self->b_array[j] == NULL) {
                         new_node->a_array[i] =
-                            (PyHamtNode *)self->b_array[j + 1];
-                        Py_INCREF(new_node->a_array[i]);
+                            (PyHamtNode *)Py_NewRef(self->b_array[j + 1]);
                     }
                     else {
                         int32_t rehash = hamt_hash(self->b_array[j]);
index c12b3e07b5d4375ea5a146a53ddbf5dd051d8e6b..ab4267210803394cc3ca3588fe8a5a72873906fe 100644 (file)
@@ -625,8 +625,7 @@ import_add_module(PyThreadState *tstate, PyObject *name)
 
     PyObject *m;
     if (PyDict_CheckExact(modules)) {
-        m = PyDict_GetItemWithError(modules, name);
-        Py_XINCREF(m);
+        m = Py_XNewRef(PyDict_GetItemWithError(modules, name));
     }
     else {
         m = PyObject_GetItem(modules, name);
index d6cc04d6f2abd3d3aa1bc39dc782236ec1677529..86cf57daa7702590f825c84ab4b0e4a01fa289b3 100644 (file)
@@ -326,8 +326,8 @@ w_ref(PyObject *v, char *flag, WFILE *p)
             goto err;
         }
         w = (int)s;
-        Py_INCREF(v);
-        if (_Py_hashtable_set(p->hashtable, v, (void *)(uintptr_t)w) < 0) {
+        if (_Py_hashtable_set(p->hashtable, Py_NewRef(v),
+                              (void *)(uintptr_t)w) < 0) {
             Py_DECREF(v);
             goto err;
         }
index eead707b541084f0ab651c2ba25830cd2067c1c7..70872222eb645876927e35e665cd64242e6db3cb 100644 (file)
@@ -515,8 +515,7 @@ parse_syntax_error(PyObject *err, PyObject **message, PyObject **filename,
     if (v == Py_None) {
         Py_DECREF(v);
         _Py_DECLARE_STR(anon_string, "<string>");
-        *filename = &_Py_STR(anon_string);
-        Py_INCREF(*filename);
+        *filename = Py_NewRef(&_Py_STR(anon_string));
     }
     else {
         *filename = v;
index b94f512d8720a0425c042492d114240ebd7fd118..1b8be28dcf2eb21b869c9297b5dda38d27549a5e 100644 (file)
@@ -74,7 +74,7 @@ PyMember_GetOne(const char *obj_addr, PyMemberDef *l)
             PyErr_Format(PyExc_AttributeError,
                          "'%.200s' object has no attribute '%s'",
                          tp->tp_name, l->name);
-       }
+        }
         Py_XINCREF(v);
         break;
     case T_LONGLONG:
index 9bb7ffa92ce090e4bd48e096c28ce6d1a689c8ac..fb2bb7d83835de6c63a6343a1f3e9fbf051c7005 100644 (file)
@@ -373,17 +373,17 @@ PySymtable_Lookup(struct symtable *st, void *key)
     if (k == NULL)
         return NULL;
     v = PyDict_GetItemWithError(st->st_blocks, k);
+    Py_DECREF(k);
+
     if (v) {
         assert(PySTEntry_Check(v));
-        Py_INCREF(v);
     }
     else if (!PyErr_Occurred()) {
         PyErr_SetString(PyExc_KeyError,
                         "unknown symbol table entry");
     }
 
-    Py_DECREF(k);
-    return (PySTEntryObject *)v;
+    return (PySTEntryObject *)Py_XNewRef(v);
 }
 
 long